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

com.oracle.coherence.concurrent.atomic.AsyncLocalAtomicReference Maven / Gradle / Ivy

Go to download

Utility classes commonly useful in concurrent programming within a Coherence Cluster.

There is a newer version: 24.03.1
Show newest version
/*
 * Copyright (c) 2020, 2021, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.oracle.coherence.concurrent.atomic;

import com.tangosol.util.function.Remote;

import java.util.concurrent.CompletableFuture;

import java.util.concurrent.atomic.AtomicReference;

import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;

import static java.util.concurrent.CompletableFuture.completedFuture;

/**
 * Local implementation of {@link AsyncAtomicReference } interface,
 * that simply wraps {@code java.util.concurrent.atomic.AtomicReference}
 * instance and returns an already completed future from each method.
 *
 * @param   the type of object referred to by this reference
 *
 * @author Aleks Seovic  2020.12.08
 */
public class AsyncLocalAtomicReference
        implements AsyncAtomicReference
    {
    // ----- constructors ---------------------------------------------------

    /**
     * Construct {@code LocalAtomicReference} instance.
     *
     * @param value  wrapped value
     */
    protected AsyncLocalAtomicReference(AtomicReference value)
        {
        f_value = value;
        }

    // ----- AsyncAtomicReference interface ---------------------------------

    @Override
    public CompletableFuture get()
        {
        return completedFuture(f_value.get());
        }

    @Override
    public CompletableFuture set(V newValue)
        {
        f_value.set(newValue);
        return completedFuture(null);
        }

    @Override
    public CompletableFuture getAndSet(V newValue)
        {
        return completedFuture(f_value.getAndSet(newValue));
        }

    @Override
    public CompletableFuture compareAndSet(V expectedValue, V newValue)
        {
        return completedFuture(f_value.compareAndSet(expectedValue, newValue));
        }

    @Override
    public CompletableFuture getAndUpdate(Remote.UnaryOperator updateFunction)
        {
        return getAndUpdate((UnaryOperator) updateFunction);
        }

    @Override
    public CompletableFuture getAndUpdate(UnaryOperator updateFunction)
        {
        return completedFuture(f_value.getAndUpdate(updateFunction));
        }

    @Override
    public CompletableFuture updateAndGet(Remote.UnaryOperator updateFunction)
        {
        return updateAndGet((UnaryOperator) updateFunction);
        }

    @Override
    public CompletableFuture updateAndGet(UnaryOperator updateFunction)
        {
        return completedFuture(f_value.updateAndGet(updateFunction));
        }

    @Override
    public CompletableFuture getAndAccumulate(V x, Remote.BinaryOperator accumulatorFunction)
        {
        return getAndAccumulate(x, (BinaryOperator) accumulatorFunction);
        }

    @Override
    public CompletableFuture getAndAccumulate(V x, BinaryOperator accumulatorFunction)
        {
        return completedFuture(f_value.getAndAccumulate(x, accumulatorFunction));
        }

    @Override
    public CompletableFuture accumulateAndGet(V x, Remote.BinaryOperator accumulatorFunction)
        {
        return accumulateAndGet(x, (BinaryOperator) accumulatorFunction);
        }

    @Override
    public CompletableFuture accumulateAndGet(V x, BinaryOperator accumulatorFunction)
        {
        return completedFuture(f_value.accumulateAndGet(x, accumulatorFunction));
        }

    @Override
    public CompletableFuture compareAndExchange(V expectedValue, V newValue)
        {
        return completedFuture(f_value.compareAndExchange(expectedValue, newValue));
        }

    // ----- Object methods -------------------------------------------------

    /**
     * Returns the String representation of the current value.
     *
     * @return the String representation of the current value
     */
    @Override
    public String toString()
        {
        return String.valueOf(get().join());
        }

    // ----- data members ---------------------------------------------------

    /**
     * Wrapped atomic value.
     */
    private final AtomicReference f_value;
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy