OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 67 | Rev 177 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 67 Rev 149
Line 26... Line 26...
26
 
26
 
27
/**
27
/**
28
 * A class to specify which objects to include or exclude.
28
 * A class to specify which objects to include or exclude.
29
 * 
29
 *
30
 * @author Sylvain
30
 * @author Sylvain
-
 
31
 * @param <T> type of items
31
 * @see #isIncluded(Object)
32
 * @see #isIncluded(Object)
32
 */
33
 */
33
@Immutable
34
@Immutable
34
public class IncludeExclude<T> {
35
public class IncludeExclude<T> {
35
 
36
 
Line 44... Line 45...
44
    @SuppressWarnings("unchecked")
45
    @SuppressWarnings("unchecked")
45
    public static <T> IncludeExclude<T> getFull() {
46
    public static <T> IncludeExclude<T> getFull() {
46
        return (IncludeExclude<T>) FULL;
47
        return (IncludeExclude<T>) FULL;
47
    }
48
    }
48
 
49
 
49
    public static <T> IncludeExclude<T> getNormalized(Collection<? extends T> includes) {
50
    public static <T> IncludeExclude<T> getNormalized(final Collection<? extends T> includes) {
50
        return getNormalized(includes, Collections.<T> emptySet());
51
        return getNormalized(includes, Collections.<T> emptySet());
51
    }
52
    }
52
 
53
 
53
    public static <T> IncludeExclude<T> getNormalized(Collection<? extends T> includes, Collection<? extends T> excludes) {
54
    public static <T> IncludeExclude<T> getNormalized(final Collection<? extends T> includes, final Collection<? extends T> excludes) {
54
        return new IncludeExclude<T>(includes, excludes).normal;
55
        return new IncludeExclude<T>(includes, excludes).normal;
55
    }
56
    }
56
 
57
 
57
    private final Set<T> includes;
58
    private final Set<T> includes;
58
    private final Set<T> excludes;
59
    private final Set<T> excludes;
59
    private final IncludeExclude<T> normal;
60
    private final IncludeExclude<T> normal;
60
 
61
 
61
    public IncludeExclude(Collection<? extends T> includes) {
62
    public IncludeExclude(final Collection<? extends T> includes) {
62
        this(includes, Collections.<T> emptySet());
63
        this(includes, Collections.<T> emptySet());
63
    }
64
    }
64
 
65
 
65
    /**
66
    /**
66
     * Create a new instance.
67
     * Create a new instance.
67
     * 
68
     *
68
     * @param includes which objects to include, <code>null</code> meaning all.
69
     * @param includes which objects to include, <code>null</code> meaning all.
69
     * @param excludes which objects to exclude, <code>null</code> meaning all.
70
     * @param excludes which objects to exclude, <code>null</code> meaning all.
70
     */
71
     */
71
    public IncludeExclude(Collection<? extends T> includes, Collection<? extends T> excludes) {
72
    public IncludeExclude(final Collection<? extends T> includes, final Collection<? extends T> excludes) {
72
        this(includes, excludes, false);
73
        this(includes, excludes, false);
73
    }
74
    }
74
 
75
 
75
    private IncludeExclude(Collection<? extends T> includes, Collection<? extends T> excludes, final boolean isNormal) {
76
    private IncludeExclude(final Collection<? extends T> includes, final Collection<? extends T> excludes, final boolean isNormal) {
-
 
77
        this(includes == null ? null : Collections.unmodifiableSet(new HashSet<T>(includes)), excludes == null ? null : Collections.unmodifiableSet(new HashSet<T>(excludes)), false, isNormal);
-
 
78
    }
-
 
79
 
-
 
80
    private IncludeExclude(final Set<T> includes, final Set<T> excludes, final boolean areMutable, final boolean isNormal) {
76
        super();
81
        super();
-
 
82
        // parameter just to overload constructor
-
 
83
        if (areMutable)
77
        this.includes = includes == null ? null : Collections.unmodifiableSet(new HashSet<T>(includes));
84
            throw new IllegalStateException();
-
 
85
        this.includes = includes;
78
        this.excludes = excludes == null ? null : Collections.unmodifiableSet(new HashSet<T>(excludes));
86
        this.excludes = excludes;
79
        this.normal = isNormal ? this : this.normalize();
87
        this.normal = isNormal ? this : this.normalize();
80
        assert this.normal.excludes.isEmpty() || this.normal.includes == null;
88
        assert this.normal.excludes.isEmpty() || this.normal.includes == null;
81
    }
89
    }
82
 
90
 
83
    private final IncludeExclude<T> normalize() {
91
    private final IncludeExclude<T> normalize() {
84
        if (this.excludes == null)
92
        if (this.excludes == null || (this.includes != null && this.includes.isEmpty()))
85
            return getEmpty();
93
            return getEmpty();
86
        if (this.excludes.isEmpty() && this.includes == null)
94
        if (this.excludes.isEmpty() && this.includes == null)
87
            return getFull();
95
            return getFull();
88
        if (this.excludes.isEmpty() || this.includes == null)
96
        if (this.excludes.isEmpty() || this.includes == null)
89
            return this;
97
            return this;
Line 107... Line 115...
107
            return false;
115
            return false;
108
        else
116
        else
109
            return (this.normal.includes == null || this.normal.includes.contains(s)) && !this.normal.excludes.contains(s);
117
            return (this.normal.includes == null || this.normal.includes.contains(s)) && !this.normal.excludes.contains(s);
110
    }
118
    }
111
 
119
 
-
 
120
    public final <S extends T> Collection<S> getExcluded(final Collection<S> items) {
-
 
121
        if (this.areNoneIncluded(items))
-
 
122
            return items;
-
 
123
        else if (this.areAllIncluded(items))
-
 
124
            return Collections.emptySet();
-
 
125
 
-
 
126
        final Set<S> res = new HashSet<>(items);
-
 
127
        // avoid checks of getIncluded()
-
 
128
        res.removeAll(this.createIncluded(items));
-
 
129
        return res;
-
 
130
    }
-
 
131
 
-
 
132
    public final <S extends T> Collection<S> getIncluded(final Collection<S> items) {
-
 
133
        if (this.areAllIncluded(items))
-
 
134
            return items;
-
 
135
        else if (this.isNoneIncluded())
-
 
136
            return Collections.emptySet();
-
 
137
        else
-
 
138
            return createIncluded(items);
-
 
139
    }
-
 
140
 
-
 
141
    private <S extends T> Collection<S> createIncluded(final Collection<S> items) {
-
 
142
        final Set<S> res = new HashSet<>(items);
-
 
143
        if (this.normal.includes != null)
-
 
144
            res.retainAll(this.normal.includes);
-
 
145
        res.removeAll(this.normal.excludes);
-
 
146
        return res;
-
 
147
    }
-
 
148
 
-
 
149
    public final boolean areAllIncluded(final Collection<? extends T> items) {
-
 
150
        if (this.isAllIncluded() || items.isEmpty())
-
 
151
            return true;
-
 
152
        else if (this.isNoneIncluded())
-
 
153
            return false;
-
 
154
        else
-
 
155
            return (this.normal.includes == null || this.normal.includes.containsAll(items)) && Collections.disjoint(this.normal.excludes, items);
-
 
156
    }
-
 
157
 
-
 
158
    public final boolean areNoneIncluded(final Collection<? extends T> items) {
-
 
159
        if (this.isNoneIncluded() || items.isEmpty())
-
 
160
            return true;
-
 
161
        else if (this.isAllIncluded())
-
 
162
            return false;
-
 
163
        else
-
 
164
            return (this.normal.includes != null && Collections.disjoint(this.normal.includes, items)) || this.normal.excludes.containsAll(items);
-
 
165
    }
-
 
166
 
112
    /**
167
    /**
113
     * Whether this includes all objects.
168
     * Whether this includes all objects.
114
     * 
169
     *
115
     * @return <code>true</code> if {@link #isIncluded(Object)} always return <code>true</code>
170
     * @return <code>true</code> if {@link #isIncluded(Object)} always return <code>true</code>
116
     */
171
     */
Line 155... Line 210...
155
            return sole.get1();
210
            return sole.get1();
156
        else
211
        else
157
            return ifNoSole;
212
            return ifNoSole;
158
    }
213
    }
