📄 lazydynalist.java
字号:
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
add(index++, iterator.next());
}
return true;
}
/**
* <p>Return the element at the specified position.</p>
*
* <p>If the position requested is greater than the current
* size of the List, then the List is automatically
* <i>grown</i> (and populated) to the appropriate size.</p>
*
* @param index The index position to insert the new elements at.
* @return The element at the specified position.
*/
public Object get(int index) {
growList(index + 1);
return super.get(index);
}
/**
* <p>Set the element at the specified position.</p>
*
* <p>If the position requested is greater than the current
* size of the List, then the List is automatically
* <i>grown</i> (and populated) to the appropriate size.</p>
*
* @param index The index position to insert the new element at.
* @param element The new element.
* @return The new element.
*/
public Object set(int index, Object element) {
DynaBean dynaBean = transform(element);
growList(index + 1);
return super.set(index, dynaBean);
}
/**
* <p>Converts the List to an Array.</p>
*
* <p>The type of Array created depends on the contents
* of the List:</p>
* <ul>
* <li>If the List contains only LazyDynaMap type elements
* then a java.util.Map[] array will be created.</li>
* <li>If the List contains only elements which are
* "wrapped" DynaBeans then an Object[] of the most
* suitable type will be created.</li>
* <li>...otherwise a DynaBean[] will be created.</li>
*
* @return An Array of the elements in this List.
*/
public Object[] toArray() {
if (size() == 0 && elementType == null) {
return new LazyDynaBean[0];
}
Object[] array = (Object[])Array.newInstance(elementType, size());
for (int i = 0; i < size(); i++) {
if (Map.class.isAssignableFrom(elementType)) {
array[i] = ((LazyDynaMap)get(i)).getMap();
} else if (DynaBean.class.isAssignableFrom(elementType)) {
array[i] = (DynaBean)get(i);
} else {
array[i] = ((WrapDynaBean)get(i)).getInstance();
}
}
return array;
}
/**
* <p>Converts the List to an Array of the specified type.</p>
*
* @param model The model for the type of array to return
* @return An Array of the elements in this List.
*/
public Object[] toArray(Object[] model) {
// Allocate the Array
Class arrayType = model.getClass().getComponentType();
Object[] array = (Object[])Array.newInstance(arrayType, size());
if (size() == 0 && elementType == null) {
return new LazyDynaBean[0];
}
if ((DynaBean.class.isAssignableFrom(arrayType))) {
for (int i = 0; i < size(); i++) {
array[i] = get(i);
}
return array;
}
if ((arrayType.isAssignableFrom(elementType))) {
for (int i = 0; i < size(); i++) {
if (Map.class.isAssignableFrom(elementType)) {
array[i] = ((LazyDynaMap)get(i)).getMap();
} else if (DynaBean.class.isAssignableFrom(elementType)) {
array[i] = get(i);
} else {
array[i] = ((WrapDynaBean)get(i)).getInstance();
}
}
return array;
}
throw new IllegalArgumentException("Invalid array type: "
+ arrayType.getName() + " - not compatible with '"
+ elementType.getName());
}
// ------------------- Public Methods ----------------------------
/**
* <p>Converts the List to an DynaBean Array.</p>
*
* @return A DynaBean[] of the elements in this List.
*/
public DynaBean[] toDynaBeanArray() {
if (size() == 0 && elementDynaBeanType == null) {
return new LazyDynaBean[0];
}
DynaBean[] array = (DynaBean[])Array.newInstance(elementDynaBeanType, size());
for (int i = 0; i < size(); i++) {
array[i] = (DynaBean)get(i);
}
return array;
}
/**
* <p>Set the element Type and DynaClass.</p>
*
* @param elementType The type of the elements.
* @exception IllegalArgumentException if the List already
* contains elements or the DynaClass is null.
*/
public void setElementType(Class elementType) {
if (elementType == null) {
throw new IllegalArgumentException("Element Type is missing");
}
if (size() > 0) {
throw new IllegalStateException("Element Type cannot be reset");
}
this.elementType = elementType;
// Create a new object of the specified type
Object object = null;
try {
object = elementType.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Error creating type: "
+ elementType.getName() + " - " + e);
}
// Create a DynaBean
DynaBean dynaBean = null;
if (Map.class.isAssignableFrom(elementType)) {
dynaBean = new LazyDynaMap((Map)object);
this.elementDynaClass = dynaBean.getDynaClass();
} else if (DynaBean.class.isAssignableFrom(elementType)) {
dynaBean = (DynaBean)object;
this.elementDynaClass = dynaBean.getDynaClass();
} else {
dynaBean = new WrapDynaBean(object);
this.wrapDynaClass = (WrapDynaClass)dynaBean.getDynaClass();
}
this.elementDynaBeanType = dynaBean.getClass();
// Re-calculate the type
if (WrapDynaBean.class.isAssignableFrom(elementDynaBeanType )) {
this.elementType = ((WrapDynaBean)dynaBean).getInstance().getClass();
} else if (LazyDynaMap.class.isAssignableFrom(elementDynaBeanType )) {
this.elementType = ((LazyDynaMap)dynaBean).getMap().getClass();
}
}
/**
* <p>Set the element Type and DynaClass.</p>
*
* @param elementDynaClass The DynaClass of the elements.
* @exception IllegalArgumentException if the List already
* contains elements or the DynaClass is null.
*/
public void setElementDynaClass(DynaClass elementDynaClass) {
if (elementDynaClass == null) {
throw new IllegalArgumentException("Element DynaClass is missing");
}
if (size() > 0) {
throw new IllegalStateException("Element DynaClass cannot be reset");
}
// Try to create a new instance of the DynaBean
try {
DynaBean dynaBean = elementDynaClass.newInstance();
this.elementDynaBeanType = dynaBean.getClass();
if (WrapDynaBean.class.isAssignableFrom(elementDynaBeanType)) {
this.elementType = ((WrapDynaBean)dynaBean).getInstance().getClass();
this.wrapDynaClass = (WrapDynaClass)elementDynaClass;
} else if (LazyDynaMap.class.isAssignableFrom(elementDynaBeanType)) {
this.elementType = ((LazyDynaMap)dynaBean).getMap().getClass();
this.elementDynaClass = elementDynaClass;
} else {
this.elementType = dynaBean.getClass();
this.elementDynaClass = elementDynaClass;
}
} catch (Exception e) {
throw new IllegalArgumentException(
"Error creating DynaBean from " +
elementDynaClass.getClass().getName() + " - " + e);
}
}
// ------------------- Private Methods ---------------------------
/**
* <p>Automatically <i>grown</i> the List
* to the appropriate size, populating with
* DynaBeans.</p>
*
* @param requiredSize the required size of the List.
*/
private void growList(int requiredSize) {
if (requiredSize < size()) {
return;
}
ensureCapacity(requiredSize + 1);
for (int i = size(); i < requiredSize; i++) {
DynaBean dynaBean = transform(null);
super.add(dynaBean);
}
}
/**
* <p>Transform the element into a DynaBean:</p>
*
* <ul>
* <li>Map elements are turned into LazyDynaMap's.</li>
* <li>POJO Beans are "wrapped" in a WrapDynaBean.</li>
* <li>DynaBeans are unchanged.</li>
* </li>
*
* @param element The element to transformt.
* @param The DynaBean to store in the List.
*/
private DynaBean transform(Object element) {
DynaBean dynaBean = null;
Class newDynaBeanType = null;
Class newElementType = null;
// Create a new element
if (element == null) {
// Default Types to LazyDynaBean
// if not specified
if (elementType == null) {
setElementDynaClass(new LazyDynaClass());
}
// Get DynaClass (restore WrapDynaClass lost in serialization)
DynaClass dynaClass = (elementDynaClass == null) ? wrapDynaClass : elementDynaClass;
if (dynaClass == null) {
setElementType(elementType);
}
// Create a new DynaBean
try {
dynaBean = dynaClass.newInstance();
newDynaBeanType = dynaBean.getClass();
} catch (Exception e) {
throw new IllegalArgumentException("Error creating DynaBean: "
+ dynaClass.getClass().getName()
+ " - " + e);
}
} else {
// Transform Object to a DynaBean
newElementType = element.getClass();
if (Map.class.isAssignableFrom(element.getClass())) {
dynaBean = new LazyDynaMap((Map)element);
} else if (DynaBean.class.isAssignableFrom(element.getClass())) {
dynaBean = (DynaBean)element;
} else {
dynaBean = new WrapDynaBean(element);
}
newDynaBeanType = dynaBean.getClass();
}
// Re-calculate the element type
newElementType = dynaBean.getClass();
if (WrapDynaBean.class.isAssignableFrom(newDynaBeanType)) {
newElementType = ((WrapDynaBean)dynaBean).getInstance().getClass();
} else if (LazyDynaMap.class.isAssignableFrom(newDynaBeanType)) {
newElementType = ((LazyDynaMap)dynaBean).getMap().getClass();
}
// Check the new element type, matches all the
// other elements in the List
if (newElementType != elementType) {
throw new IllegalArgumentException("Element Type " + newElementType
+ " doesn't match other elements " + elementType);
}
return dynaBean;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -