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;

import com.landawn.abacus.util.function.Callable;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Runnable;

/**
 * 
 * @since 0.8
 * 
 * @author Haiyang Li
 * @deprecated replaced by {@code Fn#sp(Object, Predicate), Fn#sc(Object, Consumer), Fn#sf(Object, Function)}
 */
@Deprecated
public final class Synchronized {
    private final T target;

    Synchronized(final T target) {
        this.target = target;
    }

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

        return new Synchronized<>(target);
    }

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

        return new Runnable() {
            @Override
            public void run() {
                synchronized (target) {
                    cmd.run();
                }
            }
        };
    }

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

        return new Callable() {
            @Override
            public R call() {
                synchronized (target) {
                    return cmd.call();
                }
            }
        };
    }

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

        return new Predicate() {
            @Override
            public boolean test(T t) {
                synchronized (target) {
                    return predicate.test(t);
                }
            }
        };
    }

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

        return new Predicate() {
            @Override
            public boolean test(U t) {
                synchronized (target) {
                    return predicate.test(target, t);
                }
            }
        };
    }

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

        return new Consumer() {
            @Override
            public void accept(U t) {
                synchronized (target) {
                    consumer.accept(t);
                }
            }
        };
    }

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

        return new Consumer() {
            @Override
            public void accept(U t) {
                synchronized (target) {
                    consumer.accept(target, t);
                }
            }
        };
    }

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

        return new Function() {
            @Override
            public R apply(U t) {
                synchronized (target) {
                    return funciton.apply(t);
                }
            }
        };
    }

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

        return new Function() {
            @Override
            public R apply(U t) {
                synchronized (target) {
                    return funciton.apply(target, t);
                }
            }
        };
    }

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

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

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

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

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

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

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

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

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

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