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

org.apache.cassandra.utils.btree.UpdateFunction Maven / Gradle / Ivy

There is a newer version: 3.11.12.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.cassandra.utils.btree;

import java.util.function.BiFunction;

import com.google.common.base.Function;
/**
 * An interface defining a function to be applied to both the object we are replacing in a BTree and
 * the object that is intended to replace it, returning the object to actually replace it.
 */
public interface UpdateFunction extends Function
{
    /**
     * @param replacing the value in the original tree we have matched
     * @param update the value in the updating collection that matched
     * @return the value to insert into the new tree
     */
    V apply(V replacing, K update);

    /**
     * @return true if we should fail the update
     */
    boolean abortEarly();

    /**
     * @param heapSize extra heap space allocated (over previous tree)
     */
    void allocated(long heapSize);

    public static final class Simple implements UpdateFunction
    {
        private final BiFunction wrapped;
        public Simple(BiFunction wrapped)
        {
            this.wrapped = wrapped;
        }

        public V apply(V v) { return v; }
        public V apply(V replacing, V update) { return wrapped.apply(replacing, update); }
        public boolean abortEarly() { return false; }
        public void allocated(long heapSize) { }

        public static  Simple of(BiFunction f)
        {
            return new Simple<>(f);
        }
    }

    static final Simple noOp = Simple.of((a, b) -> a);

    public static  UpdateFunction noOp()
    {
        return (UpdateFunction) noOp;
    }
}