|
Commons Beanutils example source code file (LazyDynaList.java)
The Commons Beanutils LazyDynaList.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.beanutils; import java.util.ArrayList; import java.util.Map; import java.util.Collection; import java.util.Iterator; import java.lang.reflect.Array; /** * <h2>Lazy DynaBean List. * * <p>There are two main purposes for this class: * <ul> * <li>To provide Lazy List behaviour - automatically * <i>growing and populating the* * <p>New elements of the appropriate Map type are * automatically populated:</p> * * <pre>
* // get(index) automatically grows the list
* DynaBean newElement = (DynaBean)lazyList.get(lazyList.size());
* newElement.put("someProperty", "someValue");
* </code>
*
* <p>Once you've finished you can get back an Array of the
* elements of the appropriate type:</p>
*
* <pre> method or the
* <code>setElementDynaClass(DynaClass) method - then populate
* with the normal <code>java.util.List methods(i.e.
* <code>add(), addAll() or set() ).
*
* <pre>
* // Create a new LazyDynaList (100 element capacity)
* LazyDynaList lazyList = new LazyDynaList(100);
*
* // Either Set the element type...
* lazyList.setElementType(TreeMap.class);
*
* // ...or the element DynaClass...
* lazyList.setElementDynaClass(new MyCustomDynaClass());
*
* // Populate from a collection
* lazyList.addAll(myCollection);
*
* </code>
*
* @author Niall Pemberton
* @version $Revision: 926529 $ $Date: 2010-03-23 11:44:24 +0000 (Tue, 23 Mar 2010) $
* @since 1.8.0
*/
public class LazyDynaList extends ArrayList {
/**
* The DynaClass of the List's elements.
*/
private DynaClass elementDynaClass;
/**
* The WrapDynaClass if the List's contains
* POJO Bean elements.
*
* N.B. WrapDynaClass isn't serlializable, which
* is why its stored separately in a
* transient instance variable.
*/
private transient WrapDynaClass wrapDynaClass;
/**
* The type of the List's elements.
*/
private Class elementType;
/**
* The DynaBean type of the List's elements.
*/
private Class elementDynaBeanType;
// ------------------- Constructors ------------------------------
/**
* Default Constructor.
*/
public LazyDynaList() {
super();
}
/**
* Construct a LazyDynaList with the
* specified capacity.
*
* @param capacity The initial capacity of the list.
*/
public LazyDynaList(int capacity) {
super(capacity);
}
/**
* Construct a LazyDynaList with a
* specified DynaClass for its elements.
*
* @param elementDynaClass The DynaClass of the List's elements.
*/
public LazyDynaList(DynaClass elementDynaClass) {
super();
setElementDynaClass(elementDynaClass);
}
/**
* Construct a LazyDynaList with a
* specified type for its elements.
*
* @param elementType The Type of the List's elements.
*/
public LazyDynaList(Class elementType) {
super();
setElementType(elementType);
}
/**
* Construct a LazyDynaList populated with the
* elements of a Collection.
*
* @param collection The Collection to poulate the List from.
*/
public LazyDynaList(Collection collection) {
super(collection.size());
addAll(collection);
}
/**
* Construct a LazyDynaList populated with the
* elements of an Array.
*
* @param array The Array to poulate the List from.
*/
public LazyDynaList(Object[] array) {
super(array.length);
for (int i = 0; i < array.length; i++) {
add(array[i]);
}
}
// ------------------- java.util.List Methods --------------------
/**
* <p>Insert an element at the specified index position.
*
* <p>If the index position is greater than the current
* size of the List, then the List is automatically
* <i>grown to the appropriate size.
*
* @param index The index position to insert the new element.
* @param element The new element to add.
*/
public void add(int index, Object element) {
DynaBean dynaBean = transform(element);
growList(index);
super.add(index, dynaBean);
}
/**
* <p>Add an element to the List.
*
* @param element The new element to add.
* @return true.
*/
public boolean add(Object element) {
DynaBean dynaBean = transform(element);
return super.add(dynaBean);
}
/**
* <p>Add all the elements from a Collection to the list.
*
* @param collection The Collection of new elements.
* @return true if elements were added.
*/
public boolean addAll(Collection collection) {
if (collection == null || collection.size() == 0) {
return false;
}
ensureCapacity(size() + collection.size());
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
add(iterator.next());
}
return true;
}
/**
* <p>Insert all the elements from a Collection into the
* list at a specified position.
*
* <p>If the index position is greater than the current
* size of the List, then the List is automatically
* <i>grown to the appropriate size.
*
* @param collection The Collection of new elements.
* @param index The index position to insert the new elements at.
* @return true if elements were added.
*/
public boolean addAll(int index, Collection collection) {
if (collection == null || collection.size() == 0) {
return false;
}
ensureCapacity((index > size() ? index : size()) + collection.size());
// Call "tranform" with first element, before
// List is "grown" to ensure the correct DynaClass
// is set.
if (size() == 0) {
transform(collection.iterator().next());
}
growList(index);
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
add(index++, iterator.next());
}
return true;
}
/**
* <p>Return the element at the specified position.
*
* <p>If the position requested is greater than the current
* size of the List, then the List is automatically
* <i>grown (and populated) to the appropriate size.
*
* @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>If the position requested is greater than the current
* size of the List, then the List is automatically
* <i>grown (and populated) to the appropriate size.
*
* @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>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.
*
* @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] = get(i);
} else {
array[i] = ((WrapDynaBean)get(i)).getInstance();
}
}
return array;
}
/**
* <p>Converts the List to an Array of the specified type.
*
* @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.
*
* @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.
*
* @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");
}
boolean changeType = (this.elementType != null && !this.elementType.equals(elementType));
if (changeType && 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.
*
* @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 grown 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:
*
* <ul>
* <li>Map elements are turned into LazyDynaMap's.
* <li>POJO Beans are "wrapped" in a WrapDynaBean.
* <li>DynaBeans are unchanged.
* </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)
if (getDynaClass() == null) {
setElementType(elementType);
}
// Create a new DynaBean
try {
dynaBean = getDynaClass().newInstance();
newDynaBeanType = dynaBean.getClass();
} catch (Exception e) {
throw new IllegalArgumentException("Error creating DynaBean: "
+ getDynaClass().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 (elementType != null && !newElementType.equals(elementType)) {
throw new IllegalArgumentException("Element Type " + newElementType
+ " doesn't match other elements " + elementType);
}
return dynaBean;
}
/**
* Return the DynaClass.
*/
private DynaClass getDynaClass() {
return (elementDynaClass == null ? wrapDynaClass : elementDynaClass);
}
}
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.