
jaitools.CollectionFactory Maven / Gradle / Ivy
/*
* Copyright 2009-2010 Michael Bedward
*
* This file is part of jai-tools.
*
* jai-tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* jai-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with jai-tools. If not, see .
*
*/
package 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:
*
* List<Integer> list1 = CollectionFactory.list();
* List<List<String>> list2 = CollectionFactory.list();
*
*
* @author Michael Bedward
* @since 1.0
* @source $URL: https://jai-tools.googlecode.com/svn/tags/1.0.0/utils/src/main/java/jaitools/CollectionFactory.java $
* @version $Id: CollectionFactory.java 1100 2010-02-10 07:28:08Z michael.bedward $
*/
public class CollectionFactory {
/**
* Create a new {@code Map} instance.
*
* Example of use:
*
* Map<MyObject> foo = CollectionFactory.map();
*
*
* @return a new Map<K,V> instance
*/
public static Map map() {
return new HashMap();
}
/**
* Create a new {@code Map} instance that maintains the
* input order of its key:value pairs.
*
* Example of use:
*
* Map<MyObject> foo = CollectionFactory.orderedMap();
*
*
* @return a new Map<K,V> instance
*/
public static Map orderedMap() {
return new LinkedHashMap();
}
/**
* Create a new {@code SortedMap} instance. Note that type
* {@code T} must implement {@linkplain Comparable}.
*
* Example of use:
*
* Map<MyObject> foo = CollectionFactory.sortedMap();
*
*
* @return a new SortedMap<K,V> instance
*/
public static SortedMap sortedMap() {
return new TreeMap();
}
/**
* Create a new {@code List} instance.
*
* Example of use:
*
* List<MyObject> foo = CollectionFactory.list();
*
*
* @return a new List<T> instance
*/
public static List list() {
return new ArrayList();
}
/**
* Create a new {@code Stack} instance.
*
* Example of use:
*
* Stack<MyObject> foo = CollectionFactory.stack();
*
*
* @return a new Stack<T> instance
*/
public static Stack stack() {
return new Stack();
}
/**
* Create a new {@code Set} instance.
*
* Example of use:
*
* Set<MyObject> foo = CollectionFactory.set();
*
*
* @return a new HashSet<T> instance
*/
public static Set set() {
return new HashSet();
}
/**
* Create a new {@code Set} instance that maintains
* the input order of its elements.
*
* Example of use:
*
* Set<MyObject> foo = CollectionFactory.set();
*
*
* @return a new LinkedHashSet<T> instance
*/
public static Set orderedSet() {
return new LinkedHashSet();
}
/**
* Create a new {@code SortedSet} instance. Note that the type {@code T}
* must implement {@linkplain Comparable}.
*
* Example of use:
*
* Set<MyObject> foo = CollectionFactory.sortedSet();
*
*
* @return a new Set<T> instance
*/
public static SortedSet sortedSet() {
return new TreeSet();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy