View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * License); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.lib.types;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  public final class EntitlementsHolder {
27  
28      private static final Object MONITOR = new Object();
29  
30      private static EntitlementsHolder INSTANCE;
31  
32      public static EntitlementsHolder getInstance() {
33          synchronized (MONITOR) {
34              if (INSTANCE == null) {
35                  INSTANCE = new EntitlementsHolder();
36              }
37          }
38          return INSTANCE;
39      }
40  
41      private final Set<String> values = Collections.synchronizedSet(new HashSet<>());
42  
43      private EntitlementsHolder() {
44          // private constructor for singleton
45      }
46  
47      public void addAll(final Collection<String> values) {
48          this.values.addAll(values);
49      }
50  
51      public Set<String> addFor(final String anyType) {
52          Set<String> added = new HashSet<>();
53  
54          for (AnyEntitlement operation : AnyEntitlement.values()) {
55              this.values.add(operation.getFor(anyType));
56              added.add(operation.getFor(anyType));
57          }
58  
59          return added;
60      }
61  
62      public Set<String> removeFor(final String anyType) {
63          Set<String> removed = new HashSet<>();
64  
65          for (AnyEntitlement operation : AnyEntitlement.values()) {
66              this.values.remove(operation.getFor(anyType));
67              removed.add(operation.getFor(anyType));
68          }
69  
70          return removed;
71      }
72  
73      public Set<String> getValues() {
74          return Collections.unmodifiableSet(values);
75      }
76  }