org.jaitools.CollectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jt-utils Show documentation
Show all versions of jt-utils Show documentation
Support and utility classes used by other JAITools components and
available for general use.
/*
* Copyright (c) 2009-2011, Michael Bedward. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jaitools;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Convenience methods to create generic collections
* following the DRY (Don't Repeat Yourself) principle.
* Examples of use:
*
* // simple
* List<Integer> list1 = CollectionFactory.list();
*
* // nested
* List<List<String>> list2 = CollectionFactory.list();
*
* // a multi-map
* Map<String, List<Integer>> multiMap = CollectionFactory.map();
*
*
* @author Michael Bedward
* @since 1.0
* @version $Id$
*/
public class CollectionFactory {
/**
* Returns a new {@code Map} object. The returned
* object is a {@link HashMap}.
*
* Example of use:
*
* Map<Foo, Bar> myMap = CollectionFactory.map();
*
*
* @param key type
* @param value type
* @return a new Map<K,V> instance
*/
public static Map map() {
return new HashMap();
}
/**
* Returns a new {@code Map} object that maintains insertion
* order. The returned object is a {@link LinkedHashMap}.
*
* Example of use:
*
* Map<Foo, Bar> myMap = CollectionFactory.orderedMap();
*
*
* @param key type
* @param value type
* @return a new Map<K,V> instance
*/
public static Map orderedMap() {
return new LinkedHashMap();
}
/**
* Returns a new {@code SortedMap} object. Key type {@code K} must
* implement {@linkplain Comparable}. The returned object is a
* {@link TreeMap}.
*
* Example of use:
*
* Map<Foo, Bar> myMap = CollectionFactory.sortedMap();
*
*
* @param key type
* @param value type
* @return a new SortedMap<K,V> instance
*/
public static SortedMap sortedMap() {
return new TreeMap();
}
/**
* Returns a new {@code List} object. The returned object is
* an {@link ArrayList}.
*
* Example of use:
*
* List<Foo> myList = CollectionFactory.list();
*
*
* @param element type
* @return a new List<T> instance
*/
public static List list() {
return new ArrayList();
}
/**
* Returns a new {@code Stack} object.
*
* Example of use:
*
* Stack<Foo> myStack = CollectionFactory.stack();
*
*
* @param element type
* @return a new Stack<T> instance
*/
public static Stack stack() {
return new Stack();
}
/**
* Returns a new {@code Set} instance. The returned object is
* a {@link HashSet}.
*
* Example of use:
*
* Set<Foo> mySet = CollectionFactory.set();
*
*
* @param element type
* @return a new HashSet<T> instance
*/
public static Set set() {
return new HashSet();
}
/**
* Returns a new {@code Set} instance that maintains
* the insertion order of its elements.
*
* Example of use:
*
* Set<Foo> mySet = CollectionFactory.set();
*
*
* @param element type
* @return a new LinkedHashSet<T> instance
*/
public static Set orderedSet() {
return new LinkedHashSet();
}
/**
* Returns a new {@code SortedSet} instance. The type {@code T}
* must implement {@linkplain Comparable}. The returned object
* is a {@link TreeSet}.
*
* Example of use:
*
* Set<MyObject> foo = CollectionFactory.sortedSet();
*
*
* @param element type
* @return a new TreeSet<T> instance
*/
public static SortedSet sortedSet() {
return new TreeSet();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy