Skip to content

Commit

Permalink
Simplify expression
Browse files Browse the repository at this point in the history
Reduce whitespace
  • Loading branch information
garydgregory committed Sep 3, 2024
1 parent e96a9b7 commit 02ed45c
Showing 1 changed file with 19 additions and 52 deletions.
71 changes: 19 additions & 52 deletions src/main/java/org/apache/commons/beanutils2/BasicDynaBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* successfully serialized and deserialized <strong>ONLY</strong> if all
* property values are {@code Serializable}.</p>
*/

public class BasicDynaBean implements DynaBean, Serializable {

private static final Short SHORT_ZERO = Short.valueOf((short) 0);
Expand Down Expand Up @@ -75,9 +74,7 @@ public class BasicDynaBean implements DynaBean, Serializable {
* @param dynaClass The DynaClass we are associated with
*/
public BasicDynaBean(final DynaClass dynaClass) {

this.dynaClass = dynaClass;

}

/**
Expand All @@ -94,15 +91,12 @@ public BasicDynaBean(final DynaClass dynaClass) {
*/
@Override
public boolean contains(final String name, final String key) {

final Object value = values.get(name);
requireMappedValue(name, key, value);
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 All @@ -116,19 +110,16 @@ public boolean contains(final String name, final String key) {
*/
@Override
public Object get(final String name) {

// Return any non-null value for the specified property
final Object value = values.get(name);
if (value != null) {
return value;
}

// Return a null value for a non-primitive property
final Class<?> type = getDynaProperty(name).getType();
if (!type.isPrimitive()) {
return value;
}

// Manufacture default values for primitive properties
if (type == Boolean.TYPE) {
return Boolean.FALSE;
Expand All @@ -155,7 +146,6 @@ public Object get(final String name) {
return SHORT_ZERO;
}
return null;

}

/**
Expand All @@ -176,7 +166,6 @@ public Object get(final String name) {
*/
@Override
public Object get(final String name, final int index) {

final Object value = values.get(name);
Objects.requireNonNull(value, "No indexed value for '" + name + "[" + index + "]'");
if (value.getClass().isArray()) {
Expand All @@ -185,9 +174,7 @@ public Object get(final String name, final int 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 @@ -205,14 +192,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);
requireMappedValue(name, key, value);
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 All @@ -224,9 +209,7 @@ public Object get(final String name, final String key) {
*/
@Override
public DynaClass getDynaClass() {

return this.dynaClass;

}

/**
Expand All @@ -241,8 +224,7 @@ public DynaClass getDynaClass() {
protected DynaProperty getDynaProperty(final String name) {
final DynaProperty descriptor = getDynaClass().getDynaProperty(name);
if (descriptor == null) {
throw new IllegalArgumentException
("Invalid property name '" + name + "'");
throw new IllegalArgumentException("Invalid property name '" + name + "'");
}
return descriptor;

Expand All @@ -260,7 +242,6 @@ protected DynaProperty getDynaProperty(final String name) {
* @since 1.8.0
*/
public Map<String, Object> getMap() {

// cache the Map
if (mapDecorator == null) {
mapDecorator = new DynaBeanPropertyMapDecorator(this);
Expand All @@ -278,20 +259,17 @@ public Map<String, Object> getMap() {
* destination class, otherwise {@code false}
*/
protected boolean isAssignable(final Class<?> dest, final Class<?> source) {

if (dest.isAssignableFrom(source) ||
dest == Boolean.TYPE && source == Boolean.class ||
dest == Byte.TYPE && source == Byte.class ||
dest == Character.TYPE && source == Character.class ||
dest == Double.TYPE && source == Double.class ||
dest == Float.TYPE && source == Float.class ||
dest == Integer.TYPE && source == Integer.class ||
dest == Long.TYPE && source == Long.class ||
dest == Short.TYPE && source == Short.class) {
return true;
}
return false;

// @formatter:off
return dest.isAssignableFrom(source) ||
dest == Boolean.TYPE && source == Boolean.class ||
dest == Byte.TYPE && source == Byte.class ||
dest == Character.TYPE && source == Character.class ||
dest == Double.TYPE && source == Double.class ||
dest == Float.TYPE && source == Float.class ||
dest == Integer.TYPE && source == Integer.class ||
dest == Long.TYPE && source == Long.class ||
dest == Short.TYPE && source == Short.class;
// @formatter:on
}

/**
Expand All @@ -307,15 +285,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);
requireMappedValue(name, key, value);
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);

}

private void requireMappedValue(final String name, final String key, final Object value) {
Expand All @@ -340,27 +315,23 @@ private void requireMappedValue(final String name, final String key, final Objec
*/
@Override
public void set(final String name, final int index, final Object value) {

final Object prop = values.get(name);
Objects.requireNonNull(prop, "No indexed value for '" + name + "[" + index + "]'");
if (prop.getClass().isArray()) {
Array.set(prop, index, value);
} else if (prop instanceof List) {
try {
@SuppressWarnings("unchecked")
final
// This is safe to cast because list properties are always
// of type Object
List<Object> list = (List<Object>) prop;
final List<Object> list = (List<Object>) prop;
list.set(index, value);
} catch (final ClassCastException e) {
throw new ConversionException(e.getMessage());
}
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]'");
throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
}

}

/**
Expand All @@ -378,19 +349,16 @@ public void set(final String name, final int index, final Object value) {
*/
@Override
public void set(final String name, final Object value) {

final DynaProperty descriptor = getDynaProperty(name);
if (value == null) {
if (descriptor.getType().isPrimitive()) {
throw new NullPointerException
("Primitive value for '" + name + "'");
throw new NullPointerException("Primitive value for '" + name + "'");
}
} else if (!isAssignable(descriptor.getType(), value.getClass())) {
throw ConversionException.format("Cannot assign value of type '%s' to property '%s' of type '%s'", value.getClass().getName(), name,
descriptor.getType().getName());
}
values.put(name, value);

}

/**
Expand Down Expand Up @@ -418,7 +386,6 @@ public void set(final String name, final String key, final Object value) {
// This is safe to cast because mapped properties are always
// maps of types String -> Object
((Map<String, Object>) prop).put(key, value);

}

}

0 comments on commit 02ed45c

Please sign in to comment.