View Javadoc

1   package fr.in2p3.jsaga.impl.namespace;
2   
3   import org.ogf.saga.error.BadParameterException;
4   import org.ogf.saga.namespace.Flags;
5   
6   /* ***************************************************
7   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
8   * ***             http://cc.in2p3.fr/             ***
9   * ***************************************************
10  * File:   FlagsHelper
11  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
12  * Date:   25 oct. 2008
13  * ***************************************************
14  * Description:                                      */
15  /**
16   *
17   */
18  public class FlagsHelper {
19      private static final int ALL = Flags.ALLFILEFLAGS.or(JSAGAFlags.ALLFLAGS.getValue());
20      private int m_flags;
21  
22      public FlagsHelper(int flags) {
23          m_flags = flags;
24      }
25  
26      public void allowed(Flags... allowedFlags) throws BadParameterException {
27          this.allowed(null, allowedFlags);
28      }
29      public void allowed(JSAGAFlags jsagaFlag, Flags... allowedFlags) throws BadParameterException {
30          // set allowed flags
31          int allowed = toInt(jsagaFlag, allowedFlags);
32  
33          // set forbidden flags
34          int forbidden = ALL - allowed;
35  
36          // set bad flags
37          int bad = m_flags & forbidden;
38          if (bad > 0) {
39              throw new BadParameterException("Flags not allowed for this method: "+bad);
40          }
41      }
42  
43      public void required(Flags... requiredFlags) throws BadParameterException {
44          this.required(null, requiredFlags);
45      }
46      public void required(JSAGAFlags jsagaFlag, Flags... requiredFlags) throws BadParameterException {
47          // set required flags
48          int required = toInt(jsagaFlag, requiredFlags);
49  
50          // set unset flags
51          int unset = ALL - m_flags;
52  
53          // set missing flags
54          int missing = unset & required;
55          if (missing > 0) {
56              throw new BadParameterException("Flags missing for this method: "+missing);
57          }
58      }
59  
60      public int add(Flags... addedFlags) {
61          return this.add(null, addedFlags);
62      }
63      public int add(JSAGAFlags jsagaFlag, Flags... addedFlags) {
64          // set added flags
65          int added = toInt(jsagaFlag, addedFlags);
66          
67          // return addition
68          return m_flags | added;
69      }
70  
71      public int keep(Flags... keepedFlags) {
72          return this.keep(null, keepedFlags);
73      }
74      public int keep(JSAGAFlags jsagaFlag, Flags... keepedFlags) {
75          // set keeped flags
76          int keeped = toInt(jsagaFlag, keepedFlags);
77  
78          // return intersection
79          return m_flags & keeped;
80      }
81  
82      public int remove(Flags... substractedFlags) {
83          return this.remove(null, substractedFlags);
84      }
85      public int remove(JSAGAFlags jsagaFlag, Flags... substractedFlags) {
86          // set removed flags
87          int removed = toInt(jsagaFlag, substractedFlags);
88  
89          // return substraction
90          return m_flags - (m_flags & removed);
91      }
92  
93      private static int toInt(JSAGAFlags jsagaFlag, Flags... flags) {
94          int result = (jsagaFlag!=null ? jsagaFlag.getValue() : Flags.NONE.getValue());
95          for (Flags f : flags) {
96              result = f.or(result);
97          }
98          return result;
99      }
100 }