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,43 +4,42 @@
|
||||||
package xsbti;
|
package xsbti;
|
||||||
|
|
||||||
/** Intended as a lightweight carrier for scala.Option. */
|
/** Intended as a lightweight carrier for scala.Option. */
|
||||||
public abstract class Maybe<t>
|
public abstract class Maybe<t> {
|
||||||
{
|
private Maybe() {}
|
||||||
// private pending Scala bug #3642
|
|
||||||
protected Maybe() {}
|
@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 Just(final s v) { this.v = v; }
|
||||||
|
|
||||||
|
public s value() { return v; }
|
||||||
|
|
||||||
public static <s> Maybe<s> just(final s v)
|
|
||||||
{
|
|
||||||
return new Maybe<s>() {
|
|
||||||
public boolean isDefined() { return true; }
|
public boolean isDefined() { return true; }
|
||||||
public s get() { return v; }
|
public s get() { return v; }
|
||||||
public int hashCode() { return 17 + (v == null ? 0 : v.hashCode()); }
|
public int hashCode() { return 17 + (v == null ? 0 : v.hashCode()); }
|
||||||
public String toString() { return "Maybe.just(" + v + ")"; }
|
public String toString() { return "Maybe.just(" + v + ")"; }
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (o == null) return false;
|
if (this == o) return true;
|
||||||
if (!(o instanceof Maybe)) return false;
|
if (o == null || !(o instanceof Just<?>)) return false;
|
||||||
Maybe<?> other = (Maybe<?>) o;
|
final Just<?> that = (Just<?>) o;
|
||||||
if (!other.isDefined()) return false;
|
return v == null ? that.v == null : v.equals(that.v);
|
||||||
if (v == null) return other.get() == null;
|
|
||||||
return v.equals(other.get());
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
public static <s> Maybe<s> nothing()
|
|
||||||
{
|
public static final class Nothing extends Maybe<Object> {
|
||||||
return new Maybe<s>() {
|
public static final Nothing INSTANCE = new Nothing();
|
||||||
|
private Nothing() { }
|
||||||
|
|
||||||
public boolean isDefined() { return false; }
|
public boolean isDefined() { return false; }
|
||||||
public s get() { throw new UnsupportedOperationException("nothing.get"); }
|
public Object get() { throw new UnsupportedOperationException("nothing.get"); }
|
||||||
|
|
||||||
public int hashCode() { return 1; }
|
public int hashCode() { return 1; }
|
||||||
public String toString() { return "Maybe.nothing()"; }
|
public String toString() { return "Maybe.nothing()"; }
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) { return this == o || o != null && o instanceof Nothing; }
|
||||||
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 final boolean isEmpty() { return !isDefined(); }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue