forked from plusone/plusone-commons
148 lines
3.7 KiB
Java
148 lines
3.7 KiB
Java
/*
|
|
* Copyright 2024 the original author or authors.
|
|
*
|
|
* 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
|
|
*
|
|
* https://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 xyz.zhouxy.plusone.commons.base;
|
|
|
|
import java.util.Objects;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Function;
|
|
import java.util.function.Predicate;
|
|
import java.util.function.UnaryOperator;
|
|
|
|
import com.google.common.annotations.Beta;
|
|
|
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
|
|
|
@Beta
|
|
public final class Ref<T> {
|
|
|
|
private T value;
|
|
|
|
public Ref() {
|
|
this.value = null;
|
|
}
|
|
|
|
public Ref(T value) {
|
|
this.value = value;
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static <T> Ref<T> of() {
|
|
return new Ref<>();
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static <T> Ref<T> of(T value) {
|
|
return new Ref<>(value);
|
|
}
|
|
|
|
public T getValue() {
|
|
return value;
|
|
}
|
|
|
|
public void setValue(T value) {
|
|
this.value = value;
|
|
}
|
|
|
|
public void transformValue(UnaryOperator<T> operator) {
|
|
this.value = operator.apply(this.value);
|
|
}
|
|
|
|
public <R> Ref<R> transform(Function<? super T, R> function) {
|
|
return Ref.of(function.apply(this.value));
|
|
}
|
|
|
|
public boolean checkValue(Predicate<? super T> predicate) {
|
|
return predicate.test(this.value);
|
|
}
|
|
|
|
public void execute(Consumer<? super T> consumer) {
|
|
consumer.accept(value);
|
|
}
|
|
|
|
public boolean isNull() {
|
|
return this.value == null;
|
|
}
|
|
|
|
public boolean isNotNull() {
|
|
return this.value != null;
|
|
}
|
|
|
|
// ================================
|
|
// #region - from
|
|
// ================================
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static Ref<Boolean> from(BoolRef boolRef) {
|
|
return new Ref<>(boolRef.getValue());
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static Ref<Byte> from(ByteRef byteRef) {
|
|
return new Ref<>(byteRef.getValue());
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static Ref<Character> from(CharRef charRef) {
|
|
return new Ref<>(charRef.getValue());
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static Ref<Double> from(DoubleRef doubleRef) {
|
|
return new Ref<>(doubleRef.getValue());
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static Ref<Integer> from(IntRef intRef) {
|
|
return new Ref<>(intRef.getValue());
|
|
}
|
|
|
|
@StaticFactoryMethod(Ref.class)
|
|
public static Ref<Long> from(LongRef longRef) {
|
|
return new Ref<>(longRef.getValue());
|
|
}
|
|
|
|
// ================================
|
|
// #endregion - from
|
|
// ================================
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("Ref[%s]", value);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
final int prime = 31;
|
|
int result = 1;
|
|
result = prime * result + ((value == null) ? 0 : value.hashCode());
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
Ref<?> other = (Ref<?>) obj;
|
|
return Objects.equals(this.value, other.value);
|
|
}
|
|
|
|
}
|