All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javersion.util.PersistentHashSet Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 Samppa Saarela
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.javersion.util;


import java.util.Objects;
import java.util.Spliterator;
import java.util.stream.Collectors;

import javax.annotation.concurrent.Immutable;

@Immutable
public class PersistentHashSet extends AbstractTrieSet> implements PersistentSet {

    private final Node> root;

    private final int size;

    public PersistentHashSet() {
        this(null, 0);
    }

    @SuppressWarnings("unchecked")
    PersistentHashSet(Node> root, int size) {
        this.root = root != null ? root : EMPTY_NODE;
        this.size = size;
    }

    @Override
    public MutableHashSet toMutableSet() {
        return new MutableHashSet(root, size);
    }

    @Override
    public ImmutableSet asSet() {
        return new ImmutableSet(this);
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Spliterator spliterator() {
        return new ElementSpliterator(root, size, true);
    }

    @Override
    protected PersistentHashSet doReturn(Node> newRoot, int newSize) {
        if (newRoot == root) {
            return this;
        }
        return new PersistentHashSet<>(newRoot, newSize);
    }

    @Override
    protected Node> root() {
        return root;
    }

    @Override
    public String toString() {
        return stream().map(Objects::toString).collect(Collectors.joining(", ", "[", "]"));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy