@Retention(value=CLASS)
@Target(value=TYPE)
public @interface AutoOneOf
@AutoOneOf
class is very similar to an @AutoValue
class, in that its
abstract methods define a set of properties. But unlike @AutoValue
, only one of those
properties is defined in any given instance.
@AutoOneOf(StringOrInteger.Kind.class)
public abstract class StringOrInteger {
public enum Kind {STRING, INTEGER}
public abstract Kind getKind();
public abstract String string();
public abstract int integer();
public static StringOrInteger ofString(String s) {
return AutoOneOf_StringOrInteger.string(s);
}
public static StringOrInteger ofInteger(int i) {
return AutoOneOf_StringOrInteger.integer(i);
}
}
String client(StringOrInteger stringOrInteger) {
switch (stringOrInteger.getKind()) {
case STRING:
return "the string '" + stringOrInteger.string() + "'";
case INTEGER:
return "the integer " + stringOrInteger.integer();
}
throw new AssertionError();
}
@AutoOneOf
is explained in more detail in the user guide.
Modifier and Type | Required Element and Description |
---|---|
java.lang.Class<? extends java.lang.Enum<?>> |
value
Specifies an enum that has one entry per variant in the one-of.
|
Copyright © 2020 Google LLC. All Rights Reserved.