You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/08/08 22:23:32 UTC

[4/4] incubator-tamaya-extensions git commit: TAMAYA-260: Added support for config_ordinal as alternative ordinal key.

TAMAYA-260: Added support for config_ordinal as alternative ordinal key.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/72c37c5c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/72c37c5c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/72c37c5c

Branch: refs/heads/java8
Commit: 72c37c5c1e5d3504c87e796b9af923ae476d4436
Parents: 1d1cf72
Author: anatole <an...@apache.org>
Authored: Wed Aug 9 00:21:48 2017 +0200
Committer: anatole <an...@apache.org>
Committed: Wed Aug 9 00:21:48 2017 +0200

----------------------------------------------------------------------
 .../spisupport/PropertySourceComparator.java    | 59 +++++++++++---------
 1 file changed, 32 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/72c37c5c/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java
----------------------------------------------------------------------
diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java
index f4c37ac..6799e4e 100644
--- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java
+++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java
@@ -39,6 +39,8 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser
 
     private static final PropertySourceComparator INSTANCE = new PropertySourceComparator();
 
+    private String alternativeOrdinalKey;
+
     private PropertySourceComparator(){}
 
     /**
@@ -58,9 +60,9 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser
      * @return the comparison result.
      */
     private int comparePropertySources(PropertySource source1, PropertySource source2) {
-        if (getOrdinal(source1) < getOrdinal(source2)) {
+        if (getOrdinal(source1, alternativeOrdinalKey) < getOrdinal(source2, alternativeOrdinalKey)) {
             return -1;
-        } else if (getOrdinal(source1) > getOrdinal(source2)) {
+        } else if (getOrdinal(source1, alternativeOrdinalKey) > getOrdinal(source2, alternativeOrdinalKey)) {
             return 1;
         } else {
             return source1.getClass().getName().compareTo(source2.getClass().getName());
@@ -68,35 +70,38 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser
     }
 
     public static int getOrdinal(PropertySource propertySource) {
-//        PropertyValue ordinalValue = propertySource.get(PropertySource.TAMAYA_ORDINAL);
-//        if(ordinalValue!=null){
-//            try{
-//                return Integer.parseInt(ordinalValue.getProperty().trim());
-//            }catch(Exception e){
-//                LOG.finest("Failed to parse ordinal from " + PropertySource.TAMAYA_ORDINAL +
-//                        " in " + propertySource.getName()+": "+ordinalValue.getProperty());
-//            }
-//        }
-//        try {
-//            Method method = propertySource.getClass().getMethod("getOrdinal");
-//            if(int.class.equals(method.getReturnType())){
-//                try {
-//                    return (int)method.invoke(propertySource);
-//                } catch (Exception e) {
-//                    LOG.log(Level.FINEST, "Error calling int getOrdinal() on " + propertySource.getName(), e);
-//                }
-//            }
-//        } catch (NoSuchMethodException e) {
-//            LOG.finest("No int getOrdinal() method found in " + propertySource.getName());
-//        }
-//        Priority prio = propertySource.getClass().getAnnotation(Priority.class);
-//        if(prio!=null){
-//            return prio.value();
-//        }
+        return getOrdinal(propertySource, null);
+    }
+
+    public static int getOrdinal(PropertySource propertySource, String alternativeOrdinalKey) {
+        if(alternativeOrdinalKey!=null) {
+            PropertyValue ordinalValue = propertySource.get(alternativeOrdinalKey);
+            if (ordinalValue != null) {
+                try {
+                    return Integer.parseInt(ordinalValue.getValue().trim());
+                } catch (Exception e) {
+                    LOG.finest("Failed to parse ordinal from " + alternativeOrdinalKey +
+                            " in " + propertySource.getName() + ": " + ordinalValue.getValue());
+                }
+            }
+        }
         return propertySource.getOrdinal();
     }
+
+    /**
+     * Overrides/adds the key to evaluate/override a property sources ordinal.
+     * @param ordinalKey sets the alternative ordinal key, if null default
+     *                   behaviour will be active.
+     * @return the instance for chaining.
+     */
+    public PropertySourceComparator setOrdinalKey(String ordinalKey) {
+        this.alternativeOrdinalKey = ordinalKey;
+        return this;
+    }
+
     @Override
     public int compare(PropertySource source1, PropertySource source2) {
         return comparePropertySources(source1, source2);
     }
+
 }