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

com.ocadotechnology.indexedcache.MappedPredicateIndex Maven / Gradle / Ivy

There is a newer version: 16.6.21
Show newest version
/*
 * Copyright © 2017-2023 Ocado (Ocava)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocadotechnology.indexedcache;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import javax.annotation.CheckForNull;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry;
import com.ocadotechnology.id.Identified;

public class MappedPredicateIndex, R> extends AbstractIndex {

    private final Predicate predicate;
    private final Function mappingFunction;

    private final Multiset where = LinkedHashMultiset.create();
    private final Multiset whereNot = LinkedHashMultiset.create();

    private ImmutableSet distinctWhereSnapshot;
    private ImmutableSet distinctWhereNotSnapshot;

    public MappedPredicateIndex(Predicate predicate, Function mappingFunction) {
        this(null, predicate, mappingFunction);
    }

    public MappedPredicateIndex(@CheckForNull String name, Predicate predicate, Function mappingFunction) {
        super(name);
        this.predicate = predicate;
        this.mappingFunction = mappingFunction;
    }

    @Override
    protected void remove(C object) {
        if (predicate.test(object)) {
            distinctWhereSnapshot = null;
            where.remove(mappingFunction.apply(object));
        } else {
            distinctWhereNotSnapshot = null;
            whereNot.remove(mappingFunction.apply(object));
        }
    }

    @Override
    protected void add(C object) {
        if (predicate.test(object)) {
            distinctWhereSnapshot = null;
            where.add(mappingFunction.apply(object));
        } else {
            distinctWhereNotSnapshot = null;
            whereNot.add(mappingFunction.apply(object));
        }
    }

    public boolean contains(R r) {
        return where.contains(r);
    }

    public Stream streamNonDistinctWhere() {
        return where.stream();
    }

    public Stream streamDistinctWhere() {
        return where.entrySet().stream().map(Entry::getElement);
    }

    public Stream streamNonDistinctWhereNot() {
        return whereNot.stream();
    }

    public Stream streamDistinctWhereNot() {
        return whereNot.entrySet().stream().map(Entry::getElement);
    }

    public ImmutableSet getDistinctWhere() {
        if (distinctWhereSnapshot == null) {
            distinctWhereSnapshot = streamDistinctWhere().collect(ImmutableSet.toImmutableSet());
        }
        return distinctWhereSnapshot;
    }

    public ImmutableSet getDistinctWhereNot() {
        if (distinctWhereNotSnapshot == null) {
            distinctWhereNotSnapshot = streamDistinctWhereNot().collect(ImmutableSet.toImmutableSet());
        }
        return distinctWhereNotSnapshot;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy