com.github.tonivade.purefun.Equal Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
import static java.util.Objects.isNull;
import static java.util.Objects.requireNonNull;
public final class Equal {
private final T target;
private final Eq tester;
private Equal(T target, Eq tester) {
this.target = requireNonNull(target);
this.tester = requireNonNull(tester);
}
public Equal append(Eq other) {
return new Equal<>(target, tester.and(other));
}
public Equal comparing(Function1 getter) {
return append(Eq.comparing(getter));
}
public Equal comparingArray(Function1 getter) {
return append(Eq.comparingArray(getter));
}
@SuppressWarnings("unchecked")
public boolean applyTo(Object obj) {
if (isNull(obj)) {
return false;
}
if (sameObjects(obj)) {
return true;
}
return sameClasses(obj) && areEquals((T) obj);
}
private boolean areEquals(T other) {
return tester.eqv(target, other);
}
private boolean sameClasses(Object obj) {
return target.getClass() == obj.getClass();
}
private boolean sameObjects(Object obj) {
return target == obj;
}
public static Equal of(T target) {
return new Equal<>(target, Eq.always());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy