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.BiFunction;
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 BiFunction arrayEqualsFunction = new BiFunction() {
        @Override
        public Boolean apply(Object t, Object u) {
            return N.deepEquals(t, u);
        }
    };

    private final T value;
    private final ToIntFunction hashFunction;
    private final BiFunction equalsFunction;
    private int hashCode;

    private Wrapper(T value, ToIntFunction hashFunction, BiFunction equalsFunction) {
        this.value = value;
        this.hashFunction = hashFunction;
        this.equalsFunction = equalsFunction;
    }

    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, BiFunction equalsFunction) {
        return new Wrapper(value, hashFunction, equalsFunction);
    }

    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.apply(((Wrapper) obj).value, value));

    }

    @Override
    public String toString() {
        return "Wrapper of Object: " + N.toString(value);
    }
}