Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>enum A {a,b,c} enum B extends A {d} /*B is {a,b,c,d}*/ </code></pre> <p>can be written as:</p> <pre><code>public enum All { a (ClassGroup.A,ClassGroup.B), b (ClassGroup.A,ClassGroup.B), c (ClassGroup.A,ClassGroup.B), d (ClassGroup.B) ... </code></pre> <ul> <li><em>ClassGroup.B.getMembers()</em> contains {a,b,c,d}</li> </ul> <p><strong>How it can be useful:</strong> Let say we want something like: We have events and we are using enums. Those enums can be grouped by similar processing. If we have operation with many elements, then some events starts operation, some are just step and other end the operation. To gather such operation and avoid long switch case we can group them as in example and use:</p> <pre><code>if(myEvent.is(State_StatusGroup.START)) makeNewOperationObject().. if(myEnum.is(State_StatusGroup.STEP)) makeSomeSeriousChanges().. if(myEnum.is(State_StatusGroup.FINISH)) closeTransactionOrSomething().. </code></pre> <p>Example:</p> <pre><code>public enum AtmOperationStatus { STARTED_BY_SERVER (State_StatusGroup.START), SUCCESS (State_StatusGroup.FINISH), FAIL_TOKEN_TIMEOUT (State_StatusGroup.FAIL, State_StatusGroup.FINISH), FAIL_NOT_COMPLETE (State_StatusGroup.FAIL, State_StatusGroup.STEP), FAIL_UNKNOWN (State_StatusGroup.FAIL, State_StatusGroup.FINISH), (...) private AtmOperationStatus(StatusGroupInterface ... pList){ for (StatusGroupInterface group : pList){ group.addMember(this); } } public boolean is(StatusGroupInterface with){ for (AtmOperationStatus eT : with.getMembers()){ if( eT .equals(this)) return true; } return false; } // Each group must implement this interface private interface StatusGroupInterface{ EnumSet&lt;AtmOperationStatus&gt; getMembers(); void addMember(AtmOperationStatus pE); } // DEFINING GROUPS public enum State_StatusGroup implements StatusGroupInterface{ START, STEP, FAIL, FINISH; private List&lt;AtmOperationStatus&gt; members = new LinkedList&lt;AtmOperationStatus&gt;(); @Override public EnumSet&lt;AtmOperationStatus&gt; getMembers() { return EnumSet.copyOf(members); } @Override public void addMember(AtmOperationStatus pE) { members.add(pE); } static { // forcing initiation of dependent enum try { Class.forName(AtmOperationStatus.class.getName()); } catch (ClassNotFoundException ex) { throw new RuntimeException("Class AtmEventType not found", ex); } } } } //Some use of upper code: if (p.getStatus().is(AtmOperationStatus.State_StatusGroup.FINISH)) { //do something }else if (p.getStatus().is(AtmOperationStatus.State_StatusGroup.START)) { //do something } </code></pre> <p>Add some more advanced:</p> <pre><code>public enum AtmEventType { USER_DEPOSIT (Status_EventsGroup.WITH_STATUS, Authorization_EventsGroup.USER_AUTHORIZED, ChangedMoneyAccountState_EventsGroup.CHANGED, OperationType_EventsGroup.DEPOSIT, ApplyTo_EventsGroup.CHANNEL), SERVICE_DEPOSIT (Status_EventsGroup.WITH_STATUS, Authorization_EventsGroup.TERMINAL_AUTHORIZATION, ChangedMoneyAccountState_EventsGroup.CHANGED, OperationType_EventsGroup.DEPOSIT, ApplyTo_EventsGroup.CHANNEL), DEVICE_MALFUNCTION (Status_EventsGroup.WITHOUT_STATUS, Authorization_EventsGroup.TERMINAL_AUTHORIZATION, ChangedMoneyAccountState_EventsGroup.DID_NOT_CHANGED, ApplyTo_EventsGroup.DEVICE), CONFIGURATION_4_C_CHANGED(Status_EventsGroup.WITHOUT_STATUS, ApplyTo_EventsGroup.TERMINAL, ChangedMoneyAccountState_EventsGroup.DID_NOT_CHANGED), (...) </code></pre> <p>At above if we have some fail (myEvent.is(State_StatusGroup.FAIL)) then iterating by previous events we can easily check if we must revert money transfer by:</p> <pre><code>if(myEvent2.is(ChangedMoneyAccountState_EventsGroup.CHANGED)) rollBack().. </code></pre> <p>It can be useful for:</p> <blockquote> <ol> <li>including explicite meta-data about processing logic, less to remember</li> <li>implementing some of multi-inheritance</li> <li>we don't want to use class structures, ex. for sending short status messages</li> </ol> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload