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;
20  
21  import com.fasterxml.jackson.annotation.JsonProperty;
22  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
23  import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  import javax.ws.rs.PathParam;
28  import org.apache.commons.lang3.builder.CompareToBuilder;
29  import org.apache.commons.lang3.builder.EqualsBuilder;
30  import org.apache.commons.lang3.builder.HashCodeBuilder;
31  
32  public class Attr implements Comparable<Attr>, BaseBean {
33  
34      private static final long serialVersionUID = 4941691338796323623L;
35  
36      public static class Builder {
37  
38          private final Attr instance = new Attr();
39  
40          public Builder(final String schema) {
41              instance.setSchema(schema);
42          }
43  
44          public Builder value(final String value) {
45              instance.getValues().add(value);
46              return this;
47          }
48  
49          public Builder values(final String... values) {
50              instance.getValues().addAll(List.of(values));
51              return this;
52          }
53  
54          public Builder values(final Collection<String> values) {
55              instance.getValues().addAll(values);
56              return this;
57          }
58  
59          public Attr build() {
60              return instance;
61          }
62      }
63  
64      /**
65       * Name of the schema that this attribute is referring to.
66       */
67      private String schema;
68  
69      /**
70       * Set of (string) values of this attribute.
71       */
72      private final List<String> values = new ArrayList<>();
73  
74      /**
75       * @return the name of the schema that this attribute is referring to
76       */
77      @JsonProperty(required = true)
78      public String getSchema() {
79          return schema;
80      }
81  
82      /**
83       * @param schema name to be set
84       */
85      @PathParam("schema")
86      public void setSchema(final String schema) {
87          this.schema = schema;
88  
89      }
90  
91      /**
92       * @return attribute values as strings
93       */
94      @JacksonXmlElementWrapper(localName = "values")
95      @JacksonXmlProperty(localName = "value")
96      @JsonProperty(required = true)
97      public List<String> getValues() {
98          return values;
99      }
100 
101     @Override
102     public int compareTo(final Attr other) {
103         return equals(other)
104                 ? 0
105                 : new CompareToBuilder().
106                         append(schema, other.schema).
107                         append(values.toArray(), other.values.toArray()).
108                         toComparison();
109     }
110 
111     @Override
112     public int hashCode() {
113         return new HashCodeBuilder().
114                 append(schema).
115                 append(values).
116                 build();
117     }
118 
119     @Override
120     public boolean equals(final Object obj) {
121         if (this == obj) {
122             return true;
123         }
124         if (obj == null) {
125             return false;
126         }
127         if (getClass() != obj.getClass()) {
128             return false;
129         }
130         final Attr other = (Attr) obj;
131         return new EqualsBuilder().
132                 append(schema, other.schema).
133                 append(values, other.values).
134                 build();
135     }
136 }