org.javimmutable.collections.tree.JImmutableTreeMap 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) 2019, 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.tree;
import org.javimmutable.collections.Func1;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.Proc2;
import org.javimmutable.collections.Proc2Throws;
import org.javimmutable.collections.SplitableIterator;
import org.javimmutable.collections.Sum2;
import org.javimmutable.collections.Sum2Throws;
import org.javimmutable.collections.common.AbstractJImmutableMap;
import org.javimmutable.collections.common.Conditions;
import org.javimmutable.collections.common.StreamConstants;
import org.javimmutable.collections.serialization.JImmutableTreeMapProxy;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@Immutable
public class JImmutableTreeMap
extends AbstractJImmutableMap
implements Serializable
{
@SuppressWarnings("unchecked")
private static final JImmutableTreeMap EMPTY = new JImmutableTreeMap(ComparableComparator.of(), FringeNode.instance());
private static final long serialVersionUID = -121805;
private final Comparator comparator;
private final AbstractNode root;
JImmutableTreeMap(@Nonnull Comparator comparator,
@Nonnull AbstractNode root)
{
this.comparator = comparator;
this.root = root;
}
@SuppressWarnings("unchecked")
@Nonnull
public static , V> JImmutableTreeMap of()
{
return EMPTY;
}
@Nonnull
public static JImmutableTreeMap of(@Nonnull Comparator comparator)
{
return new JImmutableTreeMap<>(comparator, FringeNode.instance());
}
@Nonnull
public static , V> JImmutableMap.Builder builder()
{
return new TreeMapBuilder<>(ComparableComparator.of());
}
@Nonnull
public static JImmutableMap.Builder builder(@Nonnull Comparator comparator)
{
return new TreeMapBuilder<>(comparator);
}
@Nonnull
@Override
public Builder mapBuilder()
{
return new TreeMapBuilder<>(comparator);
}
@Nonnull
public static , V> Collector, ?, JImmutableMap> createMapCollector()
{
return createMapCollector(ComparableComparator.of());
}
@Nonnull
public static Collector, ?, JImmutableMap> createMapCollector(@Nonnull Comparator comparator)
{
return Collector., Builder, JImmutableMap>of(() -> new TreeMapBuilder<>(comparator),
(b, v) -> b.add(v),
(b1, b2) -> b1.add(b2),
b -> b.build(),
Collector.Characteristics.CONCURRENT);
}
@Override
public V getValueOr(K key,
V defaultValue)
{
Conditions.stopNull(key);
return root.get(comparator, key, defaultValue);
}
@Nonnull
@Override
public Holder find(@Nonnull K key)
{
Conditions.stopNull(key);
return root.find(comparator, key);
}
@Nonnull
@Override
public Holder> findEntry(@Nonnull K key)
{
Conditions.stopNull(key);
return root.findEntry(comparator, key);
}
@Nonnull
@Override
public JImmutableTreeMap assign(@Nonnull K key,
V value)
{
Conditions.stopNull(key);
return create(root.assign(comparator, key, value));
}
@Nonnull
@Override
public JImmutableTreeMap update(@Nonnull K key,
@Nonnull Func1, V> generator)
{
Conditions.stopNull(key);
return create(root.update(comparator, key, generator));
}
@Nonnull
@Override
public JImmutableTreeMap delete(@Nonnull K key)
{
Conditions.stopNull(key);
final AbstractNode newRoot = root.delete(comparator, key);
if (newRoot.isEmpty()) {
return deleteAll();
} else {
return create(newRoot);
}
}
@Override
public int size()
{
return root.size();
}
@SuppressWarnings("unchecked")
@Nonnull
@Override
public JImmutableTreeMap deleteAll()
{
return (comparator == ComparableComparator.of()) ? EMPTY : new JImmutableTreeMap<>(comparator, FringeNode.instance());
}
@Override
public int getSpliteratorCharacteristics()
{
return StreamConstants.SPLITERATOR_ORDERED;
}
@Nonnull
@Override
public SplitableIterator> iterator()
{
return root.iterator();
}
@Override
public void forEach(@Nonnull Proc2 proc)
{
root.forEach(proc);
}
@Override
public void forEachThrows(@Nonnull Proc2Throws proc)
throws E
{
root.forEachThrows(proc);
}
@Override
public R reduce(R sum,
@Nonnull Sum2 proc)
{
return root.reduce(sum, proc);
}
@Override
public R reduceThrows(R sum,
@Nonnull Sum2Throws proc)
throws E
{
return root.reduceThrows(sum, proc);
}
@Override
public void checkInvariants()
{
root.checkInvariants(comparator);
}
@Nonnull
public Comparator getComparator()
{
return comparator;
}
@Nonnull
List getKeysList()
{
return keys().stream().collect(Collectors.toList());
}
@Nonnull
private JImmutableTreeMap create(AbstractNode newRoot)
{
if (newRoot == root) {
return this;
} else {
return new JImmutableTreeMap<>(comparator, newRoot);
}
}
private Object writeReplace()
{
return new JImmutableTreeMapProxy(this);
}
}