📄 lazydynabean.java
字号:
return value;
}
/**
* <p>Return the value of an indexed property with the specified name.</p>
*
* <p><strong>N.B.</strong> Returns <code>null</code> if there is no 'indexed'
* property of the specified name.</p>
*
* @param name Name of the property whose value is to be retrieved
* @param index Index of the value to be retrieved
* @return The indexed property's value
*
* @exception IllegalArgumentException if the specified property
* exists, but is not indexed
* @exception IndexOutOfBoundsException if the specified index
* is outside the range of the underlying property
*/
public Object get(String name, int index) {
// If its not a property, then create default indexed property
if (!isDynaProperty(name)) {
set(name, defaultIndexedProperty(name));
}
// Get the indexed property
Object indexedProperty = get(name);
// Check that the property is indexed
if (!dynaClass.getDynaProperty(name).isIndexed()) {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]' "
+ dynaClass.getDynaProperty(name).getName());
}
// Grow indexed property to appropriate size
indexedProperty = growIndexedProperty(name, indexedProperty, index);
// Return the indexed value
if (indexedProperty.getClass().isArray()) {
return Array.get(indexedProperty, index);
} else if (indexedProperty instanceof List) {
return ((List)indexedProperty).get(index);
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]' "
+ indexedProperty.getClass().getName());
}
}
/**
* <p>Return the value of a mapped property with the specified name.</p>
*
* <p><strong>N.B.</strong> Returns <code>null</code> if there is no 'mapped'
* property of the specified name.</p>
*
* @param name Name of the property whose value is to be retrieved
* @param key Key of the value to be retrieved
* @return The mapped property's value
*
* @exception IllegalArgumentException if the specified property
* exists, but is not mapped
*/
public Object get(String name, String key) {
// If its not a property, then create default mapped property
if (!isDynaProperty(name)) {
set(name, defaultMappedProperty(name));
}
// Get the mapped property
Object mappedProperty = get(name);
// Check that the property is mapped
if (!dynaClass.getDynaProperty(name).isMapped()) {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")' "
+ dynaClass.getDynaProperty(name).getType().getName());
}
// Get the value from the Map
if (mappedProperty instanceof Map) {
return (((Map) mappedProperty).get(key));
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'"
+ mappedProperty.getClass().getName());
}
}
/**
* Return the <code>DynaClass</code> instance that describes the set of
* properties available for this DynaBean.
*
* @return The associated DynaClass
*/
public DynaClass getDynaClass() {
return dynaClass;
}
/**
* Remove any existing value for the specified key on the
* specified mapped property.
*
* @param name Name of the property for which a value is to
* be removed
* @param key Key of the value to be removed
*
* @exception IllegalArgumentException if there is no property
* of the specified name
*/
public void remove(String name, String key) {
if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
Object value = values.get(name);
if (value == null) {
return;
}
if (value instanceof Map) {
((Map) value).remove(key);
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'"
+ value.getClass().getName());
}
}
/**
* Set the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
*
* @exception IllegalArgumentException if this is not an existing property
* name for our DynaClass and the MutableDynaClass is restricted
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception NullPointerException if an attempt is made to set a
* primitive property to null
*/
public void set(String name, Object value) {
// If the property doesn't exist, then add it
if (!isDynaProperty(name)) {
if (dynaClass.isRestricted()) {
throw new IllegalArgumentException
("Invalid property name '" + name + "' (DynaClass is restricted)");
}
if (value == null) {
dynaClass.add(name);
} else {
dynaClass.add(name, value.getClass());
}
}
DynaProperty descriptor = dynaClass.getDynaProperty(name);
if (value == null) {
if (descriptor.getType().isPrimitive()) {
throw new NullPointerException
("Primitive value for '" + name + "'");
}
} else if (!isAssignable(descriptor.getType(), value.getClass())) {
throw new ConversionException
("Cannot assign value of type '" +
value.getClass().getName() +
"' to property '" + name + "' of type '" +
descriptor.getType().getName() + "'");
}
// Set the property's value
values.put(name, value);
}
/**
* Set the value of an indexed property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param index Index of the property to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not indexed
* @exception IndexOutOfBoundsException if the specified index
* is outside the range of the underlying property
*/
public void set(String name, int index, Object value) {
// If its not a property, then create default indexed property
if (!isDynaProperty(name)) {
set(name, defaultIndexedProperty(name));
}
// Get the indexed property
Object indexedProperty = get(name);
// Check that the property is indexed
if (!dynaClass.getDynaProperty(name).isIndexed()) {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]'"
+ dynaClass.getDynaProperty(name).getType().getName());
}
// Grow indexed property to appropriate size
indexedProperty = growIndexedProperty(name, indexedProperty, index);
// Set the value in an array
if (indexedProperty.getClass().isArray()) {
Array.set(indexedProperty, index, value);
} else if (indexedProperty instanceof List) {
((List)indexedProperty).set(index, value);
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]' "
+ indexedProperty.getClass().getName());
}
}
/**
* Set the value of a mapped property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param key Key of the property to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not mapped
*/
public void set(String name, String key, Object value) {
// If the 'mapped' property doesn't exist, then add it
if (!isDynaProperty(name)) {
set(name, defaultMappedProperty(name));
}
// Get the mapped property
Object mappedProperty = get(name);
// Check that the property is mapped
if (!dynaClass.getDynaProperty(name).isMapped()) {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'"
+ dynaClass.getDynaProperty(name).getType().getName());
}
// Set the value in the Map
((Map)mappedProperty).put(key, value);
}
// ------------------- protected Methods ----------------------------------
/**
* Grow the size of an indexed property
* @param name The name of the property
* @param indexedProperty The current property value
* @param index The indexed value to grow the property to (i.e. one less than
* the required size)
* @return The new property value (grown to the appropriate size)
*/
protected Object growIndexedProperty(String name, Object indexedProperty, int index) {
// Grow a List to the appropriate size
if (indexedProperty instanceof List) {
List list = (List)indexedProperty;
while (index >= list.size()) {
Class contentType = getDynaClass().getDynaProperty(name).getContentType();
Object value = null;
if (contentType != null) {
value = createProperty(name+"["+list.size()+"]", contentType);
}
list.add(value);
}
}
// Grow an Array to the appropriate size
if ((indexedProperty.getClass().isArray())) {
int length = Array.getLength(indexedProperty);
if (index >= length) {
Class componentType = indexedProperty.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, (index + 1));
System.arraycopy(indexedProperty, 0, newArray, 0, length);
indexedProperty = newArray;
set(name, indexedProperty);
int newLength = Array.getLength(indexedProperty);
for (int i = length; i < newLength; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -