Repository: flume
Updated Branches:
  refs/heads/trunk 671fc3f1b -> d3a04c77d


FLUME-2071 Flume Context doesn't support float or double configuration values.

Adding support for getFloat() and getDouble() on context

Reviewers: Mike Percy, Ferenc Szabo

(Hans Uhlig via Ferenc Szabo)


Project: http://git-wip-us.apache.org/repos/asf/flume/repo
Commit: http://git-wip-us.apache.org/repos/asf/flume/commit/d3a04c77
Tree: http://git-wip-us.apache.org/repos/asf/flume/tree/d3a04c77
Diff: http://git-wip-us.apache.org/repos/asf/flume/diff/d3a04c77

Branch: refs/heads/trunk
Commit: d3a04c77da026a0835f43d2e2bee18f8834534b3
Parents: 671fc3f
Author: Ferenc Szabo <[email protected]>
Authored: Wed Nov 14 14:52:40 2018 +0100
Committer: Ferenc Szabo <[email protected]>
Committed: Wed Nov 14 14:52:40 2018 +0100

----------------------------------------------------------------------
 .../src/main/java/org/apache/flume/Context.java | 61 ++++++++++++++++++++
 .../test/java/org/apache/flume/TestContext.java | 11 ++++
 2 files changed, 72 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flume/blob/d3a04c77/flume-ng-configuration/src/main/java/org/apache/flume/Context.java
----------------------------------------------------------------------
diff --git a/flume-ng-configuration/src/main/java/org/apache/flume/Context.java 
b/flume-ng-configuration/src/main/java/org/apache/flume/Context.java
index 954baae..90d7816 100644
--- a/flume-ng-configuration/src/main/java/org/apache/flume/Context.java
+++ b/flume-ng-configuration/src/main/java/org/apache/flume/Context.java
@@ -228,6 +228,67 @@ public class Context {
   public String getString(String key) {
     return get(key);
   }
+
+
+  /**
+   * Gets value mapped to key, returning defaultValue if unmapped.
+   * @param key to be found
+   * @param defaultValue returned if key is unmapped
+   * @return value associated with key
+   */
+  public Float getFloat(String key, Float defaultValue) {
+    String value = get(key);
+    if (value != null) {
+      return Float.parseFloat(value.trim());
+    }
+    return defaultValue;
+  }
+  /**
+   * Gets value mapped to key, returning null if unmapped.
+   * <p>
+   * Note that this method returns an object as opposed to a
+   * primitive. The configuration key requested may not be mapped
+   * to a value and by returning the primitive object wrapper we can
+   * return null. If the key does not exist the return value of
+   * this method is assigned directly to a primitive, a
+   * {@link NullPointerException} will be thrown.
+   * </p>
+   * @param key to be found
+   * @return value associated with key or null if unmapped
+   */
+  public Float getFloat(String key) {
+    return getFloat(key, null);
+  }
+  /**
+   * Gets value mapped to key, returning defaultValue if unmapped.
+   * @param key to be found
+   * @param defaultValue returned if key is unmapped
+   * @return value associated with key
+   */
+  public Double getDouble(String key, Double defaultValue) {
+    String value = get(key);
+    if (value != null) {
+      return Double.parseDouble(value.trim());
+    }
+    return defaultValue;
+  }
+  /**
+   * Gets value mapped to key, returning null if unmapped.
+   * <p>
+   * Note that this method returns an object as opposed to a
+   * primitive. The configuration key requested may not be mapped
+   * to a value and by returning the primitive object wrapper we can
+   * return null. If the key does not exist the return value of
+   * this method is assigned directly to a primitive, a
+   * {@link NullPointerException} will be thrown.
+   * </p>
+   * @param key to be found
+   * @return value associated with key or null if unmapped
+   */
+  public Double getDouble(String key) {
+    return getDouble(key, null);
+  }
+
   private String get(String key, String defaultValue) {
     String result = parameters.get(key);
     if (result != null) {

http://git-wip-us.apache.org/repos/asf/flume/blob/d3a04c77/flume-ng-core/src/test/java/org/apache/flume/TestContext.java
----------------------------------------------------------------------
diff --git a/flume-ng-core/src/test/java/org/apache/flume/TestContext.java 
b/flume-ng-core/src/test/java/org/apache/flume/TestContext.java
index 51c350f..ec65c97 100644
--- a/flume-ng-core/src/test/java/org/apache/flume/TestContext.java
+++ b/flume-ng-core/src/test/java/org/apache/flume/TestContext.java
@@ -63,6 +63,17 @@ public class TestContext {
     assertNull(context.getLong("test"));
     assertEquals(new Long(Long.MAX_VALUE), context.getLong("test", 
Long.MAX_VALUE));
 
+    context.put("test", "0.1");
+    assertEquals(new Float(0.1), context.getFloat("test"));
+    context.clear();
+    assertNull(context.getFloat("test"));
+    assertEquals(new Float(1.1), context.getFloat("test",1.1F));
+
+    context.put("test", "0.1");
+    assertEquals(new Double(0.1), context.getDouble("test"));
+    context.clear();
+    assertNull(context.getDouble("test"));
+    assertEquals(new Double(1.1), context.getDouble("test",1.1));
   }
 
   @Test

Reply via email to