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

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

There is a newer version: 1.10.1
Show newest version
/*
 * Copyright (c) 2015, 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.BiPredicate;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ToIntFunction;

/**
 * 
 * @since 0.8
 * 
 * @author Haiyang Li
 */
public final class Wrapper {
    static final ToIntFunction arrayHashFunction = new ToIntFunction() {
        @Override
        public int applyAsInt(Object value) {
            return N.deepHashCode(value);
        }
    };

    static final BiPredicate arrayEqualsFunction = new BiPredicate() {
        @Override
        public boolean test(Object t, Object u) {
            return N.deepEquals(t, u);
        }
    };

    private final T value;
    private final ToIntFunction hashFunction;
    private final BiPredicate equalsFunction;
    private final Function toStringFunction;
    private int hashCode;

    private Wrapper(T value, ToIntFunction hashFunction, BiPredicate equalsFunction) {
        this(value, hashFunction, equalsFunction, null);
    }

    private Wrapper(T value, ToIntFunction hashFunction, BiPredicate equalsFunction,
            Function toStringFunction) {
        this.value = value;
        this.hashFunction = hashFunction;
        this.equalsFunction = equalsFunction;
        this.toStringFunction = toStringFunction;

    }

    public static  Wrapper of(T array) {
        // return new Wrapper(checkArray(array), arrayHashFunction, arrayEqualsFunction);
        return new Wrapper(array, arrayHashFunction, arrayEqualsFunction);
    }

    public static  Wrapper of(T value, ToIntFunction hashFunction, BiPredicate equalsFunction) {
        N.checkArgNotNull(hashFunction, "hashFunction");
        N.checkArgNotNull(equalsFunction, "equalsFunction");

        return new Wrapper(value, hashFunction, equalsFunction);
    }

    public static  Wrapper of(T value, ToIntFunction hashFunction, BiPredicate equalsFunction,
            Function toStringFunction) {
        N.checkArgNotNull(hashFunction, "hashFunction");
        N.checkArgNotNull(equalsFunction, "equalsFunction");
        N.checkArgNotNull(toStringFunction, "toStringFunction");

        return new Wrapper(value, hashFunction, equalsFunction, toStringFunction);
    }

    public T value() {
        return value;
    }

    //    static  T checkArray(T a) {
    //        if (a != null && a.getClass().isArray() == false) {
    //            throw new IllegalArgumentException(a.getClass().getCanonicalName() + " is not array type");
    //        }
    //
    //        return a;
    //    }

    @Override
    public int hashCode() {
        if (hashCode == 0) {
            hashCode = value == null ? 0 : hashFunction.applyAsInt(value);
        }

        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        return (obj == this) || (obj instanceof Wrapper && equalsFunction.test(((Wrapper) obj).value, value));
    }

    @Override
    public String toString() {
        if (toStringFunction == null) {
            if (value == null) {
                return "Wrapper[null]";
            } else {
                return String.format("Wrapper[%s]", N.toString(value));
            }
        } else {
            return toStringFunction.apply(value);
        }
    }
}