mirror of https://github.com/sbt/sbt.git
Improve xsbti.Maybe
* Make Nothing a singleton * Make Maybe's constructor private * Optimise equals to use reference equality first * Optimise Just.equals by having proper, non-anonymous subclasses * Having non-anonymous subclasses makes them have nicer classnames * Give Just a value() method
This commit is contained in:
parent
ea56f331a1
commit
41c7e9b85d
|
|
@ -4,46 +4,45 @@
|
|||
package xsbti;
|
||||
|
||||
/** Intended as a lightweight carrier for scala.Option. */
|
||||
public abstract class Maybe<t>
|
||||
{
|
||||
// private pending Scala bug #3642
|
||||
protected Maybe() {}
|
||||
public abstract class Maybe<t> {
|
||||
private Maybe() {}
|
||||
|
||||
public static <s> Maybe<s> just(final s v)
|
||||
{
|
||||
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()
|
||||
{
|
||||
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();
|
||||
}
|
||||
};
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <s> Maybe<s> nothing() { return (Maybe<s>) Nothing.INSTANCE; }
|
||||
public static <s> Maybe<s> just(final s v) { return new Just<s>(v); }
|
||||
|
||||
}
|
||||
public static final class Just<s> extends Maybe<s> {
|
||||
private final s v;
|
||||
|
||||
public final boolean isEmpty() { return !isDefined(); }
|
||||
public abstract boolean isDefined();
|
||||
public abstract t get();
|
||||
public Just(final s v) { this.v = v; }
|
||||
|
||||
public s value() { return v; }
|
||||
|
||||
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 (this == o) return true;
|
||||
if (o == null || !(o instanceof Just<?>)) return false;
|
||||
final Just<?> that = (Just<?>) o;
|
||||
return v == null ? that.v == null : v.equals(that.v);
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Nothing extends Maybe<Object> {
|
||||
public static final Nothing INSTANCE = new Nothing();
|
||||
private Nothing() { }
|
||||
|
||||
public boolean isDefined() { return false; }
|
||||
public Object get() { throw new UnsupportedOperationException("nothing.get"); }
|
||||
|
||||
public int hashCode() { return 1; }
|
||||
public String toString() { return "Maybe.nothing()"; }
|
||||
public boolean equals(Object o) { return this == o || o != null && o instanceof Nothing; }
|
||||
}
|
||||
|
||||
public final boolean isEmpty() { return !isDefined(); }
|
||||
public abstract boolean isDefined();
|
||||
public abstract t get();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue