Visibility of enum values ​​in Java

Is there any way to mark certain values enumin Java as private-package, i.e. give them a default modifier?

Background (only to preempt the immediate first comment "Why?";))

I have an Taskobject with various execution methods and a state of execution that decides which method to call next. Each of the execution methods returns the execution state of the next method that should be called (basically, the framework for executing the state machine).

I have enumone that contains all possible execution states, but also contains several "internal" package states, such as "pending" or "failed", which should not be returned using the execution methods.

I know that I can manage these states in a separate variable with my own enumeration, but this would make the code much less clean, since it turned a single- switchdistance into (at least) two (and possibly an environment if). Also, I could, of course, just check the return value, but I would even prefer to make the wrong ones available first.

+5
source share
2 answers

, - "".

, ( , BlackVegetable OldCurmudgeon), :

A package-private :

enum PackagePrivateEnum {
    PUBLIC_VALUE_1,
    PUBLIC_VALUE_2,
    PUBLIC_VALUE_3,
    PACKAGE_PRIVATE_VALUE_1,
    PACKAGE_PRIVATE_VALUE_2;
}

:

public enum PublicEnum {
    PUBLIC_VALUE_1 (PackagePrivateEnum.PUBLIC_VALUE_1),
    PUBLIC_VALUE_2 (PackagePrivateEnum.PUBLIC_VALUE_2),
    PUBLIC_VALUE_3 (PackagePrivateEnum.PUBLIC_VALUE_3);

    final PackagePrivateEnum value;

    private PublicEnum(PackagePrivateEnum value) {
        this.value = value;
    }
}

, , , :

public abstract PublicEnum returnSomething();

:

PackagePrivateEnum value = returnSomething().value;

, , (, switch- if-out, Map-lookups .., .value ). , , GWT, , , "inlined" , .value -lookup , .

, : PublicEnum2, PackagePrivateEnum.

+5

, .

Task . State. , , State .

, Task , - (, /) . , .

, . , Task State, Map, .

, , , , , , .

public class Test {
  public void test() {
    new Thread(new Engine()).start();
  }

  static final Map<State, State> flow = new HashMap<>();

  static {
    flow.put(State.Start, State.A);
    flow.put(State.A, State.B);
    flow.put(State.B, State.Finished);
  }

  public static class Engine implements Runnable {
    State state = State.Start;

    @Override
    public void run() {
      while (state != State.Finished) {
        System.out.println("State: "+state);
        // Perform all tasks of this state.
        for ( Task task : state.tasks ) {
          System.out.println("Task: "+task);
          Result result = Result.Start;
          // Keep performing until completed.
          while ( result != Result.Completed ) {
            System.out.println("Result: "+result);
            result = result.perform(task);
          }
          System.out.println("Result: "+result);
        }
        // All tasks performed! Next state.
        state = flow.get(state);
      }
      System.out.println("State: "+state);
    }
  }

  enum State {
    Start, 
    A(Task.One, Task.Two), 
    B(Task.Two), 
    Finished;
    Iterable<Task> tasks;

    State(Task... tasks) {
      this.tasks = Arrays.asList(tasks);
    }
  }

  enum Result {
    Start {
      @Override
      Result perform(Task t) {
        return t.initialise();
      }
    },
    Executing {
      @Override
      Result perform(Task t) {
        return t.execute();
      }
    },
    Finalising {
      @Override
      Result perform(Task t) {
        return t.finalise();
      }
    },
    Completed {
      @Override
      Result perform(Task t) {
        // Stop there.
        return Completed;
      }
    };

    abstract Result perform(Task t);
  }

  enum Task {
    One {
      @Override
      Result initialise() {
        return Result.Executing;
      }

      @Override
      Result execute() {
        return Result.Finalising;
      }

      @Override
      Result finalise() {
        return Result.Completed;
      }
    },
    Two {
      @Override
      Result initialise() {
        return Result.Executing;
      }

      @Override
      Result execute() {
        return Result.Finalising;
      }

      @Override
      Result finalise() {
        return Result.Completed;
      }
    };

    abstract Result initialise();

    abstract Result execute();

    abstract Result finalise();
  }

  public static void main(String args[]) {
    try {
      new Test().test();
    } catch (Throwable t) {
      t.printStackTrace(System.err);
    }
  }
}

, , :

public class Test {
  public void test() {
    new Thread(new Engine()).start();
  }

  static final Map<State, State> flow = new HashMap<>();

  static {
    flow.put(State.Start, State.A);
    flow.put(State.A, State.B);
    flow.put(State.B, State.Finished);
  }

  public static class Engine implements Runnable {
    State state = State.Start;

    @Override
    public void run() {
      while (state != State.Finished) {
        System.out.println("State: "+state);
        // Perform all tasks of this state.
        for ( Task task : state.tasks ) {
          System.out.println("Task: "+task);
          task.initialise();
          task.execute();
          task.finalise();
        }
        // All tasks performed! Next state.
        state = flow.get(state);
      }
      System.out.println("State: "+state);
    }
  }

  enum State {
    Start, 
    A(Task.One, Task.Two), 
    B(Task.Two), 
    Finished;
    Iterable<Task> tasks;

    State(Task... tasks) {
      this.tasks = Arrays.asList(tasks);
    }
  }

  enum Task {
    One {
      @Override
      void execute() {
      }
    },
    Two {
      @Override
      void execute() {
      }
    };

    // Nothing by default.
    void initialise() {
    }

    abstract void execute();

    // Nothing by default.
    void finalise() {
    }

  }

  public static void main(String args[]) {
    try {
      new Test().test();
    } catch (Throwable t) {
      t.printStackTrace(System.err);
    }
  }
}

, , , .

+1

All Articles