Merge pull request #46 from Duhemm/wip/maybe-equals

Implement hashCode, equals and toString in Maybe
This commit is contained in:
eugene yokota 2016-09-14 11:26:20 -04:00 committed by GitHub
commit fba4e78543
1 changed files with 20 additions and 1 deletions

View File

@ -14,6 +14,16 @@ public abstract class Maybe<t>
return new Maybe<s>() {
public boolean isDefined() { return true; }
public s get() { return v; }
public int hashCode() { return 17 + (v == null ? 0 : v.hashCode()); }
public String toString() { return "Maybe.just(" + v + ")"; }
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Maybe)) return false;
Maybe<?> other = (Maybe<?>) o;
if (!other.isDefined()) return false;
if (v == null) return other.get() == null;
return v.equals(other.get());
}
};
}
public static <s> Maybe<s> nothing()
@ -21,10 +31,19 @@ public abstract class Maybe<t>
return new Maybe<s>() {
public boolean isDefined() { return false; }
public s get() { throw new UnsupportedOperationException("nothing.get"); }
public int hashCode() { return 1; }
public String toString() { return "Maybe.nothing()"; }
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Maybe)) return false;
Maybe<?> other = (Maybe<?>) o;
return !other.isDefined();
}
};
}
public final boolean isEmpty() { return !isDefined(); }
public abstract boolean isDefined();
public abstract t get();
}
}