📄 lazydynalist.java
字号:
/*
* 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><i>Lazy</i> DynaBean List.</h2>
*
* <p>There are two main purposes for this class:</p>
* <ul>
* <li>To provide <i>Lazy List</i> behaviour - automatically
* <i>growing</i> and <i>populating</i> the <code>List</code>
* with either <code>DynaBean</code>, <code>java.util.Map</code>
* or POJO Beans.</li>
* <li>To provide a straight forward way of putting a Collection
* or Array into the lazy list <i>and</i> a straight forward
* way to get it out again at the end.</li>
* </ul>
*
* <p>All elements added to the List are stored as <code>DynaBean</code>'s:</p>
* <ul>
* <li><code>java.util.Map</code> elements are "wrapped" in a <code>LazyDynaMap</code>.</i>
* <li>POJO Bean elements are "wrapped" in a <code>WrapDynaBean.</code></i>
* <li><code>DynaBean</code>'s are stored un-changed.</i>
* </ul>
*
* <h4><code>toArray()</code></h4>
* <p>The <code>toArray()</code> method returns an array of the
* elements of the appropriate type. If the <code>LazyDynaList</code>
* is populated with <code>java.util.Map</code> objects a
* <code>Map[]</code> array is returned.
* If the list is populated with POJO Beans an appropriate
* array of the POJO Beans is returned. Otherwise a <code>DynaBean[]</code>
* array is returned.
* </p>
*
* <h4><code>toDynaBeanArray()</code></h4>
* <p>The <code>toDynaBeanArray()</code> method returns a
* <code>DynaBean[]</code> array of the elements in the List.
* </p>
*
* <p><strong>N.B.</strong>All the elements in the List must be the
* same type. If the <code>DynaClass</code> or <code>Class</code>
* of the <code>LazyDynaList</code>'s elements is
* not specified, then it will be automatically set to the type
* of the first element populated.
* </p>
*
* <h3>Example 1</h3>
* <p>If you have an array of <code>java.util.Map[]</code> - you can put that into
* a <code>LazyDynaList</code>.</p>
*
* <pre><code>
* TreeMap[] myArray = .... // your Map[]
* List lazyList = new LazyDynaList(myArray);
* </code></pre>
*
* <p>New elements of the appropriate Map type are
* automatically populated:</p>
*
* <pre><code>
* // get(index) automatically grows the list
* DynaBean newElement = (DynaBean)lazyList.get(lazyList.size());
* newElement.put("someProperty", "someValue");
* </code></pre>
*
* <p>Once you've finished you can get back an Array of the
* elements of the appropriate type:</p>
*
* <pre><code>
* // Retrieve the array from the list
* TreeMap[] myArray = (TreeMap[])lazyList.toArray());
* </code></pre>
*
*
* <h3>Example 2</h3>
* <p>Alternatively you can create an <i>empty</i> List and
* specify the Class for List's elements. The LazyDynaList
* uses the Class to automatically populate elements:</p>
*
* <pre><code>
* // e.g. For Maps
* List lazyList = new LazyDynaList(TreeMap.class);
*
* // e.g. For POJO Beans
* List lazyList = new LazyDynaList(MyPojo.class);
*
* // e.g. For DynaBeans
* List lazyList = new LazyDynaList(MyDynaBean.class);
* </code></pre>
*
* <h3>Example 3</h3>
* <p>Alternatively you can create an <i>empty</i> List and specify the
* DynaClass for List's elements. The LazyDynaList uses
* the DynaClass to automatically populate elements:</p>
*
* <pre><code>
* // e.g. For Maps
* DynaClass dynaClass = new LazyDynaMap(new HashMap());
* List lazyList = new LazyDynaList(dynaClass);
*
* // e.g. For POJO Beans
* DynaClass dynaClass = (new WrapDynaBean(myPojo)).getDynaClass();
* List lazyList = new LazyDynaList(dynaClass);
*
* // e.g. For DynaBeans
* DynaClass dynaClass = new BasicDynaClass(properties);
* List lazyList = new LazyDynaList(dynaClass);
* </code></pre>
*
* <p><strong>N.B.</strong> You may wonder why control the type
* using a <code>DynaClass</code> rather than the <code>Class</code>
* as in the previous example - the reason is that some <code>DynaBean</code>
* implementations don't have a <i>default</i> empty constructor and
* therefore need to be instantiated using the <code>DynaClass.newInstance()</code>
* method.</p>
*
* <h3>Example 4</h3>
* <p>A slight variation - set the element type using either
* the <code>setElementType(Class)</code> method or the
* <code>setElementDynaClass(DynaClass)</code> method - then populate
* with the normal <code>java.util.List</code> methods(i.e.
* <code>add()</code>, <code>addAll()</code> or <code>set()</code>).</p>
*
* <pre><code>
* // 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></pre>
*
* @author Niall Pemberton
* @version $Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
*/
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>
*
* <p>If the index position is greater than the current
* size of the List, then the List is automatically
* <i>grown</i> to the appropriate size.</p>
*
* @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.</p>
*
* @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</i> to the appropriate size.</p>
*
* @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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -