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/09/08 11:46:20 UTC

svn commit: r1759786 - in /felix/trunk/converter: codec/src/main/java/org/apache/felix/codec/impl/json/ codec/src/main/java/org/apache/felix/codec/impl/yaml/ codec/src/test/java/org/apache/felix/codec/impl/json/ converter/src/main/java/org/apache/felix...

Author: davidb
Date: Thu Sep  8 11:46:20 2016
New Revision: 1759786

URL: http://svn.apache.org/viewvc?rev=1759786&view=rev
Log:
Felix Converter - Refactoring of the API

Added:
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/StandardConverter.java
Removed:
    felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/util/
Modified:
    felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/json/JsonCodecImpl.java
    felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/yaml/YamlCodecImpl.java
    felix/trunk/converter/codec/src/test/java/org/apache/felix/codec/impl/json/JsonCodecTest.java
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java

Modified: felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/json/JsonCodecImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/json/JsonCodecImpl.java?rev=1759786&r1=1759785&r2=1759786&view=diff
==============================================================================
--- felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/json/JsonCodecImpl.java (original)
+++ felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/json/JsonCodecImpl.java Thu Sep  8 11:46:20 2016
@@ -27,14 +27,14 @@ import java.util.concurrent.ConcurrentHa
 import org.osgi.service.codec.Codec;
 import org.osgi.service.codec.Decoding;
 import org.osgi.service.codec.Encoding;
+import org.osgi.service.converter.StandardConverter;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.TypeReference;
-import org.osgi.service.converter.util.ConverterFactory;
 
 public class JsonCodecImpl implements Codec {
     private Map<String, Object> configuration = new ConcurrentHashMap<>();
     private ThreadLocal<Boolean> threadLocal = new ThreadLocal<>();
-    private Converter converter = ConverterFactory.standardConverter();
+    private Converter converter = new StandardConverter();
 
     @Override
     public Codec with(Converter c) {

Modified: felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/yaml/YamlCodecImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/yaml/YamlCodecImpl.java?rev=1759786&r1=1759785&r2=1759786&view=diff
==============================================================================
--- felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/yaml/YamlCodecImpl.java (original)
+++ felix/trunk/converter/codec/src/main/java/org/apache/felix/codec/impl/yaml/YamlCodecImpl.java Thu Sep  8 11:46:20 2016
@@ -23,13 +23,13 @@ import java.util.concurrent.ConcurrentHa
 import org.osgi.service.codec.Codec;
 import org.osgi.service.codec.Decoding;
 import org.osgi.service.codec.Encoding;
+import org.osgi.service.converter.StandardConverter;
 import org.osgi.service.converter.Converter;
 import org.osgi.service.converter.TypeReference;
-import org.osgi.service.converter.util.ConverterFactory;
 
 public class YamlCodecImpl implements Codec {
     private Map<String, Object> configuration = new ConcurrentHashMap<>();
-    private Converter converter = ConverterFactory.standardConverter();
+    private Converter converter = new StandardConverter();
 
     @Override
     public Codec with(Converter c) {

Modified: felix/trunk/converter/codec/src/test/java/org/apache/felix/codec/impl/json/JsonCodecTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/codec/src/test/java/org/apache/felix/codec/impl/json/JsonCodecTest.java?rev=1759786&r1=1759785&r2=1759786&view=diff
==============================================================================
--- felix/trunk/converter/codec/src/test/java/org/apache/felix/codec/impl/json/JsonCodecTest.java (original)
+++ felix/trunk/converter/codec/src/test/java/org/apache/felix/codec/impl/json/JsonCodecTest.java Thu Sep  8 11:46:20 2016
@@ -27,8 +27,8 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.service.converter.Adapter;
+import org.osgi.service.converter.StandardConverter;
 import org.osgi.service.converter.Converter;
-import org.osgi.service.converter.util.ConverterFactory;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -38,7 +38,7 @@ public class JsonCodecTest {
 
     @Before
     public void setUp() {
-        converter = ConverterFactory.standardConverter();
+        converter = new StandardConverter();
     }
 
     @After

Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java?rev=1759786&r1=1759785&r2=1759786&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java Thu Sep  8 11:46:20 2016
@@ -16,6 +16,15 @@
  */
 package org.apache.felix.converter.impl;
 
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.time.OffsetTime;
+import java.time.ZonedDateTime;
+import java.util.UUID;
+import java.util.regex.Pattern;
+
 import org.osgi.service.converter.Adapter;
 
 public class ConverterImpl implements InternalConverter {
@@ -28,4 +37,27 @@ public class ConverterImpl implements In
     public InternalConverting convert(Object obj) {
         return new ConvertingImpl(this, obj);
     }
+
+    public void addStandardRules(Adapter a) {
+        a.rule(Byte.class, String.class, v -> v.toString(), Byte::parseByte); // TODO test
+        a.rule(Character.class, Boolean.class, v -> v.charValue() != 0,
+                v -> v.booleanValue() ? (char) 1 : (char) 0);
+        a.rule(Character.class, String.class, v -> v.toString(),
+                v -> v.length() > 0 ? v.charAt(0) : 0);
+        a.rule(Class.class, String.class, Class::toString,
+                v -> getClass().getClassLoader().loadClass(v));
+        a.rule(Double.class, String.class, v -> v.toString(), Double::parseDouble); // TODO test
+        a.rule(Float.class, String.class, v -> v.toString(), Float::parseFloat); // TODO test
+        a.rule(Integer.class, String.class, v -> v.toString(), Integer::parseInt);
+        a.rule(LocalDateTime.class, String.class, LocalDateTime::toString, LocalDateTime::parse);
+        a.rule(LocalDate.class, String.class, LocalDate::toString, LocalDate::parse);
+        a.rule(LocalTime.class, String.class, LocalTime::toString, LocalTime::parse);
+        a.rule(Long.class, String.class, v -> v.toString(), Long::parseLong); // TODO test
+        a.rule(OffsetDateTime.class, String.class, OffsetDateTime::toString, OffsetDateTime::parse);
+        a.rule(OffsetTime.class, String.class, OffsetTime::toString, OffsetTime::parse);
+        a.rule(Pattern.class, String.class, Pattern::toString, Pattern::compile);
+        a.rule(Short.class, String.class, v -> v.toString(), Short::parseShort); // TODO test
+        a.rule(UUID.class, String.class, UUID::toString, UUID::fromString);
+        a.rule(ZonedDateTime.class, String.class, ZonedDateTime::toString, ZonedDateTime::parse);
+    }
 }

Added: felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/StandardConverter.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/StandardConverter.java?rev=1759786&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/StandardConverter.java (added)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/service/converter/StandardConverter.java Thu Sep  8 11:46:20 2016
@@ -0,0 +1,40 @@
+/*
+ * 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.osgi.service.converter;
+
+import org.apache.felix.converter.impl.ConverterImpl;
+
+public class StandardConverter implements Converter {
+    private final Adapter adapter;
+
+    public StandardConverter() {
+        ConverterImpl impl = new ConverterImpl();
+        Adapter a = impl.newAdapter();
+        impl.addStandardRules(a);
+        adapter = a;
+    }
+
+    @Override
+    public Converting convert(Object obj) {
+        return adapter.convert(obj);
+    }
+
+    @Override
+    public Adapter newAdapter() {
+        return adapter.newAdapter();
+    }
+}