com.landawn.abacus.util.Holder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2022, 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 java.util.function.Supplier;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.stream.Stream;
/**
* The Class Holder.
*
* @param
*/
public final class Holder implements Mutable {
private T value;
/**
* Instantiates a new holder.
*/
public Holder() {
this(null);
}
/**
* Instantiates a new holder.
*
* @param value
*/
Holder(T value) {
this.value = value;
}
/**
*
* @param
* @param value
* @return
*/
public static Holder of(T value) {
return new Holder<>(value);
}
/**
*
* @return
*/
public T value() {
return value;
}
/**
* Gets the value.
*
* @return
* @deprecated replace by {@link #value()}.
*/
@Deprecated
public T getValue() {
return value;
}
/**
* Sets the value.
*
* @param value the new value
*/
public void setValue(final T value) {
this.value = value;
}
/**
* Gets the and set.
*
* @param value
* @return
*/
public T getAndSet(final T value) {
final T result = this.value;
this.value = value;
return result;
}
/**
* Sets the and get.
*
* @param value
* @return
*/
public T setAndGet(final T value) {
this.value = value;
return this.value;
}
/**
* Gets the and update.
*
* @param
* @param updateFunction
* @return
* @throws E the e
*/
public T getAndUpdate(final Throwables.UnaryOperator updateFunction) throws E {
final T res = value;
this.value = updateFunction.apply(value);
return res;
}
/**
* Update and get.
*
* @param
* @param updateFunction
* @return
* @throws E the e
*/
public T updateAndGet(final Throwables.UnaryOperator updateFunction) throws E {
this.value = updateFunction.apply(value);
return value;
}
/**
* Set with the specified new value and returns true
if predicate
returns true.
* Otherwise just return false
without setting the value to new value.
*
* @param
* @param newValue
* @param predicate - test the current value.
* @return
* @throws E the e
*/
public boolean setIf(final T newValue, final Throwables.Predicate predicate) throws E {
if (predicate.test(value)) {
this.value = newValue;
return true;
}
return false;
}
/**
* Set with the specified new value and returns true
if predicate
returns true.
* Otherwise just return false
without setting the value to new value.
*
* @param
* @param newValue
* @param predicate the first parameter is the current value, the second parameter is the new value.
* @return
* @throws E the e
* @deprecated
*/
@Deprecated
public boolean setIf(final T newValue, final Throwables.BiPredicate predicate) throws E {
if (predicate.test(value, newValue)) {
this.value = newValue;
return true;
}
return false;
}
/**
* Checks if is null.
*
* @return true, if is null
*/
public boolean isNull() {
return value == null;
}
/**
* Checks if is not null.
*
* @return true, if is not null
*/
public boolean isNotNull() {
return value != null;
}
/**
* If not null.
*
* @param
* @param action
* @throws E the e
*/
public void ifNotNull(final Throwables.Consumer action) throws E {
N.checkArgNotNull(action, "action"); //NOSONAR
if (isNotNull()) {
action.accept(value);
}
}
/**
* If not null or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @throws E the e
* @throws E2 the e2
*/
public void ifNotNullOrElse(final Throwables.Consumer action,
final Throwables.Runnable emptyAction) throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isNotNull()) {
action.accept(value);
} else {
emptyAction.run();
}
}
/**
*
* @param
* @param action
* @throws E the e
*/
public void accept(final Throwables.Consumer action) throws E {
action.accept(value);
}
/**
* Accept if not null.
*
* @param
* @param action
* @throws E the e
* @deprecated replaced by {@link #ifNotNull(Throwables.Consumer)}
*/
@Deprecated
public void acceptIfNotNull(final Throwables.Consumer action) throws E {
N.checkArgNotNull(action, "action");
if (isNotNull()) {
action.accept(value);
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws E the e
*/
public U map(final Throwables.Function mapper) throws E {
return mapper.apply(value);
}
/**
* Map if not null.
*
* @param
* @param
* @param mapper
* @return
* @throws E the e
*/
public Nullable mapIfNotNull(final Throwables.Function mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isNotNull()) {
return Nullable.of((U) mapper.apply(value));
} else {
return Nullable. empty();
}
}
/**
*
*
* @param
* @param
* @param mapper
* @return
* @throws E
*/
public Optional mapToNonNullIfNotNull(final Throwables.Function mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isNotNull()) {
return Optional.of((U) mapper.apply(value));
} else {
return Optional. empty();
}
}
/**
*
* @param
* @param predicate
* @return
* @throws E the e
*/
public Nullable filter(final Throwables.Predicate predicate) throws E {
if (predicate.test(value)) {
return Nullable.of(value);
} else {
return Nullable. empty();
}
}
/**
* Filter if not null.
*
* @param
* @param predicate
* @return
* @throws E the e
*/
public Optional filterIfNotNull(final Throwables.Predicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isNotNull() && predicate.test(value)) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
/**
* Or else if null.
*
* @param other
* @return
*/
public T orElseIfNull(T other) {
return isNotNull() ? value : other;
}
/**
* Or else get if null.
*
* @param
* @param other
* @return
* @throws E the e
*/
public T orElseGetIfNull(final Throwables.Supplier other) throws E {
N.checkArgNotNull(other, "other");
if (isNotNull()) {
return value;
} else {
return other.get();
}
}
/**
* Or else throw if null.
*
* @param
* @param exceptionSupplier
* @return
* @throws X the x
*/
public T orElseThrowIfNull(final Supplier exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isNotNull()) {
return value;
} else {
throw exceptionSupplier.get();
}
}
/**
*
* @return
*/
public Stream stream() {
return Stream.of(value);
}
/**
* Returns a {@code Stream} with the {@code value} if {@code value} is not null, otherwise an empty {@code Stream} is returned.
*
* @return
*/
public Stream streamIfNotNull() {
if (isNotNull()) {
return Stream.of(value);
} else {
return Stream. empty();
}
}
/**
* Returns a non-empty {@code Nullable} with the {@code value}.
*
* @return
*/
public Nullable toNullable() {
return Nullable.of(value);
}
/**
* Returns an {@code Optional} with the {@code value} if {@code value} is not null, otherwise an empty {@code Optional} is returned.
*
* @return
*/
public Optional toOptional() {
return Optional.ofNullable(value);
}
/**
*
* @return
*/
@Override
public int hashCode() {
return (value == null) ? 0 : value.hashCode();
}
/**
*
* @param obj
* @return
*/
@SuppressWarnings("rawtypes")
@Override
public boolean equals(final Object obj) {
return this == obj || (obj instanceof Holder && N.equals(((Holder) obj).value, value));
}
/**
*
* @return
*/
@Override
public String toString() {
if (value == null) {
return "Holder[null]";
} else {
return String.format("Holder[%s]", N.toString(value));
}
}
}