← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21335: IdScheme, using a resuable Map instad of switch/case

 

------------------------------------------------------------
revno: 21335
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-12-04 14:54:03 +0100
message:
  IdScheme, using a resuable Map instad of switch/case
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdScheme.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/IdScheme.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdScheme.java	2015-12-04 11:47:08 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdScheme.java	2015-12-04 13:54:03 +0000
@@ -29,6 +29,8 @@
  */
 
 import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableMap;
+
 import org.springframework.util.StringUtils;
 
 /**
@@ -43,6 +45,14 @@
     public static final IdScheme CODE = new IdScheme( IdentifiableProperty.CODE );
     public static final IdScheme NAME = new IdScheme( IdentifiableProperty.NAME );
 
+    public static final ImmutableMap<IdentifiableProperty, IdScheme> IDPROPERTY_IDSCHEME_MAP = 
+        ImmutableMap.<IdentifiableProperty, IdScheme>builder().
+        put( IdentifiableProperty.ID, IdScheme.ID ).
+        put( IdentifiableProperty.UID, IdScheme.UID ).
+        put( IdentifiableProperty.UUID, IdScheme.UUID ).
+        put( IdentifiableProperty.CODE, IdScheme.CODE ).
+        put( IdentifiableProperty.NAME, IdScheme.NAME ).build();
+    
     private IdentifiableProperty identifiableProperty;
 
     private String attribute;
@@ -78,22 +88,9 @@
         {
             return IdScheme.NULL;
         }
-
-        switch ( property )
-        {
-            case ID:
-                return IdScheme.ID;
-            case UID:
-                return IdScheme.UID;
-            case UUID:
-                return IdScheme.UUID;
-            case CODE:
-                return IdScheme.CODE;
-            case NAME:
-                return IdScheme.NAME;
-        }
-
-        return new IdScheme( property );
+        
+        return IDPROPERTY_IDSCHEME_MAP.containsKey( property ) ? 
+            IDPROPERTY_IDSCHEME_MAP.get( property ) : new IdScheme( property );
     }
 
     private IdScheme( IdentifiableProperty identifiableProperty )
@@ -152,7 +149,6 @@
         return IdentifiableProperty.ATTRIBUTE == identifiableProperty && !StringUtils.isEmpty( attribute );
     }
 
-
     public static boolean isAttribute( String str )
     {
         return !StringUtils.isEmpty( str ) && str.toUpperCase().startsWith( "ATTRIBUTE:" ) && str.length() == 21;