felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1804537 - in /felix/trunk/converter/converter/src: main/java/org/apache/felix/converter/impl/ConverterImpl.java test/java/org/apache/felix/converter/impl/ConverterTest.java
Date Wed, 09 Aug 2017 14:56:48 GMT
Author: davidb
Date: Wed Aug  9 14:56:48 2017
New Revision: 1804537

URL: http://svn.apache.org/viewvc?rev=1804537&view=rev
Log:
Fix conversions between long and Calendar

Modified:
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java

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=1804537&r1=1804536&r2=1804537&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
Wed Aug  9 14:56:48 2017
@@ -21,6 +21,7 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.TimeZone;
 import java.util.UUID;
 import java.util.regex.Pattern;
 
@@ -32,7 +33,10 @@ import org.osgi.util.converter.TypeRule;
 import org.osgi.util.function.Function;
 
 public class ConverterImpl implements InternalConverter {
-    private final SimpleDateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
+    private static final SimpleDateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
+    static {
+        ISO8601_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
+    }
 
     @Override
     public InternalConverting convert(Object obj) {
@@ -45,7 +49,7 @@ public class ConverterImpl implements In
     }
 
     public void addStandardRules(ConverterBuilder cb) {
-//        cb.rule(new Rule<Calendar, String>(f -> f.getTime().toInstant().toString())
{});
+        // Not written using lambda's because this code needs to run with Java 7
         cb.rule(new Rule<Calendar, String>(new Function<Calendar, String>() {
             @Override
             public String apply(Calendar f) {
@@ -53,11 +57,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String, Calendar>(f -> {
-//            Calendar cc = Calendar.getInstance();
-//            cc.setTime(Date.from(Instant.parse(f)));
-//            return cc;
-//        }) {});
         cb.rule(new Rule<String, Calendar>(new Function<String, Calendar>() {
             @Override
             public Calendar apply(String f) {
@@ -71,7 +70,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Calendar,Long>(f -> f.getTime().getTime()) {});
         cb.rule(new Rule<Calendar, Long>(new Function<Calendar, Long>() {
             @Override
             public Long apply(Calendar f) {
@@ -79,16 +77,15 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Long,Calendar>(f -> new Calendar.Builder().setInstant(f).build())
{});
         cb.rule(new Rule<Long, Calendar>(new Function<Long, Calendar>() {
             @Override
             public Calendar apply(Long f) {
-//                new Calendar.Builder().setInstant(f).build()
-                return null;
+                Calendar c = Calendar.getInstance();
+                c.setTimeInMillis(f);
+                return c;
             }
         }) {});
 
-//        cb.rule(new Rule<Character,Boolean>(c -> c.charValue() != 0) {});
         cb.rule(new Rule<Character, Boolean>(new Function<Character, Boolean>()
{
             @Override
             public Boolean apply(Character c) {
@@ -96,7 +93,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Boolean,Character>(b -> b.booleanValue() ? (char) 1 :
(char) 0) {});
         cb.rule(new Rule<Boolean, Character>(new Function<Boolean, Character>()
{
             @Override
             public Character apply(Boolean b) {
@@ -104,7 +100,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Character,Integer>(c -> (int) c.charValue()) {});
         cb.rule(new Rule<Character, Integer>(new Function<Character, Integer>()
{
             @Override
             public Integer apply(Character c) {
@@ -112,7 +107,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Character,Long>(c -> (long) c.charValue()) {});
         cb.rule(new Rule<Character, Long>(new Function<Character, Long>() {
             @Override
             public Long apply(Character c) {
@@ -120,7 +114,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String,Character>(f -> f.length() > 0 ? f.charAt(0)
: 0) {});
         cb.rule(new Rule<String, Character>(new Function<String, Character>()
{
             @Override
             public Character apply(String f) {
@@ -128,7 +121,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String,Class<?>>(this::loadClassUnchecked) {});
         cb.rule(new Rule<String, Class<?>>(new Function<String, Class<?>>()
{
             @Override
             public Class<?> apply(String cn) {
@@ -136,14 +128,13 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Date,Long>(Date::getTime) {});
         cb.rule(new Rule<Date, Long>(new Function<Date, Long>() {
             @Override
             public Long apply(Date d) {
                 return d.getTime();
             }
         }) {});
-//        cb.rule(new Rule<Long,Date>(f -> new Date(f)) {});
+
         cb.rule(new Rule<Long, Date>(new Function<Long, Date>() {
             @Override
             public Date apply(Long f) {
@@ -151,7 +142,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Date,String>(f -> f.toInstant().toString()) {});
         cb.rule(new Rule<Date, String>(new Function<Date, String>() {
             @Override
             public String apply(Date d) {
@@ -159,7 +149,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String,Date>(f -> Date.from(Instant.parse(f))) {});
         cb.rule(new Rule<String, Date>(new Function<String, Date>() {
             @Override
             public Date apply(String f) {
@@ -171,7 +160,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String, Pattern>(Pattern::compile) {});
         cb.rule(new Rule<String, Pattern>(new Function<String, Pattern>() {
             @Override
             public Pattern apply(String ps) {
@@ -179,7 +167,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String, UUID>(UUID::fromString) {});
         cb.rule(new Rule<String, UUID>(new Function<String, UUID>() {
             @Override
             public UUID apply(String uuid) {
@@ -188,7 +175,6 @@ public class ConverterImpl implements In
         }) {});
 
         // Special conversions between character arrays and String
-//        cb.rule(new Rule<char[], String>(this::charArrayToString) {});
         cb.rule(new Rule<char[], String>(new Function<char[], String>() {
             @Override
             public String apply(char[] ca) {
@@ -196,7 +182,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<Character[], String>(this::characterArrayToString) {});
         cb.rule(new Rule<Character[], String>(new Function<Character[], String>()
{
             @Override
             public String apply(Character[] ca) {
@@ -204,14 +189,13 @@ public class ConverterImpl implements In
             }
         }) {});
 
-//        cb.rule(new Rule<String, char[]>(this::stringToCharArray) {});
         cb.rule(new Rule<String, char[]>(new Function<String, char[]>() {
             @Override
             public char[] apply(String s) {
                 return stringToCharArray(s);
             }
         }) {});
-//        cb.rule(new Rule<String, Character[]>(this::stringToCharacterArray) {});
+
         cb.rule(new Rule<String, Character[]>(new Function<String, Character[]>()
{
             @Override
             public Character[] apply(String s) {
@@ -219,18 +203,11 @@ public class ConverterImpl implements In
             }
         }) {});
 
-        // TODO
-//      cb.rule(new Rule<String, LocalDateTime>(LocalDateTime::parse) {});
         reflectiveAddRule(cb, "java.time.LocalDateTime", "parse");
-//      cb.rule(new Rule<String, LocalDate>(LocalDate::parse) {});
         reflectiveAddRule(cb, "java.time.LocalDate", "parse");
-//      cb.rule(new Rule<String, LocalTime>(LocalTime::parse) {});
         reflectiveAddRule(cb, "java.time.LocalTime", "parse");
-//      cb.rule(new Rule<String, OffsetDateTime>(OffsetDateTime::parse) {});
         reflectiveAddRule(cb, "java.time.OffsetDateTime", "parse");
-//      cb.rule(new Rule<String, OffsetTime>(OffsetTime::parse) {});
         reflectiveAddRule(cb, "java.time.OffsetTime", "parse");
-//      cb.rule(new Rule<String, ZonedDateTime>(ZonedDateTime::parse) {});
         reflectiveAddRule(cb, "java.time.ZonedDateTime", "parse");
     }
 

Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1804537&r1=1804536&r2=1804537&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
(original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Wed Aug  9 14:56:48 2017
@@ -512,15 +512,28 @@ public class ConverterTest {
         Converter c = converter;
 
         String s = c.convert(d).toString();
-        assertEquals("1971-02-13T12:37:41+01:00", s);
+        assertEquals("1971-02-13T11:37:41Z", s);
         assertEquals(d, c.convert(s).to(Date.class));
 
         String s2 = c.convert(cal).toString();
-        assertEquals("1971-02-13T12:37:41+01:00", s2);
+        assertEquals("1971-02-13T11:37:41Z", s2);
         Calendar cal2 = c.convert(s2).to(Calendar.class);
         assertEquals(cal.getTime(), cal2.getTime());
     }
 
+    @Test
+    public void testCalendarLong() {
+        Calendar cal = new GregorianCalendar(1971, 1, 13, 12, 37, 41);
+        TimeZone tz =TimeZone.getTimeZone("UTC");
+        cal.setTimeZone(tz);
+
+        long l = converter.convert(cal).to(Long.class);
+        assertEquals(l, cal.getTimeInMillis());
+
+        Calendar cal2 = converter.convert(l).to(Calendar.class);
+        assertEquals(cal.getTime(), cal2.getTime());
+    }
+
     @Test
     public void testDefaultValue() {
         long l = converter.convert(null).defaultValue("12").to(Long.class);



Mime
View raw message