tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [1/2] incubator-tamaya git commit: Refactored internal structure of core a bit.
Date Sat, 03 Oct 2015 07:33:42 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 137559501 -> bc7f32d66


Refactored internal structure of core a bit.


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

Branch: refs/heads/master
Commit: 60d1b74ef7d4d136d807919f9996c51fc430af7f
Parents: 1375595
Author: anatole <anatole@apache.org>
Authored: Sat Oct 3 09:32:24 2015 +0200
Committer: anatole <anatole@apache.org>
Committed: Sat Oct 3 09:33:27 2015 +0200

----------------------------------------------------------------------
 .../core/internal/DefaultConfiguration.java     | 152 ++++---------------
 .../internal/PriorityServiceComparator.java     |  56 +++++++
 .../core/internal/PropertyFilterComparator.java |  60 ++++++++
 .../tamaya/core/internal/PropertyFiltering.java | 131 ++++++++++++++++
 .../core/internal/PropertySourceComparator.java |  54 +++++++
 5 files changed, 330 insertions(+), 123 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/60d1b74e/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
b/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
index 8dc3eb1..76d9ac1 100644
--- a/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
+++ b/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
@@ -22,9 +22,9 @@ import org.apache.tamaya.ConfigException;
 import org.apache.tamaya.ConfigOperator;
 import org.apache.tamaya.ConfigQuery;
 import org.apache.tamaya.Configuration;
-import org.apache.tamaya.spi.PropertyConverter;
 import org.apache.tamaya.TypeLiteral;
 import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.PropertyConverter;
 import org.apache.tamaya.spi.PropertyFilter;
 import org.apache.tamaya.spi.PropertySource;
 import org.apache.tamaya.spi.PropertyValueCombinationPolicy;
@@ -35,13 +35,12 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
  * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext}
