Atomically replaces the array in instance with a new array that doesn't contain the given value or returns false if the instance holds the terminal state or an empty array or the value is not in the array. - Java Collection Framework

Java examples for Collection Framework:Array Contain

Description

Atomically replaces the array in instance with a new array that doesn't contain the given value or returns false if the instance holds the terminal state or an empty array or the value is not in the array.

Demo Code

/**/* w  w w  .j  a  v a  2  s . c  o m*/
 * Copyright 2015 David Karnok and Netflix, Inc.
 * 
 * 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.java2s;
import java.util.concurrent.atomic.*;
import java.util.function.*;

public class Main {
    /**
     * Atomically replaces the array in instance with a new array that doesn't contain the
     * given value or returns false if the instance holds the terminal state or an empty array
     * or the value is not in the array.
     * @param updater
     * @param instance
     * @param value
     * @param terminalValue
     * @param zeroArray
     * @param arraySupplier
     * @return
     */
    public static <T, U> boolean remove(
            AtomicReferenceFieldUpdater<T, U[]> updater, T instance,
            U value, U[] terminalValue, U[] zeroArray,
            IntFunction<U[]> arraySupplier) {
        for (;;) {
            U[] a = updater.get(instance);
            if (a == terminalValue) {
                return false;
            }
            int n = a.length;
            if (n == 0) {
                return false;
            }
            int j = -1;
            for (int i = 0; i < n; i++) {
                if (a[i] == value) {
                    j = i;
                    break;
                }
            }
            if (j < 0) {
                return false;
            }
            U[] b;
            if (n == 1) {
                b = zeroArray;
            } else {
                b = arraySupplier.apply(n - 1);
                System.arraycopy(a, 0, b, 0, j);
                System.arraycopy(a, j + 1, b, j, n - j - 1);
            }
            if (updater.compareAndSet(instance, a, b)) {
                return true;
            }
        }
    }

    /**
     * Atomically replaces the array in instance with a new array that doesn't contain the
     * given value or returns false if the instance holds the terminal state or an empty array
     * or the value is not in the array.
     * @param reference
     * @param value
     * @param terminalValue
     * @param zeroArray
     * @param arraySupplier
     * @return
     */
    public static <U> boolean remove(AtomicReference<U[]> reference,
            U value, U[] terminalValue, U[] zeroArray,
            IntFunction<U[]> arraySupplier) {
        for (;;) {
            U[] a = reference.get();
            if (a == terminalValue) {
                return false;
            }
            int n = a.length;
            if (n == 0) {
                return false;
            }
            int j = -1;
            for (int i = 0; i < n; i++) {
                if (a[i] == value) {
                    j = i;
                    break;
                }
            }
            if (j < 0) {
                return false;
            }
            U[] b;
            if (n == 1) {
                b = zeroArray;
            } else {
                b = arraySupplier.apply(n - 1);
                System.arraycopy(a, 0, b, 0, j);
                System.arraycopy(a, j + 1, b, j, n - j - 1);
            }
            if (reference.compareAndSet(a, b)) {
                return true;
            }
        }
    }
}

Related Tutorials