Skip to content

Commit

Permalink
[DMR-12] Add asXXXOrNull methods to return null from undefined nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bstansberry committed Feb 18, 2017
1 parent 8f62c4d commit cc35b92
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
93 changes: 92 additions & 1 deletion src/main/java/org/jboss/dmr/ModelNode.java
Expand Up @@ -238,6 +238,19 @@ public long asLong(final long defVal) {
return value.asLong(defVal);
}

/**
* Get the value of this node as a {@code Long}, or {@code null} if this node is not {@link #isDefined() defined}.
* Collection types will return the size of the collection for this value. Other types may attempt a string conversion.
*
* @return the long value or {@code null}
*
* @throws NumberFormatException if this node's {@link #getType() type} is {@link ModelType#STRING} and a numeric conversion of the string value is not possible
* @throws IllegalArgumentException if this node's {@link #getType() type} is one where no numeric conversion is possible
*/
public Long asLongOrNull() {
return isDefined() ? asLong() : null;
}

/**
* Get the value of this node as an {@code int}. Collection types will return the size
* of the collection for this value. Other types may attempt a string conversion.
Expand All @@ -263,6 +276,17 @@ public int asInt(final int defVal) {
return value.asInt(defVal);
}

/**
* Get the value of this node as an {@code int}, or {@code null} if this node is not {@link #isDefined() defined}.
* Collection types will return the size of the collection for this value. Other types may attempt a string conversion.
*
* @return the int value or {@code null}
* @throws IllegalArgumentException if no conversion is possible
*/
public Integer asIntOrNull() {
return isDefined() ? asInt() : null;
}

/**
* Get the value of this node as a {@code boolean}. Collection types return {@code true} for non-empty
* collections. Numerical types return {@code true} for non-zero values.
Expand All @@ -287,6 +311,18 @@ public boolean asBoolean(final boolean defVal) {
return value.asBoolean(defVal);
}


/**
* Get the value of this node as a {@code boolean}, or {@code null} if this node is not {@link #isDefined() defined}.
* Collection types return {@code true} for non-empty collections. Numerical types return {@code true} for non-zero values.
*
* @return the boolean value or {@code null}
* @throws IllegalArgumentException if no conversion is possible
*/
public Boolean asBooleanOrNull() throws IllegalArgumentException {
return isDefined() ? value.asBoolean() : null;
}

/**
* Get the value as a string. This is the literal value of this model node. More than one node type may
* yield the same value for this method.
Expand All @@ -308,6 +344,16 @@ public String asString(String defVal) {
return isDefined() ? value.asString() : defVal;
}

/**
* Get the value as a string or {@code null} if this node is not {@link #isDefined() defined}. This is the literal value of this model node. More than one node type may
* yield the same value for this method.
*
* @return the string value or {@code null}
*/
public String asStringOrNull() {
return isDefined() ? value.asString() : null;
}

/**
* Get the value of this node as a {@code double}. Collection types will return the size
* of the collection for this value. Other types may attempt a string conversion.
Expand All @@ -333,6 +379,17 @@ public double asDouble(final double defVal) {
return value.asDouble(defVal);
}

/**
* Get the value of this node as a {@code double} or {@code null} if this node is not {@link #isDefined() defined}.
* Collection types will return the size of the collection for this value. Other types may attempt a string conversion.
*
* @return the double value or {@code null}
* @throws IllegalArgumentException if no conversion is possible
*/
public Double asDoubleOrNull() throws IllegalArgumentException {
return isDefined() ? value.asDouble() : null;
}

