Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.beanutils2;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
/**
*
Lazy DynaBean List.
*
*
There are two main purposes for this class:
*
*
To provide Lazy List behavior - automatically
* growing and populating the {@code List}
* with either {@code DynaBean
, java.util.Map}
* or POJO Beans.
*
To provide a straight forward way of putting a Collection
* or Array into the lazy list and a straight forward
* way to get it out again at the end.
*
*
*
All elements added to the List are stored as {@code DynaBean}'s:
*
*
{@code java.util.Map
elements are "wrapped" in a LazyDynaMap}.
*
POJO Bean elements are "wrapped" in a {@code WrapDynaBean}.
*
{@code DynaBean}'s are stored un-changed.
*
*
*
{@code toArray()}
*
The {@code toArray()} method returns an array of the
* elements of the appropriate type. If the {@code LazyDynaList}
* is populated with {@code java.util.Map} objects a
* {@code Map[]} array is returned.
* If the list is populated with POJO Beans an appropriate
* array of the POJO Beans is returned. Otherwise a {@code DynaBean[]}
* array is returned.
*
*
*
{@code toDynaBeanArray()}
*
The {@code toDynaBeanArray()} method returns a
* {@code DynaBean[]} array of the elements in the List.
*
*
*
N.B.All the elements in the List must be the
* same type. If the {@code DynaClass
or Class}
* of the {@code LazyDynaList}'s elements is
* not specified, then it will be automatically set to the type
* of the first element populated.
*
*
*
Example 1
*
If you have an array of {@code java.util.Map[]} - you can put that into
* a {@code LazyDynaList}.
*
*
* TreeMap[] myArray = .... // your Map[]
* List lazyList = new LazyDynaList(myArray);
*
*
*
New elements of the appropriate Map type are
* automatically populated:
*
*
* // get(index) automatically grows the list
* DynaBean newElement = (DynaBean)lazyList.get(lazyList.size());
* newElement.put("someProperty", "someValue");
*
*
*
Once you've finished you can get back an Array of the
* elements of the appropriate type:
*
*
* // Retrieve the array from the list
* TreeMap[] myArray = (TreeMap[])lazyList.toArray());
*
*
*
*
Example 2
*
Alternatively you can create an empty List and
* specify the Class for List's elements. The LazyDynaList
* uses the Class to automatically populate elements:
*
*
* // 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);
*
*
*
Example 3
*
Alternatively you can create an empty List and specify the
* DynaClass for List's elements. The LazyDynaList uses
* the DynaClass to automatically populate elements:
*
*
* // 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);
*
*
*
N.B. You may wonder why control the type
* using a {@code DynaClass
rather than the Class}
* as in the previous example - the reason is that some {@code DynaBean}
* implementations don't have a default empty constructor and
* therefore need to be instantiated using the {@code DynaClass.newInstance()}
* method.
*
*
Example 4
*
A slight variation - set the element type using either
* the {@code setElementType(Class)} method or the
* {@code setElementDynaClass(DynaClass)} method - then populate
* with the normal {@code java.util.List} methods(i.e.
* {@code add()
, addAll() or set()}).
*
*
* // 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);
*
*
*
* @since 1.8.0
*/
public class LazyDynaList extends ArrayList