This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository wao. See http://git.codelutin.com/wao.git commit 4e7600f4079624b8b66354a5ecb23fbd52f992f6 Author: Brendan Le Ny <bleny@codelutin.com> Date: Wed Oct 22 17:04:14 2014 +0200 Remaniement de l'énumération ContactState car la matrice de booléen est difficille à lire --- .../java/fr/ifremer/wao/entity/ContactState.java | 85 ++++++++-------------- 1 file changed, 30 insertions(+), 55 deletions(-) diff --git a/wao-persistence/src/main/java/fr/ifremer/wao/entity/ContactState.java b/wao-persistence/src/main/java/fr/ifremer/wao/entity/ContactState.java index 69ee1aa..3d1d6ce 100644 --- a/wao-persistence/src/main/java/fr/ifremer/wao/entity/ContactState.java +++ b/wao-persistence/src/main/java/fr/ifremer/wao/entity/ContactState.java @@ -24,8 +24,8 @@ package fr.ifremer.wao.entity; -import java.util.ArrayList; -import java.util.List; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; import static org.nuiton.i18n.I18n.n; @@ -47,43 +47,45 @@ import static org.nuiton.i18n.I18n.n; * * @author fdesbois <fdesbois@codelutin.com> */ -public enum ContactState implements I18nAble{ +public enum ContactState implements I18nAble { /** Usually, the first step, observer just contacted someone */ - CONTACT_START(n("ContactState.CONTACT_START"), false, false, false), + CONTACT_START(n("ContactState.CONTACT_START")), /** Observer has now someone to meet */ - OBSERVATION_EXPECTED(n("ContactState.OBSERVATION_EXPECTED"), false, false, true), + OBSERVATION_EXPECTED(n("ContactState.OBSERVATION_EXPECTED")), /** Observation was done, all data have been collected. Everything is fine */ - OBSERVATION_DONE(n("ContactState.OBSERVATION_DONE"), true, false, true), + OBSERVATION_DONE(n("ContactState.OBSERVATION_DONE")), /** Observer failed to collect data, weather or any other circumstances made data collection impossible */ - OBSERVATION_CANCELLED(n("ContactState.OBSERVATION_CANCELLED"), true, true, true), + OBSERVATION_CANCELLED(n("ContactState.OBSERVATION_CANCELLED")), /** After a contact start, people contacted refused the observation. Observer may try later. */ - CONTACT_REFUSED(n("ContactState.CONTACT_REFUSED"), true ,true, false), + CONTACT_REFUSED(n("ContactState.CONTACT_REFUSED")), /** After a contact refused, people contacted still refuse the observation. Observer may not try later. */ - CONTACT_DEFINITELY_REFUSED(n("ContactState.CONTACT_DEFINITELY_REFUSED"), true, true, false); + CONTACT_DEFINITELY_REFUSED(n("ContactState.CONTACT_DEFINITELY_REFUSED")); - private String i18nKey; + protected static final ImmutableSet<ContactState> ALL_CONTACT_STATES = + ImmutableSet.copyOf(values()); - /** if true, the state may not be changed. */ - private boolean finalState; + protected static final ImmutableSet<ContactState> FINAL_CONTACT_STATES = + ImmutableSet.of(OBSERVATION_CANCELLED, CONTACT_REFUSED, CONTACT_DEFINITELY_REFUSED); - /** if true, observation in this state has no collected data */ - private boolean unfinishedState; + protected static final ImmutableSet<ContactState> UNFINISHED_CONTACT_STATES = + ImmutableSet.of(OBSERVATION_DONE, OBSERVATION_CANCELLED, CONTACT_REFUSED, CONTACT_DEFINITELY_REFUSED); - /** true if this value can be used for a contact of program {@link fr.ifremer.wao.entity.ObsProgram#OBSVENTE} */ - protected boolean allowedForObsVente; + protected static final ImmutableSetMultimap<ObsProgram, ContactState> ALLOWED_CONTACT_STATES_PER_OBS_PROGRAM = + ImmutableSetMultimap.<ObsProgram, ContactState>builder() + .putAll(ObsProgram.OBSMER, ALL_CONTACT_STATES) + .putAll(ObsProgram.OBSVENTE, ImmutableSet.of(OBSERVATION_EXPECTED, OBSERVATION_DONE, OBSERVATION_CANCELLED)) + .build(); - ContactState(String i18nKey, boolean finalState, boolean unfinishedState, - boolean allowedForObsVente) { + protected String i18nKey; + + ContactState(String i18nKey) { this.i18nKey = i18nKey; - this.finalState = finalState; - this.unfinishedState = unfinishedState; - this.allowedForObsVente = allowedForObsVente; } @Override @@ -91,49 +93,22 @@ public enum ContactState implements I18nAble{ return i18nKey; } + /** if true, the state may not be changed. */ public boolean isFinalState() { - return this.finalState; + return FINAL_CONTACT_STATES.contains(this); } + /** if true, observation in this state has no collected data */ public boolean isUnfinishedState() { - return this.unfinishedState; - } - - /** - * Get the contactState corresponding to the {@code ordinal} value. - * - * @param ordinal reference for the ContactState - * @return ContactState - */ - public static ContactState valueOf(int ordinal) { - for (ContactState curr : ContactState.values()) { - if (ordinal == curr.ordinal()) { - return curr; - } - } - return null; + return UNFINISHED_CONTACT_STATES.contains(this); } public boolean isAllowed(ObsProgram obsProgram) { - boolean allowed; - if (obsProgram == ObsProgram.OBSMER) { - allowed = true; - } else if (obsProgram == ObsProgram.OBSVENTE) { - allowed = allowedForObsVente; - } else { - throw new IllegalStateException(); - } - return allowed; + return getAllowedStates(obsProgram).contains(this); } - public static List<ContactState> getAllowedStates(ObsProgram obsProgram) { - List<ContactState> allowedStates = new ArrayList<>(); - for (ContactState contactState : values()) { - if (contactState.isAllowed(obsProgram)) { - allowedStates.add(contactState); - } - } - return allowedStates; + public static ImmutableSet<ContactState> getAllowedStates(ObsProgram obsProgram) { + return ALLOWED_CONTACT_STATES_PER_OBS_PROGRAM.get(obsProgram); } /** -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.