org.neo4j.collection.diffset.DiffSets Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-collections Show documentation
Show all versions of neo4j-collections Show documentation
Collections and collection utilities for Neo4j.
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.collection.diffset;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Predicate;
/**
* Given a sequence of add and removal operations, instances of DiffSets track
* which elements need to actually be added and removed at minimum from some
* hypothetical target collection such that the result is equivalent to just
* executing the sequence of additions and removals in order
*
* @param type of elements
*/
public interface DiffSets {
boolean isAdded(T elem);
boolean isRemoved(T elem);
Set getAdded();
Set getRemoved();
boolean isEmpty();
Iterator apply(Iterator extends T> source);
DiffSets filterAdded(Predicate addedFilter);
final class Empty implements DiffSets {
@SuppressWarnings("unchecked")
public static DiffSets instance() {
return (DiffSets) INSTANCE;
}
@SuppressWarnings("unchecked")
public static DiffSets ifNull(DiffSets diffSets) {
return diffSets == null ? (DiffSets) INSTANCE : diffSets;
}
private static final DiffSets> INSTANCE = new Empty<>();
private Empty() {
// singleton
}
@Override
public boolean isAdded(T elem) {
return false;
}
@Override
public boolean isRemoved(T elem) {
return false;
}
@Override
public Set getAdded() {
return Collections.emptySet();
}
@Override
public Set getRemoved() {
return Collections.emptySet();
}
@Override
public boolean isEmpty() {
return true;
}
@SuppressWarnings("unchecked")
@Override
public Iterator apply(Iterator extends T> source) {
return (Iterator) source;
}
@Override
public DiffSets filterAdded(Predicate addedFilter) {
return this;
}
}
}