From 3200b2ec588dfa5c50f1ec6192ff93ab9187f82d Mon Sep 17 00:00:00 2001 From: Christopher Douglas Date: Mon, 20 Jul 2009 22:58:21 +0000 Subject: [PATCH] HADOOP-6161. Add get/setEnum methods to Configuration. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@796052 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 2 ++ .../org/apache/hadoop/conf/Configuration.java | 24 +++++++++++++++++++ .../apache/hadoop/conf/TestConfiguration.java | 17 +++++++++++++ 3 files changed, 43 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 67d4a085a67..e841f83a1d6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -480,6 +480,8 @@ Trunk (unreleased changes) HADOOP-6146. Upgrade to JetS3t version 0.7.1. (tomwhite) + HADOOP-6161. Add get/setEnum methods to Configuration. (cdouglas) + OPTIMIZATIONS HADOOP-5595. NameNode does not need to run a replicator to choose a diff --git a/src/java/org/apache/hadoop/conf/Configuration.java b/src/java/org/apache/hadoop/conf/Configuration.java index 16958186ee1..fedf5e62f33 100644 --- a/src/java/org/apache/hadoop/conf/Configuration.java +++ b/src/java/org/apache/hadoop/conf/Configuration.java @@ -595,6 +595,30 @@ public void setBooleanIfUnset(String name, boolean value) { setIfUnset(name, Boolean.toString(value)); } + /** + * Set the value of the name property to the given type. This + * is equivalent to set(<name>, value.toString()). + * @param name property name + * @param value new value + */ + public > void setEnum(String name, T value) { + set(name, value.toString()); + } + + /** + * Return value matching this enumerated type. + * @param name Property name + * @param defaultValue Value returned if no mapping exists + * @throws IllegalArgumentException If mapping is illegal for the type + * provided + */ + public > T getEnum(String name, T defaultValue) { + final String val = get(name); + return null == val + ? defaultValue + : Enum.valueOf(defaultValue.getDeclaringClass(), val); + } + /** * A class that represents a set of positive integer ranges. It parses * strings of the form: "2-3,5,7-" where ranges are separated by comma and diff --git a/src/test/core/org/apache/hadoop/conf/TestConfiguration.java b/src/test/core/org/apache/hadoop/conf/TestConfiguration.java index e509fd34641..03683880409 100644 --- a/src/test/core/org/apache/hadoop/conf/TestConfiguration.java +++ b/src/test/core/org/apache/hadoop/conf/TestConfiguration.java @@ -327,6 +327,23 @@ public void testIntegerValues() throws IOException{ assertEquals(-20, conf.getLong("test.int3", 0)); } + enum Dingo { FOO, BAR }; + enum Yak { RAB, FOO }; + public void testEnum() throws IOException { + Configuration conf = new Configuration(); + conf.setEnum("test.enum", Dingo.FOO); + assertSame(Dingo.FOO, conf.getEnum("test.enum", Dingo.BAR)); + assertSame(Yak.FOO, conf.getEnum("test.enum", Yak.RAB)); + boolean fail = false; + try { + conf.setEnum("test.enum", Dingo.BAR); + Yak y = conf.getEnum("test.enum", Yak.FOO); + } catch (IllegalArgumentException e) { + fail = true; + } + assertTrue(fail); + } + public void testReload() throws IOException { out=new BufferedWriter(new FileWriter(CONFIG)); startConfig();