Skip to content

Commit

Permalink
[utils] Missing getters for ParameterTool
Browse files Browse the repository at this point in the history
  • Loading branch information
mbalassi committed Sep 24, 2015
1 parent 135db3a commit 6b26e23
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,84 @@ public double getDouble(String key, double defaultValue) {
}
}

// -------------- BOOLEAN

/**
* Returns the Boolean value for the given key.
* The method fails if the key does not exist.
*/
public boolean getBoolean(String key) {
addToDefaults(key, null);
String value = getRequired(key);
return Boolean.valueOf(value);
}

/**
* Returns the Boolean value for the given key. If the key does not exists it will return the default value given.
* The method returns whether the string of the value is "true" ignoring cases.
*/
public boolean getBoolean(String key, boolean defaultValue) {
addToDefaults(key, Boolean.toString(defaultValue));
String value = get(key);
if(value == null) {
return defaultValue;
} else {
return Boolean.valueOf(value);
}
}

// -------------- SHORT

/**
* Returns the Short value for the given key.
* The method fails if the key does not exist.
*/
public short getShort(String key) {
addToDefaults(key, null);
String value = getRequired(key);
return Short.valueOf(value);
}

/**
* Returns the Short value for the given key. If the key does not exists it will return the default value given.
* The method fails if the value is not a Short.
*/
public short getShort(String key, short defaultValue) {
addToDefaults(key, Short.toString(defaultValue));
String value = get(key);
if(value == null) {
return defaultValue;
} else {
return Short.valueOf(value);
}
}

// -------------- BYTE

/**
* Returns the Byte value for the given key.
* The method fails if the key does not exist.
*/
public byte getByte(String key) {
addToDefaults(key, null);
String value = getRequired(key);
return Byte.valueOf(value);
}

/**
* Returns the Byte value for the given key. If the key does not exists it will return the default value given.
* The method fails if the value is not a Byte.
*/
public byte getByte(String key, byte defaultValue) {
addToDefaults(key, Byte.toString(defaultValue));
String value = get(key);
if(value == null) {
return defaultValue;
} else {
return Byte.valueOf(value);
}
}

// --------------- Internals

protected void addToDefaults(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ public void testEmptyValShort() {

@Test
public void testFromCliArgs() {
ParameterTool parameter = ParameterTool.fromArgs(new String[]{"--input", "myInput", "-expectedCount", "15", "--withoutValues", "--negativeFloat", "-0.58"});
Assert.assertEquals(4, parameter.getNumberOfParameters());
ParameterTool parameter = ParameterTool.fromArgs(new String[]{"--input", "myInput", "-expectedCount", "15", "--withoutValues",
"--negativeFloat", "-0.58", "-isWorking", "true", "--maxByte", "127", "-negativeShort", "-1024"});
Assert.assertEquals(7, parameter.getNumberOfParameters());
validate(parameter);
Assert.assertTrue(parameter.has("withoutValues"));
Assert.assertEquals(-0.58, parameter.getFloat("negativeFloat"), 0.1);
Assert.assertTrue(parameter.getBoolean("isWorking"));
Assert.assertEquals(127, parameter.getByte("maxByte"));
Assert.assertEquals(-1024, parameter.getShort("negativeShort"));
}

@Test
Expand Down Expand Up @@ -162,6 +166,9 @@ private void validate(ParameterTool parameter) {
Assert.assertEquals("myDefaultValue", parameter.get("output", "myDefaultValue"));
Assert.assertEquals(null, parameter.get("whatever"));
Assert.assertEquals(15L, parameter.getLong("expectedCount", -1L));
Assert.assertTrue(parameter.getBoolean("thisIsUseful", true));
Assert.assertEquals(42, parameter.getByte("myDefaultByte", (byte) 42));
Assert.assertEquals(42, parameter.getShort("myDefaultShort", (short) 42));

Configuration config = parameter.getConfiguration();
Assert.assertEquals(15L, config.getLong("expectedCount", -1L));
Expand Down

0 comments on commit 6b26e23

Please sign in to comment.