You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2016/04/15 11:51:34 UTC
svn commit: r1739266 - in /felix/trunk/converter/src:
main/java/org/apache/felix/converter/impl/ConvertingImpl.java
test/java/org/apache/felix/converter/impl/ConverterTest.java
Author: davidb
Date: Fri Apr 15 09:51:34 2016
New Revision: 1739266
URL: http://svn.apache.org/viewvc?rev=1739266&view=rev
Log:
Felix Converter Service - add some support for Arrays and Collections.
Modified:
felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1739266&r1=1739265&r2=1739266&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java (original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java Fri Apr 15 09:51:34 2016
@@ -69,30 +69,48 @@ public class ConvertingImpl implements C
if (res != null)
return res;
- if (String.class.equals(targetCls)) {
- if (object instanceof Object[]) {
- return (T) ((Object[])object)[0];
- } else if (object instanceof Collection) {
- Collection<?> c = (Collection<?>) object;
- if (c.size() == 0) {
- return null;
- }
- }
- return (T) object.toString();
- } else if (String[].class.equals(targetCls)) {
- String[] sa = new String[1];
- sa[0] = object.toString();
- return (T) sa;
+ if (targetCls.isArray()) {
+ return convertToArray(targetCls);
+ } else if (Collection.class.isAssignableFrom(targetCls)) {
+ return convertToCollection(targetCls);
}
+ // TODO maps
- res = (T) tryStandardMethods(targetCls);
- if (res != null) {
- return res;
+ // At this point we know that the target is a 'singular' type: not a map, collection or array
+ if (object instanceof Collection) {
+ Collection<?> coll = (Collection<?>) object;
+ if (coll.size() == 0)
+ return null;
+ else
+ return converter.convert(coll.iterator().next()).to(cls);
+ } else if (object instanceof Object[]) {
+ Object[] arr = (Object[]) object;
+ if (arr.length == 0)
+ return null;
+ else
+ return converter.convert(arr[0]).to(cls);
+ }
+
+ T res2 = (T) tryStandardMethods(targetCls);
+ if (res2 != null) {
+ return res2;
} else {
return null;
}
}
+ @SuppressWarnings("unchecked")
+ private <T> T convertToArray(Class<?> targetClass) {
+ String[] sa = new String[1];
+ sa[0] = object.toString();
+ return (T) sa;
+ }
+
+ private <T> T convertToCollection(Class<?> targetCls) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
private Object handleNull(Class<?> cls) {
Class<?> boxed = boxedClasses.get(cls);
if (boxed == null) {
@@ -101,10 +119,6 @@ public class ConvertingImpl implements C
}
if (cls.equals(boolean.class)) {
return false;
- } else if (cls.equals(Class.class)) {
- return null;
- } else if (Enum.class.isAssignableFrom(cls)) {
- return null;
} else {
return 0;
}
Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1739266&r1=1739265&r2=1739266&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java (original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java Fri Apr 15 09:51:34 2016
@@ -19,12 +19,17 @@ package org.apache.felix.converter.impl;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
+import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import org.osgi.service.converter.Adapter;
import org.osgi.service.converter.Converter;
@@ -148,6 +153,28 @@ public class ConverterTest {
assertEquals("127.0.0.1", url.getHost());
assertEquals(1234, url.getPort());
assertEquals("/blah", url.getPath());
+
+ assertNull(converter.convert(null).to(URL.class));
+ assertNull(converter.convert(Collections.emptyList()).to(URL.class));
+ }
+
+ @Test
+ public void testFromMultiToSingle() {
+ assertEquals("abc", converter.convert(Collections.singleton("abc")).to(String.class));
+ assertEquals("abc", converter.convert(Arrays.asList("abc", "def", "ghi")).to(String.class));
+ assertEquals(42, (int) converter.convert(Arrays.asList("42", "17")).to(Integer.class));
+ MyClass2 mc = converter.convert(new String[] {"xxx", "yyy", "zzz"}).to(MyClass2.class);
+ assertEquals("xxx", mc.toString());
+ MyClass2[] arr = new MyClass2[] {new MyClass2("3.1412"), new MyClass2("6.2824")};
+ assertEquals(Float.valueOf(3.1412f), Float.valueOf(converter.convert(arr).to(float.class)));
+ }
+
+ @Test @Ignore
+ public void testFromListToSet() {
+ List<Object> l = new ArrayList<>(Arrays.asList("A", 'B', 333));
+
+ Set<?> s = converter.convert(l).to(Set.class);
+ assertEquals(3, s.size());
}
@Test
@@ -172,4 +199,15 @@ public class ConverterTest {
assertArrayEquals(sa, adapter.convert("A,B").to(String[].class));
}
+ static class MyClass2 {
+ private final String value;
+ public MyClass2(String v) {
+ value = v;
+ }
+
+ @Override
+ public String toString() {
+ return value;
+ }
+ }
}