📄 lazydynamap.java
字号:
* zero-length array will be returned.</p>
*
* <p><strong>FIXME</strong> - Should we really be implementing
* <code>getBeanInfo()</code> instead, which returns property descriptors
* and a bunch of other stuff?</p>
* @return the set of properties for this DynaClass
*/
public DynaProperty[] getDynaProperties() {
int i = 0;
DynaProperty[] properties = new DynaProperty[values.size()];
Iterator iterator = values.keySet().iterator();
while (iterator.hasNext()) {
String name = (String)iterator.next();
Object value = values.get(name);
properties[i++] = new DynaProperty(name, value == null ? null : value.getClass());
}
return properties;
}
/**
* Instantiate and return a new DynaBean instance, associated
* with this DynaClass.
* @return A new <code>DynaBean</code> instance
*/
public DynaBean newInstance() {
// Create a new instance of the Map
Map newMap = null;
try {
newMap = (Map)getMap().getClass().newInstance();
} catch(Exception ex) {
newMap = newMap();
}
// Crate new LazyDynaMap and initialize properties
LazyDynaMap lazyMap = new LazyDynaMap(newMap);
DynaProperty[] properties = this.getDynaProperties();
if (properties != null) {
for (int i = 0; i < properties.length; i++) {
lazyMap.add(properties[i]);
}
}
return lazyMap;
}
// ------------------- MutableDynaClass Methods ----------------------------------
/**
* <p>Is this DynaClass currently restricted.</p>
* <p>If restricted, no changes to the existing registration of
* property names, data types, readability, or writeability are allowed.</p>
*
* @return <code>true</code> if this Mutable {@link DynaClass} is restricted,
* otherwise <code>false</code>
*/
public boolean isRestricted() {
return restricted;
}
/**
* <p>Set whether this DynaClass is currently restricted.</p>
* <p>If restricted, no changes to the existing registration of
* property names, data types, readability, or writeability are allowed.</p>
*
* @param restricted The new restricted state
*/
public void setRestricted(boolean restricted) {
this.restricted = restricted;
}
/**
* Add a new dynamic property with no restrictions on data type,
* readability, or writeability.
*
* @param name Name of the new dynamic property
*
* @exception IllegalArgumentException if name is null
*/
public void add(String name) {
add(name, null);
}
/**
* Add a new dynamic property with the specified data type, but with
* no restrictions on readability or writeability.
*
* @param name Name of the new dynamic property
* @param type Data type of the new dynamic property (null for no
* restrictions)
*
* @exception IllegalArgumentException if name is null
* @exception IllegalStateException if this DynaClass is currently
* restricted, so no new properties can be added
*/
public void add(String name, Class type) {
if (name == null) {
throw new IllegalArgumentException("Property name is missing.");
}
if (isRestricted()) {
throw new IllegalStateException("DynaClass is currently restricted. No new properties can be added.");
}
Object value = values.get(name);
// Check if the property already exists
if (value == null) {
values.put(name, type == null ? null : createProperty(name, type));
}
}
/**
* <p>Add a new dynamic property with the specified data type, readability,
* and writeability.</p>
*
* <p><strong>N.B.</strong>Support for readable/writeable properties has not been implemented
* and this method always throws a <code>UnsupportedOperationException</code>.</p>
*
* <p>I'm not sure the intention of the original authors for this method, but it seems to
* me that readable/writable should be attributes of the <code>DynaProperty</code> class
* (which they are not) and is the reason this method has not been implemented.</p>
*
* @param name Name of the new dynamic property
* @param type Data type of the new dynamic property (null for no
* restrictions)
* @param readable Set to <code>true</code> if this property value
* should be readable
* @param writeable Set to <code>true</code> if this property value
* should be writeable
*
* @exception UnsupportedOperationException anytime this method is called
*/
public void add(String name, Class type, boolean readable, boolean writeable) {
throw new java.lang.UnsupportedOperationException("readable/writable properties not supported");
}
/**
* Add a new dynamic property.
*
* @param property Property the new dynamic property to add.
*
* @exception IllegalArgumentException if name is null
*/
protected void add(DynaProperty property) {
add(property.getName(), property.getType());
}
/**
* Remove the specified dynamic property, and any associated data type,
* readability, and writeability, from this dynamic class.
* <strong>NOTE</strong> - This does <strong>NOT</strong> cause any
* corresponding property values to be removed from DynaBean instances
* associated with this DynaClass.
*
* @param name Name of the dynamic property to remove
*
* @exception IllegalArgumentException if name is null
* @exception IllegalStateException if this DynaClass is currently
* restricted, so no properties can be removed
*/
public void remove(String name) {
if (name == null) {
throw new IllegalArgumentException("Property name is missing.");
}
if (isRestricted()) {
throw new IllegalStateException("DynaClass is currently restricted. No properties can be removed.");
}
// Remove, if property doesn't exist
if (values.containsKey(name)) {
values.remove(name);
}
}
// ------------------- Additional Public Methods ----------------------------------
/**
* Should this DynaClass return a <code>null</code> from
* the <code>getDynaProperty(name)</code> method if the property
* doesn't exist.
*
* @return <code>true<code> if a <code>null</code> {@link DynaProperty}
* should be returned if the property doesn't exist, otherwise
* <code>false</code> if a new {@link DynaProperty} should be created.
*/
public boolean isReturnNull() {
return returnNull;
}
/**
* Set whether this DynaClass should return a <code>null</code> from
* the <code>getDynaProperty(name)</code> method if the property
* doesn't exist.
*
* @param returnNull <code>true<code> if a <code>null</code> {@link DynaProperty}
* should be returned if the property doesn't exist, otherwise
* <code>false</code> if a new {@link DynaProperty} should be created.
*/
public void setReturnNull(boolean returnNull) {
this.returnNull = returnNull;
}
// ------------------- Protected Methods ----------------------------------
/**
* <p>Indicate whether a property actually exists.</p>
*
* <p><strong>N.B.</strong> Using <code>getDynaProperty(name) == null</code>
* doesn't work in this implementation because that method might
* return a DynaProperty if it doesn't exist (depending on the
* <code>returnNull</code> indicator).</p>
*
* @param name Name of the dynamic property
* @return <code>true</code> if the property exists,
* otherwise <code>false</code>
* @exception IllegalArgumentException if no property name is specified
*/
protected boolean isDynaProperty(String name) {
if (name == null) {
throw new IllegalArgumentException("Property name is missing.");
}
return values.containsKey(name);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -