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/03/05 23:29:44 UTC

[4/6] incubator-tamaya git commit: TAMAYA-252: Clarified PropertyValue API.

TAMAYA-252: Clarified PropertyValue API.


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

Branch: refs/heads/master
Commit: 8c0081b9a8f6123f080bdd4c4b33dc0c62fde451
Parents: b59c1ae
Author: anatole <an...@apache.org>
Authored: Fri Mar 3 22:27:02 2017 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Mar 6 00:29:34 2017 +0100

----------------------------------------------------------------------
 .../org/apache/tamaya/spi/PropertyValue.java    | 60 +++++++++++++----
 .../apache/tamaya/spi/PropertyValueBuilder.java | 70 +++++++++++++++++---
 2 files changed, 108 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8c0081b9/code/api/src/main/java/org/apache/tamaya/spi/PropertyValue.java
----------------------------------------------------------------------
diff --git a/code/api/src/main/java/org/apache/tamaya/spi/PropertyValue.java b/code/api/src/main/java/org/apache/tamaya/spi/PropertyValue.java
index 30afeab..0332b85 100644
--- a/code/api/src/main/java/org/apache/tamaya/spi/PropertyValue.java
+++ b/code/api/src/main/java/org/apache/tamaya/spi/PropertyValue.java
@@ -18,6 +18,7 @@
  */
 package org.apache.tamaya.spi;
 
+import java.io.Serializable;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -29,18 +30,21 @@ import java.util.Objects;
  * it is possible to create a PropertyValue with a null value. Nevertheless in all cases the provider source (typically
  * the name of the PropertySource) must be set.
  */
-public final class PropertyValue {
+public final class PropertyValue implements Serializable{
+    private static final long serialVersionUID = 1L;
     /** The requested key. */
     private String key;
+    /** The value. */
+    private String value;
     /** Additional metadata provided by the provider. */
-    private Map<String,String> configEntries = new HashMap<>();
+    private Map<String,String> metaEntries = new HashMap<>();
 
     PropertyValue(PropertyValueBuilder builder){
         this.key = builder.key;
-        if(builder.contextData!=null) {
-            this.configEntries.putAll(builder.contextData);
+        this.value = Objects.requireNonNull(builder.value);
+        if(builder.metaEntries !=null) {
+            this.metaEntries.putAll(builder.metaEntries);
         }
-        this.configEntries.put(key, Objects.requireNonNull(builder.value));
     }
 
     /**
@@ -51,8 +55,8 @@ public final class PropertyValue {
      */
     private PropertyValue(String key, String value, String source){
         this.key = Objects.requireNonNull(key, "key is required.");
-        this.configEntries.put(key, value);
-        this.configEntries.put("_"+key+".source", Objects.requireNonNull(source, "source is required."));
+        this.value = Objects.requireNonNull(value);
+        this.metaEntries.put("_"+key+".source", Objects.requireNonNull(source, "source is required."));
     }
 
     /**
@@ -69,7 +73,7 @@ public final class PropertyValue {
      * {@link PropertySource#get(String)}.
      */
     public String getValue() {
-        return configEntries.get(key);
+        return this.value;
     }
 
     /**
@@ -77,8 +81,8 @@ public final class PropertyValue {
      * is also used for subsequent processing, like value filtering.
      * @return the property value entry map.
      */
-    public Map<String, String> getConfigEntries() {
-        return Collections.unmodifiableMap(configEntries);
+    public Map<String, String> getMetaEntries() {
+        return Collections.unmodifiableMap(metaEntries);
     }
 
     /**
@@ -111,7 +115,39 @@ public final class PropertyValue {
      * @param key the key, not null.
      * @return the value found, or null.
      */
-    public String get(String key) {
-        return this.configEntries.get(key);
+    public String getMetaEntry(String key) {
+        return this.metaEntries.get(key);
+    }
+
+    /**
+     * Creates a new builder instance based on this item.
+     * @return a new builder, never null.
+     */
+    public PropertyValueBuilder toBuilder() {
+        return new PropertyValueBuilder(this.getKey(), this.getValue(), this.metaEntries);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (!(o instanceof PropertyValue)) return false;
+        PropertyValue that = (PropertyValue) o;
+        return Objects.equals(getKey(), that.getKey()) &&
+                Objects.equals(getValue(), that.getValue()) &&
+                Objects.equals(getMetaEntries(), that.getMetaEntries());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(getKey(), getValue(), getMetaEntries());
+    }
+
+    @Override
+    public String toString() {
+        return "PropertyValue{" +
+                "key='" + key + '\'' +
+                ", value='" + value + '\'' +
+                ", metaEntries=" + metaEntries +
+                '}';
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8c0081b9/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueBuilder.java
----------------------------------------------------------------------
diff --git a/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueBuilder.java b/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueBuilder.java
index 7eaeb94..57a40be 100644
--- a/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueBuilder.java
+++ b/code/api/src/main/java/org/apache/tamaya/spi/PropertyValueBuilder.java
@@ -18,6 +18,7 @@
  */
 package org.apache.tamaya.spi;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -31,7 +32,7 @@ public class PropertyValueBuilder {
     /** The property value. */
     String value;
     /** additional metadata entries (optional). */
-    Map<String,String> contextData = new HashMap<>();
+    Map<String,String> metaEntries = new HashMap<>();
 
     /**
      * Create a new builder instance, for a given set of parameters.
@@ -43,12 +44,25 @@ public class PropertyValueBuilder {
     public PropertyValueBuilder(String key, String value, String source) {
         this.key = Objects.requireNonNull(key);
         this.value = Objects.requireNonNull(value);
-        this.contextData.put("_" + key + ".source", Objects.requireNonNull(source));
+        this.metaEntries.put("source", Objects.requireNonNull(source));
+    }
+
+    /**
+     * Creates a new builder from data from a {@link PropertyValue}.
+     * @param key to access a property value.
+     * @param value the value, not null. If a value is null {@link PropertySource#get(String)} should return
+     * {@code null}.
+     * @param metaEntries the context data, not null.
+     */
+    PropertyValueBuilder(String key, String value, Map<String,String> metaEntries) {
+        this.key = Objects.requireNonNull(key);
+        this.value = Objects.requireNonNull(value);
+        this.metaEntries.putAll(metaEntries);
     }
 
     /**
      * Replaces/sets the context data.
-     * @param contextData the context data to be applied, not null. Note that all keys should only identify the context
+     * @param metaEntries the context data to be applied, not null. Note that all keys should only identify the context
      *                    data item. the builder does create a corresponding metadata entry, e.g.
      *                    <pre>
      *                    provider=myProviderName
@@ -65,11 +79,9 @@ public class PropertyValueBuilder {
      *                    </pre>
      * @return the builder for chaining.
      */
-    public PropertyValueBuilder setContextData(Map<String, String> contextData) {
-        this.contextData.clear();
-        for(Map.Entry<String,String> en:contextData.entrySet()) {
-            this.contextData.put("_"+this.key+'.'+en.getKey(), en.getValue());
-        }
+    public PropertyValueBuilder setMetaEntries(Map<String, String> metaEntries) {
+        this.metaEntries.clear();
+        this.metaEntries.putAll(metaEntries);
         return this;
     }
 
@@ -80,7 +92,44 @@ public class PropertyValueBuilder {
      * @return the builder for chaining.
      */
     public PropertyValueBuilder addContextData(String key, Object value) {
-        this.contextData.put("_"+this.key+'.'+key, String.valueOf(Objects.requireNonNull(value, "Meta value is null.")));
+        this.metaEntries.put(key, String.valueOf(Objects.requireNonNull(value, "Meta value is null.")));
+        return this;
+    }
+
+    /**
+     * Get the value's context data.
+     * @return the context data.
+     */
+    public Map<String,String> getMetaEntries(){
+        return Collections.unmodifiableMap(this.metaEntries);
+    }
+
+    /**
+     * Changes the entry's key, mapping also corresponding context entries.
+     * @param key the new key, not null.
+     * @return the builder for chaining.
+     */
+    public PropertyValueBuilder mapKey(String key) {
+        Map<String,String> newContext = new HashMap<>();
+        for(Map.Entry<String,String> en:this.metaEntries.entrySet()){
+            if(en.getKey().startsWith("_"+this.key)){
+                newContext.put("_"+key+'.'+ en.getKey().substring(this.key.length()+1), en.getValue());
+            }else{
+                newContext.put(en.getKey(), en.getValue());
+            }
+        }
+        this.metaEntries = newContext;
+        this.key = key;
+        return this;
+    }
+
+    /**
+     * Sets a new value.
+     * @param value the new value.
+     * @return the builder for chaining.
+     */
+    public PropertyValueBuilder setValue(String value) {
+        this.value = value;
         return this;
     }
 
@@ -97,7 +146,8 @@ public class PropertyValueBuilder {
         return "PropertyValueBuilder{" +
                 "key='" + key + '\'' +
                 "value='" + value + '\'' +
-                ", contextData=" + contextData +
+                ", metaEntries=" + metaEntries +
                 '}';
     }
+
 }