
com.github.tonivade.purefun.data.NonEmptyString Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of purefun-core Show documentation
Show all versions of purefun-core Show documentation
Functional Programming Library for Java
The newest version!
/*
* Copyright (c) 2018-2024, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun.data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import com.github.tonivade.purefun.core.Equal;
import com.github.tonivade.purefun.core.Function1;
import com.github.tonivade.purefun.core.Operator1;
import static com.github.tonivade.purefun.type.Validation.requireNonEmpty;
public class NonEmptyString implements Serializable {
@Serial
private static final long serialVersionUID = -4000125976618351707L;
private static final Equal EQUAL = Equal.of().comparing(x -> x.value);
private final String value;
private NonEmptyString(String value) {
this.value = value;
}
public NonEmptyString map(Operator1 mapper) {
return NonEmptyString.of(mapper.apply(value));
}
public T transform(Function1 mapper) {
return mapper.apply(value);
}
public String get() {
return value;
}
@Override
public boolean equals(Object obj) {
return EQUAL.applyTo(this, obj);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return String.format("NonEmptyString(%s)", value);
}
public static NonEmptyString of(String value) {
return requireNonEmpty(value).map(NonEmptyString::new).getOrElseThrow();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy