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.collect.AbstractIterator;
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 {@link NavigableSet}s.
 */
class DifferenceNavigableSet extends AbstractMultiNavigableSet {

    /**
     * Constructor.
     *
     * @param set1 first set
     * @param set2 second set
     */
    @SuppressWarnings("unchecked")
    DifferenceNavigableSet(NavigableSet set1, NavigableSet set2) {
        super(Lists.newArrayList(set1, set2));
    }

    /**
     * Internal constructor.
     *
     * @param set1 first set
     * @param set2 second set
     * @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 CloseableIterator iterator() {
        return new Iter<>(this.list.get(0), this.list.get(1));
    }

// Iter

    private static class Iter extends AbstractIterator implements CloseableIterator {

        private final Iterator iter1;
        private final NavigableSet set2;

        Iter(NavigableSet set1, NavigableSet set2) {
            this.iter1 = set1.iterator();
            this.set2 = set2;
        }

        @Override
        protected E computeNext() {
            while (this.iter1.hasNext()) {
                final E next = this.iter1.next();
                if (!this.set2.contains(next))
                    return next;
            }
            return this.endOfData();
        }

        @Override
        public void close() {
            if (this.iter1 instanceof CloseableIterator)
                ((CloseableIterator)this.iter1).close();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy