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
8
9
10
11
12
13
14
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
31 int allowed = toInt(jsagaFlag, allowedFlags);
32
33
34 int forbidden = ALL - allowed;
35
36
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
48 int required = toInt(jsagaFlag, requiredFlags);
49
50
51 int unset = ALL - m_flags;
52
53
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
65 int added = toInt(jsagaFlag, addedFlags);
66
67
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
76 int keeped = toInt(jsagaFlag, keepedFlags);
77
78
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
87 int removed = toInt(jsagaFlag, substractedFlags);
88
89
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 }