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

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

There is a newer version: 1.10.1
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;

/**
 * 
 * @since 0.8
 * 
 * @author Haiyang Li 
 */
public final class Synchronized {
    private final T mutex;

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

        this.mutex = mutex;
    }

    public static  Synchronized on(final T mutex) {
        N.checkArgNotNull(mutex);

        return new Synchronized<>(mutex);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public  void run(final Try.Runnable cmd) throws E {
        N.checkArgNotNull(cmd);

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

    public  R call(final Try.Callable cmd) throws E {
        N.checkArgNotNull(cmd);

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

    public  boolean test(final Try.Predicate predicate) throws E {
        N.checkArgNotNull(predicate);

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

    public  void accept(final Try.Consumer consumer) throws E {
        N.checkArgNotNull(consumer);

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

    public  R apply(final Try.Function function) throws E {
        N.checkArgNotNull(function);

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy