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

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

There is a newer version: 5.1.0
Show newest version

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

package io.permazen.util;

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

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

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

    /**
     * Constructor.
     *
     * @param sets the sets to difference
     */
    @SuppressWarnings("unchecked")
    DifferenceNavigableSet(NavigableSet set1, NavigableSet set2) {
        super(Lists.newArrayList(set1, set2));
    }

    /**
     * Internal constructor.
     *
     * @param sets the sets to difference
     * @param comparator common comparator
     * @param bounds range restriction
     * @throws IllegalArgumentException if {@code bounds} is null
     */
    @SuppressWarnings("unchecked")
    protected DifferenceNavigableSet(NavigableSet set1, NavigableSet set2,
      Comparator comparator, Bounds bounds) {
        super(Lists.newArrayList(set1, set2), comparator, bounds);
    }

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

    @Override
    public boolean contains(Object obj) {
        return this.list.get(0).contains(obj) && !this.list.get(1).contains(obj);
    }

    @Override
    public Iterator iterator() {
        return Iterators.filter(this.list.get(0).iterator(), Predicates.not(Predicates.in(this.list.get(1))));
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy