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

com.landawn.abacus.util.Synchronized Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.2.4
Show newest version
/*
 * Copyright (C) 2016 HaiYang Li
 *
 * 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.landawn.abacus.util;

/**
 *
 * @author Haiyang Li
 * @param 
 * @since 0.8
 */
@SuppressWarnings("java:S2445")
public final class Synchronized {

    private final T mutex;

    Synchronized(final T mutex) {
        N.checkArgNotNull(mutex);

        this.mutex = mutex;
    }

    /**
     *
     * @param 
     * @param mutex
     * @return
     */
    public static  Synchronized on(final T mutex) {
        N.checkArgNotNull(mutex);

        return new Synchronized<>(mutex);
    }

    /**
     *
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param cmd
     * @throws E the e
     */
    public static  void run(final T mutex, final Throwables.Runnable cmd) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(cmd);

        synchronized (mutex) {
            cmd.run();
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param cmd
     * @return
     * @throws E the e
     */
    public static  R call(final T mutex, final Throwables.Callable cmd) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(cmd);

        synchronized (mutex) {
            return cmd.call();
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param predicate
     * @return
     * @throws E the e
     */
    public static  boolean test(final T mutex, final Throwables.Predicate predicate) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(predicate);

        synchronized (mutex) {
            return predicate.test(mutex);
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param u
     * @param predicate
     * @return
     * @throws E the e
     */
    public static  boolean test(final T mutex, final U u, final Throwables.BiPredicate predicate) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(predicate);

        synchronized (mutex) {
            return predicate.test(mutex, u);
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param consumer
     * @throws E the e
     */
    public static  void accept(final T mutex, final Throwables.Consumer consumer) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(consumer);

        synchronized (mutex) {
            consumer.accept(mutex);
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param u
     * @param consumer
     * @throws E the e
     */
    public static  void accept(final T mutex, final U u, final Throwables.BiConsumer consumer) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(consumer);

        synchronized (mutex) {
            consumer.accept(mutex, u);
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param funciton
     * @return
     * @throws E the e
     */
    public static  R apply(final T mutex, final Throwables.Function funciton) throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(funciton);

        synchronized (mutex) {
            return funciton.apply(mutex);
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param 
     * @param 
     * @param mutex to locked on.
     * @param u
     * @param funciton
     * @return
     * @throws E the e
     */
    public static  R apply(final T mutex, final U u, final Throwables.BiFunction funciton)
            throws E {
        N.checkArgNotNull(mutex);
        N.checkArgNotNull(funciton);

        synchronized (mutex) {
            return funciton.apply(mutex, u);
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @throws E the e
     */
    public  void run(final Throwables.Runnable cmd) throws E {
        N.checkArgNotNull(cmd);

        synchronized (mutex) {
            cmd.run();
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param cmd
     * @return
     * @throws E the e
     */
    public  R call(final Throwables.Callable cmd) throws E {
        N.checkArgNotNull(cmd);

        synchronized (mutex) {
            return cmd.call();
        }
    }

    /**
     *
     * @param 
     * @param predicate
     * @return
     * @throws E the e
     */
    public  boolean test(final Throwables.Predicate predicate) throws E {
        N.checkArgNotNull(predicate);

        synchronized (mutex) {
            return predicate.test(mutex);
        }
    }

    /**
     *
     * @param 
     * @param consumer
     * @throws E the e
     */
    public  void accept(final Throwables.Consumer consumer) throws E {
        N.checkArgNotNull(consumer);

        synchronized (mutex) {
            consumer.accept(mutex);
        }
    }

    /**
     *
     * @param 
     * @param 
     * @param function
     * @return
     * @throws E the e
     */
    public  R apply(final Throwables.Function function) throws E {
        N.checkArgNotNull(function);

        synchronized (mutex) {
            return function.apply(mutex);
        }
    }
}