org.pure4j.immutable.AbstractImmutableValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pure4j-core Show documentation
Show all versions of pure4j-core Show documentation
Pure Functional Programming Semantics For Java
package org.pure4j.immutable;
import org.pure4j.Pure4J;
import org.pure4j.annotations.immutable.ImmutableValue;
import org.pure4j.annotations.mutable.MutableUnshared;
/**
* Save yourself the bother of writing all the boilerplate for your immutable
* value class by extending this one.
*
* @author robmoffat
*
*/
@ImmutableValue
public abstract class AbstractImmutableValue implements Comparable {
@MutableUnshared
protected interface Visitor {
public void visit(Object o, Object o2);
}
private static class HashCodeVisitor implements Visitor {
int result = 0;
@Override
public void visit(Object o, Object o2) {
result = (result * Pure4J.SOME_PRIME) + o.hashCode();
}
}
private static class EqualsVisitor implements Visitor {
boolean result = true;
@Override
public void visit(Object o, Object o2) {
if (result != false) {
result = Pure4J.equals(o, o2);
}
}
}
private static class ToStringVisitor implements Visitor {
StringBuilder in;
boolean addComma = false;
private ToStringVisitor(StringBuilder in) {
this.in = in;
}
public void visit(Object o, Object o2) {
if (addComma) {
in.append(",");
}
if (o != null) {
in.append(o.toString());
}
addComma = true;
}
}
private static class ComparisonVisitor implements Visitor {
int result = 0;
@SuppressWarnings("unchecked")
public void visit(Object o, Object o2) {
if (result == 0) {
if (o instanceof Comparable) {
if (o2 == null) {
result = -1;
} else {
if (o2.getClass() == o.getClass()) {
result = ((Comparable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy