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.to;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
23  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.Set;
28  import java.util.TreeSet;
29  import org.apache.commons.lang3.builder.EqualsBuilder;
30  import org.apache.commons.lang3.builder.HashCodeBuilder;
31  import org.apache.commons.lang3.builder.ToStringBuilder;
32  import org.apache.commons.lang3.builder.ToStringStyle;
33  import org.apache.syncope.common.lib.Attr;
34  import org.apache.syncope.common.lib.Attributable;
35  import org.apache.syncope.common.lib.BaseBean;
36  
37  public class MembershipTO implements BaseBean, Attributable {
38  
39      private static final long serialVersionUID = 5992828670273935861L;
40  
41      public static class Builder {
42  
43          private final MembershipTO instance = new MembershipTO();
44  
45          public Builder(final String groupKey) {
46              instance.setGroupKey(groupKey);
47          }
48  
49          public Builder groupName(final String groupName) {
50              instance.setGroupName(groupName);
51              return this;
52          }
53  
54          public Builder plainAttr(final Attr plainAttr) {
55              instance.getPlainAttrs().add(plainAttr);
56              return this;
57          }
58  
59          public Builder plainAttrs(final Attr... plainAttrs) {
60              instance.getPlainAttrs().addAll(List.of(plainAttrs));
61              return this;
62          }
63  
64          public Builder plainAttrs(final Collection<Attr> plainAttrs) {
65              instance.getPlainAttrs().addAll(plainAttrs);
66              return this;
67          }
68  
69          public Builder virAttr(final Attr virAttr) {
70              instance.getVirAttrs().add(virAttr);
71              return this;
72          }
73  
74          public Builder virAttrs(final Collection<Attr> virAttrs) {
75              instance.getVirAttrs().addAll(virAttrs);
76              return this;
77          }
78  
79          public Builder virAttrs(final Attr... virAttrs) {
80              instance.getVirAttrs().addAll(List.of(virAttrs));
81              return this;
82          }
83  
84          public MembershipTO build() {
85              return instance;
86          }
87      }
88  
89      private String groupKey;
90  
91      private String groupName;
92  
93      private final Set<Attr> plainAttrs = new TreeSet<>();
94  
95      private final Set<Attr> derAttrs = new TreeSet<>();
96  
97      private final Set<Attr> virAttrs = new TreeSet<>();
98  
99      public String getGroupKey() {
100         return groupKey;
101     }
102 
103     public void setGroupKey(final String groupKey) {
104         this.groupKey = groupKey;
105     }
106 
107     public String getGroupName() {
108         return groupName;
109     }
110 
111     public void setGroupName(final String groupName) {
112         this.groupName = groupName;
113     }
114 
115     @JacksonXmlElementWrapper(localName = "plainAttrs")
116     @JacksonXmlProperty(localName = "plainAttr")
117     @Override
118     public Set<Attr> getPlainAttrs() {
119         return plainAttrs;
120     }
121 
122     @JsonIgnore
123     @Override
124     public Optional<Attr> getPlainAttr(final String schema) {
125         return plainAttrs.stream().filter(attr -> attr.getSchema().equals(schema)).findFirst();
126     }
127 
128     @JacksonXmlElementWrapper(localName = "derAttrs")
129     @JacksonXmlProperty(localName = "derAttr")
130     @Override
131     public Set<Attr> getDerAttrs() {
132         return derAttrs;
133     }
134 
135     @JsonIgnore
136     @Override
137     public Optional<Attr> getDerAttr(final String schema) {
138         return derAttrs.stream().filter(attr -> attr.getSchema().equals(schema)).findFirst();
139     }
140 
141     @JacksonXmlElementWrapper(localName = "virAttrs")
142     @JacksonXmlProperty(localName = "virAttr")
143     @Override
144     public Set<Attr> getVirAttrs() {
145         return virAttrs;
146     }
147 
148     @JsonIgnore
149     @Override
150     public Optional<Attr> getVirAttr(final String schema) {
151         return virAttrs.stream().filter(attr -> attr.getSchema().equals(schema)).findFirst();
152     }
153 
154     @Override
155     public int hashCode() {
156         return new HashCodeBuilder().
157                 append(groupKey).
158                 append(groupName).
159                 append(plainAttrs).
160                 append(derAttrs).
161                 append(virAttrs).
162                 build();
163     }
164 
165     @Override
166     public boolean equals(final Object obj) {
167         if (this == obj) {
168             return true;
169         }
170         if (obj == null) {
171             return false;
172         }
173         if (getClass() != obj.getClass()) {
174             return false;
175         }
176         final MembershipTO other = (MembershipTO) obj;
177         return new EqualsBuilder().
178                 append(groupKey, other.groupKey).
179                 append(groupName, other.groupName).
180                 append(plainAttrs, other.plainAttrs).
181                 append(derAttrs, other.derAttrs).
182                 append(virAttrs, other.virAttrs).
183                 build();
184     }
185 
186     @Override
187     public String toString() {
188         return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).
189                 append(groupKey).
190                 append(groupName).
191                 build();
192     }
193 }