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.core.persistence.api.dao;
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.function.Predicate;
26  import org.apache.syncope.core.persistence.api.entity.Schema;
27  import org.apache.syncope.core.persistence.api.entity.group.Group;
28  
29  public class AllowedSchemas<S extends Schema> {
30  
31      private final Set<S> forSelf = new HashSet<>();
32  
33      private final Map<Group, Set<S>> forMemberships = new HashMap<>();
34  
35      public Set<S> getForSelf() {
36          return forSelf;
37      }
38  
39      public Set<S> getForMembership(final Group group) {
40          return forMemberships.get(group) == null ? Set.of() : forMemberships.get(group);
41      }
42  
43      public Map<Group, Set<S>> getForMemberships() {
44          return forMemberships;
45      }
46  
47      public boolean forSelfContains(final S schema) {
48          return forSelf.contains(schema);
49      }
50  
51      public boolean forSelfContains(final String schema) {
52          return forSelf.stream().anyMatch(new KeyMatches(schema));
53      }
54  
55      public boolean forMembershipsContains(final Group group, final S schema) {
56          return getForMembership(group).stream().anyMatch(s -> s.equals(schema));
57      }
58  
59      public boolean forMembershipsContains(final S schema) {
60          return forMemberships.entrySet().stream().
61                  anyMatch(entry -> entry.getValue().contains(schema));
62      }
63  
64      public boolean forMembershipsContains(final Group group, final String schema) {
65          return getForMembership(group).stream().anyMatch(new KeyMatches(schema));
66      }
67  
68      public boolean forMembershipsContains(final String schema) {
69          KeyMatches keyMatches = new KeyMatches(schema);
70  
71          return forMemberships.entrySet().stream().
72                  anyMatch(entry -> entry.getValue().stream().anyMatch(keyMatches));
73      }
74  
75      public boolean contains(final S schema) {
76          if (forSelfContains(schema)) {
77              return true;
78          }
79          return forMembershipsContains(schema);
80      }
81  
82      public boolean contains(final String schema) {
83          if (forSelfContains(schema)) {
84              return true;
85          }
86          return forMembershipsContains(schema);
87      }
88  
89      private class KeyMatches implements Predicate<S> {
90  
91          private final String schema;
92  
93          KeyMatches(final String schema) {
94              this.schema = schema;
95          }
96  
97          @Override
98          public boolean test(final S object) {
99              return object.getKey().equals(schema);
100         }
101     }
102 }