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.Indexed;
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.common.IndexedArray;
import org.javimmutable.collections.common.IndexedList;
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.List;
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 JImmutableArrayList.of(IndexedArray.retained(values));
}
public static JImmutableList list(Cursor cursor)
{
return Functions.insertAll(JImmutableArrayList.of(), cursor);
}
public static JImmutableList list(Indexed cursorable)
{
return list(cursorable, 0, cursorable.size());
}
public static JImmutableList list(Indexed cursorable,
int offset,
int limit)
{
return JImmutableArrayList.of(cursorable, offset, limit);
}
public static JImmutableList list(JImmutableSet cursorable)
{
return list(cursorable.cursor());
}
public static JImmutableList list(Iterator iterator)
{
return Functions.insertAll(JImmutableArrayList.of(), iterator);
}
public static JImmutableList list(List collection)
{
return JImmutableArrayList.of(IndexedList.retained(collection));
}
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());
}
/**
* Constructs an empty unsorted map.
*
* @param
* @param
* @return
*/
public static JImmutableMap map()
{
return JImmutableHashMap.of();
}
/**
* Constructs an unsorted map.
* All key/value pairs from source are copied into the newly created map.
*
* @param source
* @param
* @param
* @return
*/
public static JImmutableMap map(Map source)
{
return Functions.assignAll(JImmutableHashMap.of(), source);
}
/**
* Constructs an unsorted map.
* If source is already an unsorted map it is returned directly, otherwise a new map
* is created and all key/value pairs from source are copied into the newly created map.
*
* @param source
* @param
* @param
* @return
*/
public static JImmutableMap map(JImmutableMap source)
{
if (source instanceof JImmutableHashMap) {
return source;
} else {
return Functions.assignAll(JImmutableHashMap.of(), source);
}
}
/**
* Constructs an empty map that sorts keys in their natural sort order (using ComparableComparator).
*/
public static , V> JImmutableMap sortedMap()
{
return JImmutableTreeMap.of();
}
/**
* Constructs a map that sorts keys in their natural sort order (using ComparableComparator).
* All key/value pairs from source are copied into the newly created map.
*
* @param source java.util.Map containing starting key/value pairs
*/
public static , V> JImmutableMap sortedMap(Map source)
{
return Functions.assignAll(JImmutableTreeMap.of(), source);
}
/**
* Constructs a map that sorts keys in their natural sort order (using ComparableComparator).
* All key/value pairs from source are copied into the newly created map.
* If source is already a sorted map using the natural sort order it will be returned directly
* (effectively performing a simple cast).
*
* @param source JImmutableMap containing starting key/value pairs
*/
public static , V> JImmutableMap sortedMap(JImmutableMap source)
{
return sortedMap(ComparableComparator.of(), source);
}
/**
* Constructs a map that sorts keys using the specified Comparator.
*
* Note that the Comparator MUST BE IMMUTABLE.
* The Comparator will be retained and used throughout the life of the map and its offspring and will
* be aggressively shared so it is imperative that the Comparator be completely immutable.
*
* All key/value pairs from map are copied into the newly created map.
*/
public static JImmutableMap sortedMap(Comparator comparator)
{
return JImmutableTreeMap.of(comparator);
}
/**
* Constructs a map that sorts keys using the specified Comparator.
*
* Note that the Comparator MUST BE IMMUTABLE.
* The Comparator will be retained and used throughout the life of the map and its offspring and will
* be aggressively shared so it is imperative that the Comparator be completely immutable.
*
* All key/value pairs from source are copied into the newly created map.
*
* @param source java.util.Map containing starting key/value pairs
*/
public static JImmutableMap sortedMap(Comparator comparator,
Map source)
{
return Functions.assignAll(JImmutableTreeMap.of(comparator), source);
}
/**
* Constructs a map that sorts keys using the specified Comparator.
*
* Note that the Comparator MUST BE IMMUTABLE.
* The Comparator will be retained and used throughout the life of the map and its offspring and will
* be aggressively shared so it is imperative that the Comparator be completely immutable.
*
* If source is already a sorted map that uses the same comparator (as indicated by comparator.equals())
* then source will be returned directly. Otherwise all key/value pairs from source are copied into
* the newly created map.
*
* @param source JImmutableMap containing starting key/value pairs
*/
public static JImmutableMap sortedMap(Comparator comparator,
JImmutableMap source)
{
if (source instanceof JImmutableTreeMap) {
JImmutableTreeMap treemap = (JImmutableTreeMap)source;
if (treemap.getComparator().equals(comparator)) {
return source;
}
}
return Functions.assignAll(JImmutableTreeMap.of(comparator), source);
}
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());
}
}