Author: davidb
Date: Wed Jun 21 13:32:25 2017
New Revision: 1799441

URL: http://svn.apache.org/viewvc?rev=1799441&view=rev
Log:
Felix Converter - add support for conversions of Map Entries.

Modified:
    
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java

Modified: 
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1799441&r1=1799440&r2=1799441&view=diff
==============================================================================
--- 
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
 (original)
+++ 
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
 Wed Jun 21 13:32:25 2017
@@ -216,9 +216,11 @@ public class ConvertingImpl implements C
 
         // At this point we know that the target is a 'singular' type: not a 
map, collection or array
         if (Collection.class.isAssignableFrom(sourceClass)) {
-            return convertCollectionToSingleValue(cls);
+            return convertCollectionToSingleValue(targetAsClass);
+        } else if (object instanceof Map.Entry) {
+            return convertMapEntryToSingleValue(targetAsClass);
         } else if ((object = asBoxedArray(object)) instanceof Object[]) {
-            return convertArrayToSingleValue(cls);
+            return convertArrayToSingleValue(targetAsClass);
         }
 
         Object res2 = tryStandardMethods();
@@ -248,6 +250,30 @@ public class ConvertingImpl implements C
             return converter.convert(coll.iterator().next()).to(cls);
     }
 
+    @SuppressWarnings("rawtypes")
+    private Object convertMapEntryToSingleValue(Class<?> cls) {
+        Map.Entry entry = (Map.Entry) object;
+
+        Class keyCls = entry.getKey() != null ? entry.getKey().getClass() : 
null;
+        Class valueCls = entry.getValue() != null ? 
entry.getValue().getClass() : null;
+
+        if (cls.equals(keyCls)) {
+            return converter.convert(entry.getKey()).to(cls);
+        } else if (cls.equals(valueCls)) {
+            return converter.convert(entry.getValue()).to(cls);
+        } else if (cls.isAssignableFrom(keyCls)) {
+            return converter.convert(entry.getKey()).to(cls);
+        } else if (cls.isAssignableFrom(valueCls)) {
+            return converter.convert(entry.getValue()).to(cls);
+        } else if (entry.getKey() instanceof String) {
+            return converter.convert(entry.getKey()).to(cls);
+        } else if (entry.getValue() instanceof String) {
+            return converter.convert(entry.getValue()).to(cls);
+        }
+
+        return 
converter.convert(converter.convert(entry.getKey()).to(String.class)).to(cls);
+    }
+
     @SuppressWarnings("unchecked")
     private <T> T convertToArray() {
         Collection<?> collectionView = collectionView(object);

Modified: 
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java?rev=1799441&r1=1799440&r2=1799441&view=diff
==============================================================================
--- 
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
 (original)
+++ 
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
 Wed Jun 21 13:32:25 2017
@@ -429,6 +429,57 @@ public class ConverterMapTest {
         assertEquals(true, m3.invoke(ta2));
     }
 
+    @Test
+    public void testMapEntry() {
+        Map<String, Boolean> m1 = Collections.singletonMap("Hi", Boolean.TRUE);
+        Map.Entry<String, Boolean> e1 = getMapEntry(m1);
+
+        assertTrue(converter.convert(e1).to(Boolean.class));
+        assertTrue(converter.convert(e1).to(boolean.class));
+        assertEquals("Hi", converter.convert(e1).to(String.class));
+
+    }
+
+    @Test
+    public void testMapEntry1() {
+        Map<Long, String> m1 = Collections.singletonMap(17L, "18");
+        Map.Entry<Long, String> e1 = getMapEntry(m1);
+
+        assertEquals(17L, converter.convert(e1).to(Number.class));
+        assertEquals("18", converter.convert(e1).to(String.class));
+        assertEquals("18", converter.convert(e1).to(Bar.class).value);
+    }
+
+    @Test
+    public void testMapEntry2() {
+        Map<String, Short> m1 = Collections.singletonMap("123", 
Short.valueOf((short) 567));
+        Map.Entry<String, Short> e1 = getMapEntry(m1);
+
+        assertEquals(Integer.valueOf(123), 
converter.convert(e1).to(Integer.class));
+    }
+
+    @Test
+    public void testMapEntry3() {
+        Map<Long,Long> l1 = Collections.singletonMap(9L, 10L);
+        Map.Entry<Long, Long> e1 = getMapEntry(l1);
+
+        assertEquals("Should take the key if key and value are equally 
suitable",
+                9L, (long) converter.convert(e1).to(long.class));
+    }
+
+    @Test
+    public void testMapEntry4() {
+        Map<Foo, Foo> m1 = Collections.singletonMap(new Foo(111), new 
Foo(999));
+        Map.Entry<Foo, Foo> e1 = getMapEntry(m1);
+
+        assertEquals("111", converter.convert(e1).to(Bar.class).value);
+    }
+
+    private <K,V> Map.Entry<K,V> getMapEntry(Map<K,V> map) {
+        assertEquals("This method assumes a map of size 1", 1, map.size());
+        return map.entrySet().iterator().next();
+    }
+
     interface TestInterface {
         String foo();
         int bar();
@@ -446,4 +497,24 @@ public class ConverterMapTest {
        String[] value();
        long somethingElse() default -87;
     }
+
+    private static class Foo {
+        private final int value;
+
+        Foo(int v) {
+            value = v;
+        }
+
+        @Override
+        public String toString() {
+            return "" + value;
+        }
+    }
+
+    public static class Bar {
+        final String value;
+        public Bar(String v) {
+            value = v;
+        }
+    }
 }


Reply via email to