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

org.neo4j.collection.diffset.MutableDiffSetsImpl Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
/*
 * 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 static java.lang.String.format;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Predicate;
import org.neo4j.collection.trackable.HeapTrackingCollections;
import org.neo4j.internal.helpers.collection.Iterables;
import org.neo4j.internal.helpers.collection.Iterators;
import org.neo4j.memory.EmptyMemoryTracker;
import org.neo4j.memory.MemoryTracker;

public class MutableDiffSetsImpl implements MutableDiffSets {
    private final MemoryTracker memoryTracker;
    private Set addedElements;
    private Set removedElements;
    private Predicate filter;

    private MutableDiffSetsImpl(Set addedElements, Set removedElements, MemoryTracker memoryTracker) {
        this.addedElements = addedElements;
        this.removedElements = removedElements;
        this.memoryTracker = memoryTracker;
    }

    public static  MutableDiffSets newMutableDiffSets(MemoryTracker memoryTracker) {
        return new MutableDiffSetsImpl<>(null, null, memoryTracker);
    }

    @Override
    public boolean add(T elem) {
        boolean wasRemoved = removed(false).remove(elem);
        // Add to the addedElements only if it was not removed from the removedElements
        return wasRemoved || added(true).add(elem);
    }

    @Override
    public boolean remove(T elem) {
        boolean removedFromAddedElements = added(false).remove(elem);
        // Add to the removedElements only if it was not removed from the addedElements.
        return removedFromAddedElements || removed(true).add(elem);
    }

    @Override
    public boolean unRemove(T item) {
        return removed(false).remove(item);
    }

    @Override
    public String toString() {
        return format("{+%s, -%s}", added(false), removed(false));
    }

    @Override
    public boolean isAdded(T elem) {
        return added(false).contains(elem);
    }

    @Override
    public boolean isRemoved(T elem) {
        return removed(false).contains(elem);
    }

    @Override
    public Set getAdded() {
        return resultSet(addedElements);
    }

    @Override
    public Set getRemoved() {
        return resultSet(removedElements);
    }

    @Override
    public boolean isEmpty() {
        return added(false).isEmpty() && removed(false).isEmpty();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Iterator apply(Iterator source) {
        Iterator result = (Iterator) source;
        if ((removedElements != null && !removedElements.isEmpty())
                || (addedElements != null && !addedElements.isEmpty())) {
            if (filter == null) {
                filter = item -> !removed(false).contains(item) && !added(false).contains(item);
            }
            result = Iterators.filter(filter, result);
        }
        if (addedElements != null && !addedElements.isEmpty()) {
            result = Iterators.concat(result, addedElements.iterator());
        }
        return result;
    }

    @Override
    public MutableDiffSetsImpl filterAdded(Predicate addedFilter) {
        return new MutableDiffSetsImpl<>(
                Iterables.asSet(Iterables.filter(addedFilter, added(false))),
                Iterables.asSet(removed(false)),
                EmptyMemoryTracker.INSTANCE); // no tracker, hopefully a temporary copy
    }

    private Set added(boolean create) {
        if (addedElements == null) {
            if (!create) {
                return Collections.emptySet();
            }
            addedElements = HeapTrackingCollections.newSet(memoryTracker);
        }
        return addedElements;
    }

    private Set removed(boolean create) {
        if (removedElements == null) {
            if (!create) {
                return Collections.emptySet();
            }
            removedElements = HeapTrackingCollections.newSet(memoryTracker);
        }
        return removedElements;
    }

    private Set resultSet(Set coll) {
        return coll == null ? Collections.emptySet() : Collections.unmodifiableSet(coll);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy