Skip to content

Commit

Permalink
Use Objects.requireNonNull()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 26, 2024
1 parent 13c9f98 commit 8336ca9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 77 deletions.
59 changes: 14 additions & 45 deletions src/main/java/org/apache/commons/beanutils/BasicDynaBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* <p>Minimal implementation of the <code>DynaBean</code> interface. Can be
Expand Down Expand Up @@ -80,18 +81,12 @@ public BasicDynaBean(final DynaClass dynaClass) {
*/
@Override
public boolean contains(final String name, final String key) {

final Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
}
Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'");
if (value instanceof Map) {
return ((Map<?, ?>) value).containsKey(key);
}
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");

throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'");
}

/**
Expand Down Expand Up @@ -162,21 +157,15 @@ public Object get(final String name) {
*/
@Override
public Object get(final String name, final int index) {

final Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No indexed value for '" + name + "[" + index + "]'");
}
Objects.requireNonNull(value, () -> "No indexed value for '" + name + "[" + index + "]'");
if (value.getClass().isArray()) {
return Array.get(value, index);
}
if (value instanceof List) {
return ((List<?>) value).get(index);
}
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]'");

throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
}

/**
Expand All @@ -193,18 +182,12 @@ public Object get(final String name, final int index) {
*/
@Override
public Object get(final String name, final String key) {

final Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
}
Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'");
if (value instanceof Map) {
return ((Map<?, ?>) value).get(key);
}
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");

throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'");
}

/**
Expand Down Expand Up @@ -297,18 +280,12 @@ protected boolean isAssignable(final Class<?> dest, final Class<?> source) {
*/
@Override
public void remove(final String name, final String key) {

final Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
}
Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'");
if (!(value instanceof Map)) {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");
throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'");
}
((Map<?, ?>) value).remove(key);

}

/**
Expand All @@ -330,10 +307,7 @@ public void remove(final String name, final String key) {
public void set(final String name, final int index, final Object value) {

final Object prop = values.get(name);
if (prop == null) {
throw new NullPointerException
("No indexed value for '" + name + "[" + index + "]'");
}
Objects.requireNonNull(prop, () -> "No indexed value for '" + name + "[" + index + "]'");
if (prop.getClass().isArray()) {
Array.set(prop, index, value);
} else if (prop instanceof List) {
Expand Down Expand Up @@ -401,23 +375,18 @@ public void set(final String name, final Object value) {
*/
@Override
public void set(final String name, final String key, final Object value) {

final Object prop = values.get(name);
if (prop == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
}
Objects.requireNonNull(prop, () -> "No mapped value for '" + name + "(" + key + ")'");
if (!(prop instanceof Map)) {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");
}
@SuppressWarnings("unchecked")
final
// This is safe to cast because mapped properties are always
// maps of types String -> Object
Map<String, Object> map = (Map<String, Object>) prop;
@SuppressWarnings("unchecked")
final Map<String, Object> map = (Map<String, Object>) prop;
map.put(key, value);

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Objects;

/**
* <p>Implements <code>DynaClass</code> for DynaBeans that wrap the
Expand Down Expand Up @@ -153,11 +154,7 @@ public ResultSetDynaClass(final ResultSet resultSet, final boolean lowerCase)
*/
public ResultSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final boolean useColumnLabel)
throws SQLException {

if (resultSet == null) {
throw new NullPointerException();
}
this.resultSet = resultSet;
this.resultSet = Objects.requireNonNull(resultSet, "resultSet");
this.lowerCase = lowerCase;
setUseColumnLabel(useColumnLabel);
introspect(resultSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* <p>Implements {@link DynaClass} to create an in-memory collection
Expand Down Expand Up @@ -198,17 +199,13 @@ public RowSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final
* @since 1.8.3
*/
public RowSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final int limit, final boolean useColumnLabel)
throws SQLException {

if (resultSet == null) {
throw new NullPointerException();
}
throws SQLException {
Objects.requireNonNull(resultSet, "resultSet");
this.lowerCase = lowerCase;
this.limit = limit;
setUseColumnLabel(useColumnLabel);
introspect(resultSet);
copy(resultSet);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
Expand Down Expand Up @@ -135,55 +136,41 @@ public AbstractArrayConverter(final Object defaultValue) {
* is <code>null</code>
*/
protected List<String> parseElements(String svalue) {

// Validate the passed argument
if (svalue == null) {
throw new NullPointerException();
}

Objects.requireNonNull(svalue, "svalue");
// Trim any matching '{' and '}' delimiters
svalue = svalue.trim();
if (svalue.startsWith("{") && svalue.endsWith("}")) {
svalue = svalue.substring(1, svalue.length() - 1);
}

try {

// Set up a StreamTokenizer on the characters in this String
final StreamTokenizer st =
new StreamTokenizer(new StringReader(svalue));
st.whitespaceChars(',',','); // Commas are delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
final StreamTokenizer st = new StreamTokenizer(new StringReader(svalue));
st.whitespaceChars(',', ','); // Commas are delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.ordinaryChars('.', '.');
st.ordinaryChars('-', '-');
st.wordChars('0', '9'); // Needed to make part of tokens
st.wordChars('0', '9'); // Needed to make part of tokens
st.wordChars('.', '.');
st.wordChars('-', '-');

// Split comma-delimited tokens into a List
final ArrayList<String> list = new ArrayList<>();
while (true) {
final int ttype = st.nextToken();
if (ttype == StreamTokenizer.TT_WORD ||
ttype > 0) {
if (ttype == StreamTokenizer.TT_WORD || ttype > 0) {
list.add(st.sval);
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException
("Encountered token of type " + ttype);
throw new ConversionException("Encountered token of type " + ttype);
}
}

// Return the completed list
return list;

} catch (final IOException e) {

throw new ConversionException(e);

}

}

}

0 comments on commit 8336ca9

Please sign in to comment.