159
 
214
 
-
 
215
    public final IncludeExclude<T> include(final Collection<? extends T> items) {
-
 
216
        if (this.areAllIncluded(items))
-
 
217
            return this;
-
 
218
        else if (this.excludes == null)
-
 
219
            throw new IllegalStateException("Cannot include an item when excluding all");
-
 
220
 
-
 
221
        Set<T> newIncludes;
-
 
222
        if (this.includes == null || this.includes.containsAll(items)) {
-
 
223
            newIncludes = this.includes;
-
 
224
        } else {
-
 
225
            newIncludes = new HashSet<>(this.includes);
-
 
226
            newIncludes.addAll(items);
-
 
227
            newIncludes = Collections.unmodifiableSet(newIncludes);
-
 
228
        }
-
 
229
        Set<T> newExcludes;
-
 
230
        if (Collections.disjoint(this.excludes, items)) {
-
 
231
            newExcludes = this.excludes;
-
 
232
        } else {
-
 
233
            newExcludes = new HashSet<>(this.excludes);
-
 
234
            newExcludes.removeAll(items);
-
 
235
            newExcludes = Collections.unmodifiableSet(newExcludes);
-
 
236
        }
-
 
237
 
-
 
238
        return new IncludeExclude<T>(newIncludes, newExcludes, false, false);
-
 
239
    }
