← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20043: ValueType. Avoiding duplication of various type constants

 

------------------------------------------------------------
revno: 20043
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-09-08 18:23:34 +0200
message:
  ValueType. Avoiding duplication of various type constants
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/util/ObjectUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java	2015-09-04 10:49:08 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java	2015-09-08 16:23:34 +0000
@@ -29,8 +29,10 @@
  */
 
 import com.google.common.collect.Lists;
+
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.trackedentity.TrackedEntityInstance;
+import org.hisp.dhis.util.ObjectUtils;
 
 import java.util.Date;
 import java.util.List;
@@ -60,26 +62,18 @@
     OPTION_SET( String.class ),
     USERNAME( String.class );
 
-    public static List<ValueType> INTEGER_TYPES = Lists.newArrayList(
-        INTEGER, INTEGER_POSITIVE, INTEGER_NEGATIVE, INTEGER_ZERO_OR_POSITIVE
-    );
-
-    public static List<ValueType> NUMERIC_TYPES = Lists.newArrayList(
-        INTEGER, INTEGER_POSITIVE, INTEGER_NEGATIVE, INTEGER_ZERO_OR_POSITIVE, NUMBER, UNIT_INTERVAL, PERCENTAGE
-    );
-
-    public static List<ValueType> TEXT_TYPES = Lists.newArrayList( TEXT, LONG_TEXT, LETTER );
-
-    public static List<String> INTEGER_TYPE_STRINGS = Lists.newArrayList(
-        INTEGER.toString(), INTEGER_POSITIVE.toString(), INTEGER_NEGATIVE.toString(), INTEGER_ZERO_OR_POSITIVE.toString()
-    );
-
-    public static List<String> NUMERIC_TYPE_STRINGS = Lists.newArrayList(
-        INTEGER.toString(), INTEGER_POSITIVE.toString(), INTEGER_NEGATIVE.toString(), INTEGER_ZERO_OR_POSITIVE.toString(),
-        NUMBER.toString(), UNIT_INTERVAL.toString(), PERCENTAGE.toString()
-    );
-
-    public static List<String> TEXT_TYPE_STRINGS = Lists.newArrayList( TEXT.toString(), LONG_TEXT.toString(), LETTER.toString() );
+    public static final List<ValueType> INTEGER_TYPES = Lists.newArrayList(
+        INTEGER, INTEGER_POSITIVE, INTEGER_NEGATIVE, INTEGER_ZERO_OR_POSITIVE );
+
+    public static final List<ValueType> NUMERIC_TYPES = Lists.newArrayList(
+        INTEGER, INTEGER_POSITIVE, INTEGER_NEGATIVE, INTEGER_ZERO_OR_POSITIVE, NUMBER, UNIT_INTERVAL, PERCENTAGE );
+
+    public static final List<ValueType> TEXT_TYPES = Lists.newArrayList( 
+        TEXT, LONG_TEXT, LETTER );
+
+    public static final List<String> INTEGER_TYPE_STRINGS = ObjectUtils.asStringList( INTEGER_TYPES );
+    public static final List<String> NUMERIC_TYPE_STRINGS = ObjectUtils.asStringList( INTEGER_TYPES );
+    public static final List<String> TEXT_TYPE_STRINGS = ObjectUtils.asStringList( TEXT_TYPES );
 
     private final Class<?> javaClass;
 

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/util/ObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/util/ObjectUtils.java	2015-09-03 09:26:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/util/ObjectUtils.java	2015-09-08 16:23:34 +0000
@@ -1,5 +1,9 @@
 package org.hisp.dhis.util;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
 /*
  * Copyright (c) 2004-2015, University of Oslo
  * All rights reserved.
@@ -53,4 +57,23 @@
         
         return null;
     }
+    
+    /**
+     * Returns a list of strings, where the strings are the result of calling
+     * String.valueOf( Object ) of each object in the given collection.
+     * 
+     * @param objects the collection of objects.
+     * @return a list of strings.
+     */
+    public static List<String> asStringList( Collection<? extends Object> objects )
+    {
+        List<String> list = new ArrayList<>();
+        
+        for ( Object object : objects )
+        {
+            list.add( String.valueOf( object ) );
+        }
+        
+        return list;
+    }
 }