to evaluate the
- * chain of {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter}
+ * chain of {@link PropertySource} and {@link PropertyFilter}
  * instance to evaluate the current Configuration.
  */
 public class DefaultConfiguration implements Configuration {
@@ -49,16 +48,13 @@ public class DefaultConfiguration implements Configuration {
      * The logger.
      */
     private static final Logger LOG = Logger.getLogger(DefaultConfiguration.class.getName());
-    /**
-     * The maximal number of filter cycles performed before aborting.
-     */
-    private static final int MAX_FILTER_LOOPS = 10;
 
     /**
-     * The current {@link org.apache.tamaya.spi.ConfigurationContext} of the current instance.
+     * The current {@link ConfigurationContext} of the current instance.
      */
     private final ConfigurationContext configurationContext;
 
+
     /**
      * Constructor.
      * @param configurationContext The configuration Context to be used.
@@ -67,28 +63,23 @@ public class DefaultConfiguration implements Configuration {
         this.configurationContext = Objects.requireNonNull(configurationContext);
     }
 
-    /**
-     * This method evaluates the given configuration key. Hereby if goes down the chain or
PropertySource instances
-     * provided by the current {@link org.apache.tamaya.spi.ConfigurationContext}. The first
non-null-value returned
-     * is taken as an intermediate value. Finally the value is filtered through the
-     * {@link org.apache.tamaya.spi.PropertyFilter} instances installed, before it is returned
as the final result of
-     * this method.
-     *
-     * @param key the property's key, not null.
-     * @return the optional configuration value, never null.
-     */
-    @Override
+
     public String get(String key) {
+        return PropertyFiltering.applyFilter(key, evaluteRawValue(key), configurationContext);
+    }
+
+    protected String evaluteRawValue(String key) {
         List<PropertySource> propertySources = configurationContext.getPropertySources();
         String unfilteredValue = null;
         PropertyValueCombinationPolicy combinationPolicy = this.configurationContext
                 .getPropertyValueCombinationPolicy();
         for (PropertySource propertySource : propertySources) {
-                unfilteredValue = combinationPolicy.collect(unfilteredValue, key, propertySource);
+            unfilteredValue = combinationPolicy.collect(unfilteredValue, key, propertySource);
         }
-        return applyFilter(key, unfilteredValue);
+        return unfilteredValue;
     }
 
+
     @Override
     public String getOrDefault(String key, String defaultValue) {
         String val = get(key);
@@ -108,56 +99,17 @@ public class DefaultConfiguration implements Configuration {
     }
 
     /**
-     * Apply filters to a single property value.
-     *
-     * @param key             the key, used for logging, not null.
-     * @param unfilteredValue the unfiltered property value.
-     * @return the filtered value, or null.
-     */
-    private String applyFilter(String key, String unfilteredValue) {
-        // Apply filters to values, prevent values filtered to null!
-        for (int i = 0; i < MAX_FILTER_LOOPS; i++) {
-            boolean changed = false;
-            // Apply filters to values, prevent values filtered to null!
-            for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
-                String newValue = filter.filterProperty(key, unfilteredValue);
-                if (newValue != null && !newValue.equals(unfilteredValue)) {
-                    changed = true;
-                    if (LOG.isLoggable(Level.FINEST)) {
-                        LOG.finest("Filter - " + key + ": " + unfilteredValue + " -> "
+ newValue + " by " + filter);
-                    }
-                } else if (unfilteredValue != null && !unfilteredValue.equals(newValue))
{
-                    changed = true;
-                    if (LOG.isLoggable(Level.FINEST)) {
-                        LOG.finest("Filter - " + key + ": " + unfilteredValue + " -> "
+ newValue + " by " + filter);
-                    }
-                }
-                unfilteredValue = newValue;
-            }
-            if (!changed) {
-                LOG.finest("Finishing filter loop, no changes detected.");
-                break;
-            } else {
-                if (i == (MAX_FILTER_LOOPS - 1)) {
-                    if (LOG.isLoggable(Level.WARNING)) {
-                        LOG.warning("Maximal filter loop count reached, aborting filter evaluation
after cycles: " + i);
-                    }
-                } else {
-                    LOG.finest("Repeating filter loop, changes detected.");
-                }
-            }
-        }
-        return unfilteredValue;
-    }
-
-    /**
-     * Get the current properties, composed by the loaded {@link org.apache.tamaya.spi.PropertySource}
and filtered
-     * by registered {@link org.apache.tamaya.spi.PropertyFilter}.
+     * Get the current properties, composed by the loaded {@link PropertySource} and filtered
+     * by registered {@link PropertyFilter}.
      *
      * @return the final properties.
      */
     @Override
     public Map<String, String> getProperties() {
+        return PropertyFiltering.applyFilters(evaluateUnfilteredMap(), configurationContext);
+    }
+
+    protected Map<String, String> evaluateUnfilteredMap() {
         List<PropertySource> propertySources = new ArrayList<>(configurationContext.getPropertySources());
         Collections.reverse(propertySources);
         Map<String, String> result = new HashMap<>();
@@ -173,63 +125,13 @@ public class DefaultConfiguration implements Configuration {
                 LOG.log(Level.SEVERE, "Error adding properties from PropertySource: " + propertySource
+ ", ignoring PropertySource.", e);
             }
         }
-        return applyFilters(result);
-    }
-
-    /**
-     * Filter a full configuration property map.
-     *
-     * @param inputMap the unfiltered map
-     * @return the filtered map.
-     */
-    private Map<String, String> applyFilters(Map<String, String> inputMap) {
-        Map<String, String> resultMap = new HashMap<>(inputMap);
-        // Apply filters to values, prevent values filtered to null!
-        for (int i = 0; i < MAX_FILTER_LOOPS; i++) {
-            AtomicInteger changes = new AtomicInteger();
-            for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
-                for (Map.Entry<String, String> entry : inputMap.entrySet()) {
-                    final String k = entry.getKey();
-                    final String v = entry.getValue();
-
-                    String newValue = filter.filterProperty(k, v);
-                    if (newValue != null && !newValue.equals(v)) {
-                        changes.incrementAndGet();
-                        LOG.finest("Filter - " + k + ": " + v + " -> " + newValue + "
by " + filter);
-                    } else if (v != null && !v.equals(newValue)) {
-                        changes.incrementAndGet();
-                        LOG.finest("Filter - " + k + ": " + v + " -> " + newValue + "
by " + filter);
-                    }
-                    // Remove null values
-                    if (null != newValue) {
-                        resultMap.put(k, newValue);
-                    }
-                    else{
-                        resultMap.remove(k);
-                    }
-                }
-            }
-            if (changes.get() == 0) {
-                LOG.finest("Finishing filter loop, no changes detected.");
-                break;
-            } else {
-                if (i == (MAX_FILTER_LOOPS - 1)) {
-                    if (LOG.isLoggable(Level.WARNING)) {
-                        LOG.warning("Maximal filter loop count reached, aborting filter evaluation
after cycles: " + i);
-                    }
-                } else {
-                    LOG.finest("Repeating filter loop, changes detected: " + changes.get());
-                }
-                changes.set(0);
-            }
-        }
-        return resultMap;
+        return result;
     }
 
     /**
      * Accesses the current String value for the given key and tries to convert it
-     * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided by the
current
-     * {@link org.apache.tamaya.spi.ConfigurationContext}.
+     * using the {@link PropertyConverter} instances provided by the current
+     * {@link ConfigurationContext}.
      *
      * @param key  the property's absolute, or relative path, e.g. @code
      *             a/b/c/d.myProperty}.
@@ -244,8 +146,8 @@ public class DefaultConfiguration implements Configuration {
 
     /**
      * Accesses the current String value for the given key and tries to convert it
-     * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided by the
current
-     * {@link org.apache.tamaya.spi.ConfigurationContext}.
+     * using the {@link PropertyConverter} instances provided by the current
+     * {@link ConfigurationContext}.
      *
      * @param key  the property's absolute, or relative path, e.g. @code
      *             a/b/c/d.myProperty}.
@@ -255,7 +157,10 @@ public class DefaultConfiguration implements Configuration {
      */
     @Override
     public <T> T get(String key, TypeLiteral<T> type) {
-        String value = get(key);
+        return convertValue(key, get(key), type);
+    }
+
+    protected <T> T convertValue(String key, String value, TypeLiteral<T> type)
{
         if (value != null) {
             List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type);
             for (PropertyConverter<T> converter : converters) {
@@ -270,7 +175,6 @@ public class DefaultConfiguration implements Configuration {
             }
             throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName()
+ ": " + key);
         }
-
         return null;
     }
 
@@ -283,10 +187,12 @@ public class DefaultConfiguration implements Configuration {
         return val;
     }
 
+    @Override
     public Configuration with(ConfigOperator operator) {
         return operator.operate(this);
     }
 
+    @Override
     public <T> T query(ConfigQuery<T> query) {
         return query.query(this);
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/60d1b74e/core/src/main/java/org/apache/tamaya/core/internal/PriorityServiceComparator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/PriorityServiceComparator.java
b/core/src/main/java/org/apache/tamaya/core/internal/PriorityServiceComparator.java
new file mode 100644
index 0000000..b667900
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/PriorityServiceComparator.java
@@ -0,0 +1,56 @@
+/*
+ * 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.apache.tamaya.core.internal;
+
+import javax.annotation.Priority;
+import java.util.Comparator;
+
+/**
+ * Comparator implementation for odering services loaded based on their increasing priority
values.
+ */
+public class PriorityServiceComparator implements Comparator<Object> {
+
+    @Override
+    public int compare(Object o1, Object o2) {
+        int prio = getPriority(o1) - getPriority(o2);
+        if (prio < 0) {
+            return 1;
+        } else if (prio > 0) {
+            return -1;
+        } else {
+            return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
+        }
+    }
+
+    /**
+     * Checks the given instance for a @Priority annotation. If present the annotation's
value s evaluated. If no such
+     * annotation is present, a default priority is returned (1);
+     *
+     * @param o the instance, not null.
+     * @return a priority, by default 1.
+     */
+    public static int getPriority(Object o) {
+        int prio = 1;
+        Priority priority = o.getClass().getAnnotation(Priority.class);
+        if (priority != null) {
+            prio = priority.value();
+        }
+        return prio;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/60d1b74e/core/src/main/java/org/apache/tamaya/core/internal/PropertyFilterComparator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/PropertyFilterComparator.java
b/core/src/main/java/org/apache/tamaya/core/internal/PropertyFilterComparator.java
new file mode 100644
index 0000000..abf22be
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/PropertyFilterComparator.java
@@ -0,0 +1,60 @@
+/*
+ * 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.apache.tamaya.core.internal;
+
+import org.apache.tamaya.spi.PropertyFilter;
+
+import javax.annotation.Priority;
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * Comparator for PropertyFilters based on their priority annotations.
+ */
+public class PropertyFilterComparator implements Comparator<PropertyFilter>, Serializable
{
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Compare 2 filters for ordering the filter chain.
+     *
+     * @param filter1 the first filter
+     * @param filter2 the second filter
+     * @return the comparison result
+     */
+    private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
+        Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
+        Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
+        int ord1 = prio1 != null ? prio1.value() : 0;
+        int ord2 = prio2 != null ? prio2.value() : 0;
+
+        if (ord1 < ord2) {
+            return -1;
+        } else if (ord1 > ord2) {
+            return 1;
+        } else {
+            return filter1.getClass().getName().compareTo(filter2.getClass().getName());
+        }
+    }
+
+    @Override
+    public int compare(PropertyFilter filter1, PropertyFilter filter2) {
+        return comparePropertyFilters(filter1, filter2);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/60d1b74e/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java b/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java
new file mode 100644
index 0000000..9f84e7c
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java
@@ -0,0 +1,131 @@
+/*
+ * 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.apache.tamaya.core.internal;
+
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.PropertyFilter;
+import org.apache.tamaya.spi.PropertySource;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext}
to evaluate the
+ * chain of {@link PropertySource} and {@link PropertyFilter}
+ * instance to evaluate the current Configuration.
+ */
+public final class PropertyFiltering{
+    /**
+     * The logger.
+     */
+    private static final Logger LOG = Logger.getLogger(PropertyFiltering.class.getName());
+    /**
+     * The maximal number of filter cycles performed before aborting.
+     */
+    private static final int MAX_FILTER_LOOPS = 10;
+
+    /**
+     * Private singleton constructor.
+     */
+    private PropertyFiltering(){}
+
+    public static String applyFilter(String key, String unfilteredValue, ConfigurationContext
configurationContext) {
+        // Apply filters to values, prevent values filtered to null!
+        for (int i = 0; i < MAX_FILTER_LOOPS; i++) {
+            boolean changed = false;
+            // Apply filters to values, prevent values filtered to null!
+            for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
+                String newValue = filter.filterProperty(key, unfilteredValue);
+                if (newValue != null && !newValue.equals(unfilteredValue)) {
+                    changed = true;
+                    if (LOG.isLoggable(Level.FINEST)) {
+                        LOG.finest("Filter - " + key + ": " + unfilteredValue + " -> "
+ newValue + " by " + filter);
+                    }
+                } else if (unfilteredValue != null && !unfilteredValue.equals(newValue))
{
+                    changed = true;
+                    if (LOG.isLoggable(Level.FINEST)) {
+                        LOG.finest("Filter - " + key + ": " + unfilteredValue + " -> "
+ newValue + " by " + filter);
+                    }
+                }
+                unfilteredValue = newValue;
+            }
+            if (!changed) {
+                LOG.finest("Finishing filter loop, no changes detected.");
+                break;
+            } else {
+                if (i == (MAX_FILTER_LOOPS - 1)) {
+                    if (LOG.isLoggable(Level.WARNING)) {
+                        LOG.warning("Maximal filter loop count reached, aborting filter evaluation
after cycles: " + i);
+                    }
+                } else {
+                    LOG.finest("Repeating filter loop, changes detected.");
+                }
+            }
+        }
+        return unfilteredValue;
+    }
+
+    public static Map<String, String> applyFilters(Map<String, String> inputMap,
ConfigurationContext configurationContext) {
+        Map<String, String> resultMap = new HashMap<>(inputMap);
+        // Apply filters to values, prevent values filtered to null!
+        for (int i = 0; i < MAX_FILTER_LOOPS; i++) {
+            AtomicInteger changes = new AtomicInteger();
+            for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
+                for (Map.Entry<String, String> entry : inputMap.entrySet()) {
+                    final String k = entry.getKey();
+                    final String v = entry.getValue();
+
+                    String newValue = filter.filterProperty(k, v);
+                    if (newValue != null && !newValue.equals(v)) {
+                        changes.incrementAndGet();
+                        LOG.finest("Filter - " + k + ": " + v + " -> " + newValue + "
by " + filter);
+                    } else if (v != null && !v.equals(newValue)) {
+                        changes.incrementAndGet();
+                        LOG.finest("Filter - " + k + ": " + v + " -> " + newValue + "
by " + filter);
+                    }
+                    // Remove null values
+                    if (null != newValue) {
+                        resultMap.put(k, newValue);
+                    }
+                    else{
+                        resultMap.remove(k);
+                    }
+                }
+            }
+            if (changes.get() == 0) {
+                LOG.finest("Finishing filter loop, no changes detected.");
+                break;
+            } else {
+                if (i == (MAX_FILTER_LOOPS - 1)) {
+                    if (LOG.isLoggable(Level.WARNING)) {
+                        LOG.warning("Maximal filter loop count reached, aborting filter evaluation
after cycles: " + i);
+                    }
+                } else {
+                    LOG.finest("Repeating filter loop, changes detected: " + changes.get());
+                }
+                changes.set(0);
+            }
+        }
+        return resultMap;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/60d1b74e/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
b/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
new file mode 100644
index 0000000..24ee1f2
--- /dev/null
+++ b/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
@@ -0,0 +1,54 @@
+/*
+ * 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.apache.tamaya.core.internal;
+
+import org.apache.tamaya.spi.PropertySource;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * Comparator for ordering of PropertySources based on their ordinal method and class name.
+ */
+public class PropertySourceComparator implements Comparator<PropertySource>, Serializable
{
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Order property source reversely, the most important come first.
+     *
+     * @param source1 the first PropertySource
+     * @param source2 the second PropertySource
+     * @return the comparison result.
+     */
+    private int comparePropertySources(PropertySource source1, PropertySource source2) {
+        if (source1.getOrdinal() < source2.getOrdinal()) {
+            return -1;
+        } else if (source1.getOrdinal() > source2.getOrdinal()) {
+            return 1;
+        } else {
+            return source1.getClass().getName().compareTo(source2.getClass().getName());
+        }
+    }
+
+    @Override
+    public int compare(PropertySource source1, PropertySource source2) {
+        return comparePropertySources(source1, source2);
+    }
+}


Mime
View raw message