/**
* Get the value of this node as a type, expressed using the {@code ModelType} enum. The string
* value of this node must be convertible to a type.
Expand All @@ -345,7 +402,7 @@ public ModelType asType() throws IllegalArgumentException {
}

/**
* Get the value of this node as a {@code BigDecimal}. Collection types will return the size
* Get the value of this node as a {@code BigDecimal}. Collection types will return the size
* of the collection for this value. Other types may attempt a string conversion.
*
* @return the {@code BigDecimal} value
Expand All @@ -355,6 +412,17 @@ public BigDecimal asBigDecimal() throws IllegalArgumentException {
return value.asBigDecimal();
}

/**
* Get the value of this node as a {@code BigDecimal} or {@code null} if this node is not {@link #isDefined() defined}.
* Collection types will return the size of the collection for this value. Other types may attempt a string conversion.
*
* @return the {@code BigDecimal} value or {@code null}
* @throws IllegalArgumentException if no conversion is possible
*/
public BigDecimal asBigDecimalOrNull() throws IllegalArgumentException {
return isDefined() ? value.asBigDecimal() : null;
}

/**
* Get the value of this node as a {@code BigInteger}. Collection types will return the size
* of the collection for this value. Other types may attempt a string conversion.
Expand All @@ -366,6 +434,17 @@ public BigInteger asBigInteger() throws IllegalArgumentException {
return value.asBigInteger();
}

/**
* Get the value of this node as a {@code BigInteger} or {@code null} if this node is not {@link #isDefined() defined}.
* Collection types will return the size of the collection for this value. Other types may attempt a string conversion.
*
* @return the {@code BigInteger} value or {@code null}
* @throws IllegalArgumentException if no conversion is possible
*/
public BigInteger asBigIntegerOrNull() throws IllegalArgumentException {
return isDefined() ? value.asBigInteger() : null;
}

/**
* Get the value of this node as a byte array. Strings and string-like values will return
* the UTF-8 encoding of the string. Numerical values will return the byte representation of the
Expand All @@ -378,6 +457,18 @@ public byte[] asBytes() throws IllegalArgumentException {
return value.asBytes();
}

/**
* Get the value of this node as a byte array or {@code null} if this node is not {@link #isDefined() defined}.
* Strings and string-like values will return the UTF-8 encoding of the string. Numerical values will return the
* byte representation of the number.
*
* @return the bytes or {@code null}
* @throws IllegalArgumentException if no conversion is possible
*/
public byte[] asBytesOrNull() throws IllegalArgumentException {
return isDefined() ? value.asBytes() : null;
}

/**
* Get the value of this node as an expression.
*
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/jboss/dmr/test/ModelNodeTest.java
Expand Up @@ -47,6 +47,53 @@ public void testUndefinedAsString() {
ModelNode testee = new ModelNode();
assertEquals("undefined", testee.asString());
assertEquals("a", testee.asString("a"));
assertNull(testee.asStringOrNull());
}

@Test
public void testUndefinedAsLong() {
ModelNode testee = testUndefinedConversion(ModelNode::asLong);
assertEquals(Long.MAX_VALUE, testee.asLong(Long.MAX_VALUE));
assertNull(testee.asLongOrNull());
}

@Test
public void testUndefinedAsInt() {
ModelNode testee = testUndefinedConversion(ModelNode::asInt);
assertEquals(Integer.MAX_VALUE, testee.asInt(Integer.MAX_VALUE));
assertNull(testee.asIntOrNull());
}

@Test
public void testUndefinedAsBoolean() {
ModelNode testee = testUndefinedConversion(ModelNode::asBoolean);
assertTrue(testee.asBoolean(true));
assertNull(testee.asBooleanOrNull());
}

@Test
public void testUndefinedAsDouble() {
ModelNode testee = testUndefinedConversion(ModelNode::asDouble);
assertEquals((double) 2, testee.asDouble((double) 2), 0);
assertNull(testee.asDoubleOrNull());
}

@Test
public void testUndefinedAsBigDecimal() {
ModelNode testee = testUndefinedConversion(ModelNode::asBigDecimal);
assertNull(testee.asBigDecimalOrNull());
}

@Test
public void testUndefinedAsBigInteger() {
ModelNode testee = testUndefinedConversion(ModelNode::asBigInteger);
assertNull(testee.asBigIntegerOrNull());
}

@Test
public void testUndefinedAsBytes() {
ModelNode testee = testUndefinedConversion(ModelNode::asBytes);
assertNull(testee.asBytesOrNull());
}

@Test
Expand Down

0 comments on commit cc35b92

Please sign in to comment.