-
 
240
 
-
 
241
    public final IncludeExclude<T> exclude(final Collection<? extends T> items) {
-
 
242
        if (this.areNoneIncluded(items))
-
 
243
            return this;
-
 
244
 
-
 
245
        assert this.excludes != null : "!areNoneIncluded() but this.excludes == null";
-
 
246
        Set<T> newExcludes = new HashSet<>(this.excludes);
-
 
247
        newExcludes.addAll(items);
-
 
248
        newExcludes = Collections.unmodifiableSet(newExcludes);
-
 
249
        return new IncludeExclude<T>(this.includes, Collections.unmodifiableSet(newExcludes), false, false);
-
 
250
    }
-
 
251
 
160
    @Override
252
    @Override
161
    public int hashCode() {
253
    public int hashCode() {
162
        final int prime = 31;
254
        final int prime = 31;
163
        int result = 1;
255
        int result = 1;
164
        result = prime * result + ((excludes == null) ? 0 : excludes.hashCode());
256
        result = prime * result + ((this.excludes == null) ? 0 : this.excludes.hashCode());
165
        result = prime * result + ((includes == null) ? 0 : includes.hashCode());
257
        result = prime * result + ((this.includes == null) ? 0 : this.includes.hashCode());
166
        return result;
258
        return result;
167
    }
259
    }
168
 
260
 
169
    @Override
261
    @Override
170
    public boolean equals(Object obj) {
262
    public boolean equals(final Object obj) {
171
        if (this == obj)
263
        if (this == obj)
172
            return true;
264
            return true;
173
        if (obj == null)
265
        if (obj == null)
174
            return false;
266
            return false;
175
        if (getClass() != obj.getClass())
267
        if (getClass() != obj.getClass())
176
            return false;
268
            return false;
177
        final IncludeExclude<?> other = (IncludeExclude<?>) obj;
269
        final IncludeExclude<?> other = (IncludeExclude<?>) obj;
178
        return CompareUtils.equals(this.includes, other.includes) && CompareUtils.equals(this.excludes, other.excludes);
270
        return CompareUtils.equals(this.includes, other.includes) && CompareUtils.equals(this.excludes, other.excludes);
179
    }
271
    }
180
 
272
 
-
 
273
    @Override
-
 
274
    public String toString() {
-
 
275
        final String suffix;
-
 
276
        if (this == FULL)
-
 
277
            suffix = " ALL";
-
 
278
        else if (this == EMPTY)
-
 
279
            suffix = " NONE";
-
 
280
        else
-
 
281
            suffix = " includes " + this.includes + " except " + this.excludes;
-
 
282
        return this.getClass().getSimpleName() + suffix;
-
 
283
    }
181
}
284
}