Annotation Type AutoOneOf


@Retention(CLASS) @Target(TYPE) public @interface AutoOneOf
Specifies that the annotated class is a one-of class, also known as a tagged union. An @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.

  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Class<? extends Enum<?>>
    Specifies an enum that has one entry per variant in the one-of.
  • Element Details

    • value

      Class<? extends Enum<?>> value
      Specifies an enum that has one entry per variant in the one-of.