midrange.com code scratchpad
Name:
James R. Perkins
Scriptlanguage:
Java
Tabwidth:
4
Date:
03/06/2009 08:10:25 pm
IP:
Logged
Description:
Generic java.util.List factory
Code:
  1. /*
  2.  * Copyright (c) 2009 James R. Perkins Jr. (JRP) - All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the James R. Perkins Jr. nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. package com.jamezp.util;
  32.  
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.LinkedList;
  36. import java.util.Stack;
  37. import java.util.Vector;
  38.  
  39. /**
  40.  * This is a convenience factory class for various generic <code>List</code>
  41.  * implementations.
  42.  * 
  43.  * @author James R. Perkins (JRP)
  44.  * 
  45.  */
  46. public class ListFactory {
  47.     /**
  48.      * This static factory method will construct a new generic
  49.      * <code>ArrayList</code> instance.
  50.      * 
  51.      * @return a new generic <code>ArrayList</code> instance
  52.      * @see java.util.ArrayList
  53.      */
  54.     public static <E> ArrayList<E> newArrayList() {
  55.         return new ArrayList<E>();
  56.     }
  57.  
  58.     /**
  59.      * This static factory method will construct a new generic
  60.      * <code>ArrayList</code> instance.
  61.      * 
  62.      * @param c
  63.      *            a <code>Collection</code> to initialize the
  64.      *            <code>ArrayList</code> with
  65.      * 
  66.      * @return a new generic <code>ArrayList</code> instance
  67.      * @see java.util.ArrayList
  68.      */
  69.     public static <E> ArrayList<E> newArrayList(Collection<? extends E> c) {
  70.         return new ArrayList<E>(c);
  71.     }
  72.  
  73.     /**
  74.      * This static factory method will construct a new generic
  75.      * <code>ArrayList</code> instance.
  76.      * 
  77.      * @param initialCapacity
  78.      *            the initial capacity of the <code>ArrayList</code>
  79.      * 
  80.      * @return a new generic <code>ArrayList</code> instance
  81.      * @see java.util.ArrayList
  82.      */
  83.     public static <E> ArrayList<E> newArrayList(int initialCapacity) {
  84.         return new ArrayList<E>(initialCapacity);
  85.     }
  86.  
  87.     /**
  88.      * This static factory method will construct a new generic
  89.      * <code>LinkedList</code> instance.
  90.      * 
  91.      * @return a new generic <code>LinkedList</code> instance
  92.      * @see java.util.LinkedList
  93.      */
  94.     public static <E> LinkedList<E> newLinkedList() {
  95.         return new LinkedList<E>();
  96.     }
  97.  
  98.     /**
  99.      * This static factory method will construct a new generic
  100.      * <code>LinkedList</code> instance.
  101.      * 
  102.      * @param c
  103.      *            a <code>Collection</code> to initialize the
  104.      *            <code>LinkedList</code> with
  105.      * 
  106.      * @return a new generic <code>LinkedList</code> instance
  107.      * @see java.util.LinkedList
  108.      */
  109.     public static <E> LinkedList<E> newLinkedList(Collection<? extends E> c) {
  110.         return new LinkedList<E>(c);
  111.     }
  112.  
  113.     /**
  114.      * This static factory method will construct a new generic
  115.      * <code>Stack</code> instance.
  116.      * 
  117.      * @return a new generic <code>Stack</code> instance
  118.      * @see java.util.Stack
  119.      */
  120.     public static <E> Stack<E> newStatck() {
  121.         return new Stack<E>();
  122.     }
  123.  
  124.     /**
  125.      * This static factory method will construct a new generic
  126.      * <code>Vector</code> instance.
  127.      * 
  128.      * @return a new generic <code>Vector</code> instance
  129.      * @see java.util.Vector
  130.      */
  131.     public static <E> Vector<E> newVector() {
  132.         return new Vector<E>();
  133.     }
  134.  
  135.     /**
  136.      * This static factory method will construct a new generic
  137.      * <code>Vector</code> instance.
  138.      * 
  139.      * @param c
  140.      *            a <code>Collection</code> to initialize the
  141.      *            <code>Vector</code> with
  142.      * 
  143.      * @return a new generic <code>Vector</code> instance
  144.      * @see java.util.Vector
  145.      */
  146.     public static <E> Vector<E> newVector(Collection<? extends E> c) {
  147.         return new Vector<E>(c);
  148.     }
  149.  
  150.     /**
  151.      * This static factory method will construct a new generic
  152.      * <code>Vector</code> instance.
  153.      * 
  154.      * @param initialCapacity
  155.      *            the initial capacity of the <code>Vector</code>
  156.      * 
  157.      * @return a new generic <code>Vector</code> instance
  158.      * @see java.util.Vector
  159.      */
  160.     public static <E> Vector<E> newVectory(int initialCapacity) {
  161.         return new Vector<E>(initialCapacity);
  162.     }
  163.  
  164.     /**
  165.      * This static factory method will construct a new generic
  166.      * <code>Vector</code> instance.
  167.      * 
  168.      * @param initialCapacity
  169.      *            the initial capacity of the <code>Vector</code>
  170.      * @param the
  171.      *            amount by which the capacity is increased when the vector
  172.      *            overflows
  173.      * 
  174.      * @return a new generic <code>Vector</code> instance
  175.      * @see java.util.Vector
  176.      */
  177.     public static <E> Vector<E> newVectory(int initialCapacity,
  178.             int capacityIncrement) {
  179.         return new Vector<E>(initialCapacity);
  180.     }
  181.  
  182. }
  183.  
© 2004-2019 by midrange.com generated in 0.022s valid xhtml & css