Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.Wrapper 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.
/*
* 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;
/**
* The Class Wrapper.
*
* @author Haiyang Li
* @param
* @since 0.8
*/
public final class Wrapper {
/** The Constant arrayHashFunction. */
static final ToIntFunction arrayHashFunction = new ToIntFunction() {
@Override
public int applyAsInt(Object value) {
return N.deepHashCode(value);
}
};
/** The Constant arrayEqualsFunction. */
static final BiPredicate arrayEqualsFunction = new BiPredicate() {
@Override
public boolean test(Object t, Object u) {
return N.deepEquals(t, u);
}
};
/** The value. */
private final T value;
/** The hash function. */
private final ToIntFunction super T> hashFunction;
/** The equals function. */
private final BiPredicate super T, ? super T> equalsFunction;
/** The to string function. */
private final Function super T, String> toStringFunction;
/** The hash code. */
private int hashCode;
/**
* Instantiates a new wrapper.
*
* @param value
* @param hashFunction
* @param equalsFunction
*/
private Wrapper(T value, ToIntFunction super T> hashFunction, BiPredicate super T, ? super T> equalsFunction) {
this(value, hashFunction, equalsFunction, null);
}
/**
* Instantiates a new wrapper.
*
* @param value
* @param hashFunction
* @param equalsFunction
* @param toStringFunction
*/
private Wrapper(T value, ToIntFunction super T> hashFunction, BiPredicate super T, ? super T> equalsFunction,
Function super T, String> toStringFunction) {
this.value = value;
this.hashFunction = hashFunction;
this.equalsFunction = equalsFunction;
this.toStringFunction = toStringFunction;
}
/**
*
* @param
* @param array
* @return
*/
public static Wrapper of(T array) {
// return new Wrapper(checkArray(array), arrayHashFunction, arrayEqualsFunction);
return new Wrapper(array, arrayHashFunction, arrayEqualsFunction);
}
/**
*
* @param
* @param value
* @param hashFunction
* @param equalsFunction
* @return
*/
public static Wrapper of(T value, ToIntFunction super T> hashFunction, BiPredicate super T, ? super T> equalsFunction) {
N.checkArgNotNull(hashFunction, "hashFunction");
N.checkArgNotNull(equalsFunction, "equalsFunction");
return new Wrapper(value, hashFunction, equalsFunction);
}
/**
*
* @param
* @param value
* @param hashFunction
* @param equalsFunction
* @param toStringFunction
* @return
*/
public static Wrapper of(T value, ToIntFunction super T> hashFunction, BiPredicate super T, ? super T> equalsFunction,
Function super T, String> toStringFunction) {
N.checkArgNotNull(hashFunction, "hashFunction");
N.checkArgNotNull(equalsFunction, "equalsFunction");
N.checkArgNotNull(toStringFunction, "toStringFunction");
return new Wrapper(value, hashFunction, equalsFunction, toStringFunction);
}
/**
*
* @return
*/
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;
// }
/**
*
* @return
*/
@Override
public int hashCode() {
if (hashCode == 0) {
hashCode = value == null ? 0 : hashFunction.applyAsInt(value);
}
return hashCode;
}
/**
*
* @param obj
* @return true, if successful
*/
@Override
public boolean equals(Object obj) {
return (obj == this) || (obj instanceof Wrapper && equalsFunction.test(((Wrapper) obj).value, value));
}
/**
*
* @return
*/
@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);
}
}
}