felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1779872 - in /felix/trunk/converter/converter/src: main/java/org/apache/felix/converter/impl/ main/java/org/osgi/util/converter/ test/java/org/apache/felix/converter/impl/
Date Mon, 23 Jan 2017 01:18:43 GMT
Author: davidb
Date: Mon Jan 23 01:18:43 2017
New Revision: 1779872

URL: http://svn.apache.org/viewvc?rev=1779872&view=rev
Log:
Felix Converter - support for the Converter.equals() method

Added:
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterEqualsTest.java
Modified:
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/InternalConverter.java
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/StandardConverter.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=1779872&r1=1779871&r2=1779872&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
Mon Jan 23 01:18:43 2017
@@ -165,6 +165,7 @@ public class ConvertingImpl implements C
         return (T) to(ref.getType());
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public Object to(Type type) {
         Class<?> cls = null;
@@ -647,25 +648,15 @@ public class ConvertingImpl implements C
 
     @SuppressWarnings("rawtypes")
     private static Map createMapFromInterface(Object obj) {
-        Class intf = null;
-        for (Class i : obj.getClass().getInterfaces()) {
-            intf = i;
-            break;
-        }
-        if (intf == null)
-            throw new ConversionException("" + obj);
-
-        Set<String> invokedMethods = new HashSet<>();
-
         Map result = new HashMap();
-        for (Method md : obj.getClass().getDeclaredMethods()) {
-            handleInterfaceMethod(obj, md, invokedMethods, result);
-        }
-        for (Method md : obj.getClass().getMethods()) {
-            handleInterfaceMethod(obj, md, invokedMethods, result);
+        for (Class i : obj.getClass().getInterfaces()) {
+            for (Method md : i.getMethods()) {
+                handleInterfaceMethod(obj, md, new HashSet<>(), result);
+            }
+            if (result.size() > 0)
+                return result;
         }
-
-        return result;
+        throw new ConversionException("Cannot be converted to map: " + obj);
     }
 
     private static Object createMapOrCollection(Class<?> cls, int initialSize) {
@@ -829,6 +820,9 @@ public class ConvertingImpl implements C
         if (Modifier.isStatic(md.getModifiers()))
             return;
 
+        if (md.getParameterCount() > 0)
+            return;
+
         String mn = md.getName();
         if (invokedMethods.contains(mn))
             return; // method with this name already invoked

Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/InternalConverter.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/InternalConverter.java?rev=1779872&r1=1779871&r2=1779872&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/InternalConverter.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/InternalConverter.java
Mon Jan 23 01:18:43 2017
@@ -16,8 +16,37 @@
  */
 package org.apache.felix.converter.impl;
 
+import java.util.Map;
+
+import org.osgi.util.converter.ConversionException;
 import org.osgi.util.converter.Converter;
+import org.osgi.util.converter.TypeReference;
 
 public interface InternalConverter extends Converter {
     public InternalConverting convert(Object obj);
+
+    @Override
+    default public boolean equals(Object o1, Object o2) {
+        try {
+            Map<String, Object> m1 = convert(o1).to(new TypeReference<Map<String,Object>>(){});
+            Map<String, Object> m2 = convert(o2).to(new TypeReference<Map<String,Object>>(){});
+
+            if (m1.size() != m2.size())
+                return false;
+
+            for (Map.Entry<String, Object> entry : m1.entrySet()) {
+                Object val = m2.get(entry.getKey());
+                if (!equals(entry.getValue(), val))
+                    return false;
+            }
+            return true;
+        } catch (ConversionException e) {
+            // do lists as well
+
+            // It's a scalar - compare via strings
+            String s1 = convert(o1).to(String.class);
+            String s2 = convert(o2).to(String.class);
+            return s1.equals(s2);
+        }
+    }
 }

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/StandardConverter.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/StandardConverter.java?rev=1779872&r1=1779871&r2=1779872&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/StandardConverter.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/StandardConverter.java
Mon Jan 23 01:18:43 2017
@@ -45,4 +45,9 @@ public class StandardConverter implement
     public ConverterBuilder newConverterBuilder() {
         return converter.newConverterBuilder();
     }
+
+    @Override
+    public boolean equals(Object o1, Object o2) {
+        return converter.equals(o1, o2);
+    }
 }

Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterEqualsTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterEqualsTest.java?rev=1779872&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterEqualsTest.java
(added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterEqualsTest.java
Mon Jan 23 01:18:43 2017
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.converter.impl;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.junit.Test;
+import org.osgi.util.converter.Converter;
+import org.osgi.util.converter.StandardConverter;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ConverterEqualsTest {
+    @Test
+    public void testEquals() {
+        Converter c = new StandardConverter();
+
+        Dictionary<String, Object> o1 = new Hashtable<>();
+        o1.put("foo", 12);
+
+        Map<String, Object> o2 = new HashMap<>();
+        o2.put("foo", "12");
+        assertTrue(c.equals(o1, o2));
+
+        o2.put("bar", true);
+        assertFalse(c.equals(o1, o2));
+
+        o1.put("bar", true);
+        assertTrue(c.equals(o1, o2));
+
+        Map<Object,Object> hm = new HashMap<>();
+        o1.put("submap", hm);
+        assertFalse(c.equals(o1, o2));
+
+        Dictionary<String,Long> dm = new Hashtable<>();
+        o2.put("submap", dm);
+        assertTrue(c.equals(o1, o2));
+
+        hm.put(Boolean.TRUE, '4');
+        assertFalse(c.equals(o1, o2));
+
+        dm.put("true", 4L);
+        assertTrue(c.equals(o1, o2));
+    }
+}



Mime
View raw message