org.javimmutable.collections.util.JImmutables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javimmutable-collections Show documentation
Show all versions of javimmutable-collections Show documentation
Library providing immutable/persistent collection classes for
Java. While collections are immutable they provide methods for
adding and removing values by creating new modified copies of
themselves. Each copy shares almost all of its structure with
other copies to minimize memory consumption.
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2013, Burton Computer Corporation
// 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.
//
// Neither the name of the Burton Computer Corporation nor the names
// of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// 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.javimmutable.collections.util;
import org.javimmutable.collections.Cursor;
import org.javimmutable.collections.Cursorable;
import org.javimmutable.collections.JImmutableList;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.JImmutableRandomAccessList;
import org.javimmutable.collections.JImmutableSet;
import org.javimmutable.collections.JImmutableStack;
import org.javimmutable.collections.hash.JImmutableHashMap;
import org.javimmutable.collections.hash.JImmutableHashSet;
import org.javimmutable.collections.list.JImmutableArrayList;
import org.javimmutable.collections.list.JImmutableLinkedStack;
import org.javimmutable.collections.tree.ComparableComparator;
import org.javimmutable.collections.tree.JImmutableTreeMap;
import org.javimmutable.collections.tree.JImmutableTreeSet;
import org.javimmutable.collections.tree_list.JImmutableTreeList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
/**
* This class contains static factory methods to create instances of each of the collection interfaces.
* Overloaded variants are provided for each to pre-populate the created collection with existing values.
* Where possible the empty collection methods return a common singleton instance to save memory. The
* factory methods always return the fastest implementation of each interface (i.e. hash when sort not
* required, trie when random access not required, etc).
*/
public final class JImmutables
{
public static JImmutableStack stack()
{
return JImmutableLinkedStack.of();
}
public static JImmutableStack stack(T... values)
{
return Functions.insertAll(JImmutableLinkedStack.of(), values);
}
public static JImmutableStack stack(Cursor cursor)
{
return Functions.insertAll(JImmutableLinkedStack.of(), cursor);
}
public static JImmutableStack stack(Cursorable cursorable)
{
return Functions.insertAll(JImmutableLinkedStack.of(), cursorable.cursor());
}
public static JImmutableStack stack(Iterator iterator)
{
return Functions.insertAll(JImmutableLinkedStack.of(), iterator);
}
public static JImmutableStack stack(Collection collection)
{
return Functions.insertAll(JImmutableLinkedStack.of(), collection.iterator());
}
public static JImmutableList list()
{
return JImmutableArrayList.of();
}
public static JImmutableList list(T... values)
{
return Functions.insertAll(JImmutableArrayList.of(), values);
}
public static JImmutableList list(Cursor cursor)
{
return Functions.insertAll(JImmutableArrayList.of(), cursor);
}
public static JImmutableList list(Cursorable cursorable)
{
return Functions.insertAll(JImmutableArrayList.of(), cursorable.cursor());
}
public static JImmutableList list(Iterator iterator)
{
return Functions.insertAll(JImmutableArrayList.of(), iterator);
}
public static JImmutableList list(Collection collection)
{
return Functions.insertAll(JImmutableArrayList.of(), collection.iterator());
}
public static JImmutableRandomAccessList ralist()
{
return JImmutableTreeList.of();
}
public static JImmutableRandomAccessList ralist(Cursor cursor)
{
return Functions.insertAll(JImmutableTreeList.of(), cursor);
}
public static JImmutableRandomAccessList ralist(T... values)
{
return Functions.insertAll(JImmutableTreeList.of(), values);
}
public static JImmutableRandomAccessList ralist(Cursorable cursorable)
{
return Functions.insertAll(JImmutableTreeList.of(), cursorable.cursor());
}
public static JImmutableRandomAccessList ralist(Iterator iterator)
{
return Functions.insertAll(JImmutableTreeList.of(), iterator);
}
public static JImmutableRandomAccessList ralist(Collection collection)
{
return Functions.insertAll(JImmutableTreeList.of(), collection.iterator());
}
public static JImmutableMap map()
{
return JImmutableHashMap.of();
}
public static JImmutableMap map(Map map)
{
return Functions.assignAll(JImmutableHashMap.of(), map);
}
public static JImmutableMap map(JImmutableMap map)
{
if (map instanceof JImmutableHashMap) {
return map;
} else {
return Functions.assignAll(JImmutableHashMap.of(), map);
}
}
public static , V> JImmutableMap sortedMap()
{
return JImmutableTreeMap.of();
}
public static , V> JImmutableMap sortedMap(Map map)
{
return Functions.assignAll(JImmutableTreeMap.of(), map);
}
public static , V> JImmutableMap sortedMap(JImmutableMap map)
{
if (map instanceof JImmutableTreeMap) {
JImmutableTreeMap treemap = (JImmutableTreeMap)map;
if (treemap.getComparatorClass().equals(ComparableComparator.class)) {
return map;
}
}
return Functions.assignAll(JImmutableTreeMap.of(), map);
}
public static JImmutableMap sortedMap(Comparator comparator)
{
return JImmutableTreeMap.of(comparator);
}
public static JImmutableMap sortedMap(Comparator comparator,
Map map)
{
return Functions.assignAll(JImmutableTreeMap.of(comparator), map);
}
public static JImmutableMap sortedMap(Comparator comparator,
JImmutableMap map)
{
return Functions.assignAll(JImmutableTreeMap.of(comparator), map);
}
public static JImmutableSet set()
{
return JImmutableHashSet.of();
}
public static JImmutableSet set(Cursor cursor)
{
return Functions.insertAll(JImmutableHashSet.of(), cursor);
}
public static JImmutableSet set(T... values)
{
return Functions.insertAll(JImmutableHashSet.of(), values);
}
public static JImmutableSet set(Cursorable cursorable)
{
return Functions.insertAll(JImmutableHashSet.of(), cursorable.cursor());
}
public static JImmutableSet set(Iterator iterator)
{
return Functions.insertAll(JImmutableHashSet.of(), iterator);
}
public static JImmutableSet set(Collection collection)
{
return Functions.insertAll(JImmutableHashSet.of(), collection.iterator());
}
public static > JImmutableSet sortedSet()
{
return JImmutableTreeSet.of();
}
public static > JImmutableSet sortedSet(T... values)
{
return Functions.insertAll(JImmutableTreeSet.of(), values);
}
public static > JImmutableSet sortedSet(Cursor cursor)
{
return Functions.insertAll(JImmutableTreeSet.of(), cursor);
}
public static > JImmutableSet sortedSet(Cursorable cursorable)
{
return Functions.insertAll(JImmutableTreeSet.of(), cursorable.cursor());
}
public static > JImmutableSet sortedSet(Iterator iterator)
{
return Functions.insertAll(JImmutableTreeSet.of(), iterator);
}
public static > JImmutableSet sortedSet(Collection collection)
{
return Functions.insertAll(JImmutableTreeSet.of(), collection.iterator());
}
public static JImmutableSet sortedSet(Comparator comparator)
{
return JImmutableTreeSet.of(comparator);
}
public static JImmutableSet sortedSet(Comparator comparator,
Cursor cursor)
{
return Functions.insertAll(JImmutableTreeSet.of(comparator), cursor);
}
public static JImmutableSet sortedSet(Comparator comparator,
T... values)
{
return Functions.insertAll(JImmutableTreeSet.of(comparator), values);
}
public static JImmutableSet sortedSet(Comparator comparator,
Cursorable cursorable)
{
return Functions.insertAll(JImmutableTreeSet.of(comparator), cursorable.cursor());
}
public static JImmutableSet sortedSet(Comparator comparator,
Iterator iterator)
{
return Functions.insertAll(JImmutableTreeSet.of(comparator), iterator);
}
public static JImmutableSet sortedSet(Comparator comparator,
Collection collection)
{
return Functions.insertAll(JImmutableTreeSet.of(comparator), collection.iterator());
}
}