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

io.permazen.util.UnionNavigableSet Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen.util;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.NavigableSet;

/**
 * Provides a read-only view of the union of two or more {@link NavigableSet}s.
 */
class UnionNavigableSet extends AbstractMultiNavigableSet {

    /**
     * Constructor.
     *
     * @param sets the sets to union
     */
    UnionNavigableSet(Collection> sets) {
        super(sets);
    }

    /**
     * Internal constructor.
     *
     * @param sets the sets to union
     * @param comparator common comparator
     * @param bounds range restriction
     * @throws IllegalArgumentException if {@code bounds} is null
     */
    protected UnionNavigableSet(Collection> sets, Comparator comparator, Bounds bounds) {
        super(sets, comparator, bounds);
    }

    @Override
    protected NavigableSet createSubSet(boolean reverse, Bounds newBounds, List> newList) {
        final Comparator newComparator = this.getComparator(reverse);
        return new UnionNavigableSet<>(newList, newComparator, newBounds);
    }

    @Override
    public boolean contains(Object obj) {
        for (NavigableSet set : this.list) {
            if (set.contains(obj))
                return true;
        }
        return false;
    }

    @Override
    public CloseableIterator iterator() {
        final Comparator comparator = this.getComparator(false);
        return new UniqueIterator(
          Iterators.mergeSorted(Lists.transform(this.list, NavigableSet::iterator), comparator),
          comparator);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy