01a4cac6b8afbca48262f5d1177dd4548aed0881
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.dao.types;
18
19 import com.datastax.driver.mapping.annotations.Transient;
20 import com.datastax.driver.mapping.annotations.UDT;
21 import org.apache.commons.collections4.CollectionUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24
25 import java.util.HashSet;
26 import java.util.Set;
27
28 @UDT(keyspace = "dox", name = "multi_choice_or_other")
29 public class MultiChoiceOrOther<E extends Enum<E>> {
30
31   private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
32       "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
33
34   private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
35       "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
36
37   public static final String OTHER_ENUM_VALUE = "Other";
38
39   @Transient
40   private Set<E> choices;
41   @Transient
42   private String other;
43
44
45   private Set<String> results;
46
47   public MultiChoiceOrOther() {
48     // Default constructor
49   }
50
51   /**
52    * Instantiates a new Multi choice or other.
53    *
54    * @param choices the choices
55    * @param other   the other
56    */
57   public MultiChoiceOrOther(Set<E> choices, String other) {
58     this.choices = choices;
59     this.other = other;
60     results = resolveResult();
61   }
62
63   public Set<E> getChoices() {
64     return choices;
65   }
66
67   public void setChoices(Set<E> choices) {
68     this.choices = choices;
69   }
70
71   public String getOther() {
72     return other;
73   }
74
75   public void setOther(String other) {
76     this.other = other;
77   }
78
79   public Set<String> getResults() {
80     return results;
81   }
82
83   /**
84    * Sets results.
85    *
86    * @param results the results
87    */
88   public void setResults(Set<String> results) {
89     if (choices != null) {
90       if (results == null) {
91         this.results = resolveResult();
92       }
93     } else {
94       this.results = results;
95     }
96   }
97
98   private Set<String> resolveResult() {
99     if (choices != null) {
100         results = new HashSet<>();
101         if (choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
102             results.add(other);
103         } else {
104             for (E choice : choices) {
105                 results.add(choice.name());
106             }
107         }
108     }
109
110     return results;
111   }
112
113   /**
114    * Resolve enum.
115    *
116    * @param enumClass the enum class
117    */
118   public void resolveEnum(Class<E> enumClass) {
119     if (choices != null || CollectionUtils.isEmpty(results)) {
120       return;
121     }
122
123     choices = new HashSet<>();
124     if (results.size() > 1) {
125       for (String result : results) {
126         choices.add(E.valueOf(enumClass, result));
127       }
128     } else {
129       String result = results.iterator().next();
130       try {
131         choices.add(E.valueOf(enumClass, result));
132       } catch (IllegalArgumentException exception) {
133         try {
134           choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
135         } catch (IllegalArgumentException ex) {
136           throw new CoreException(new ErrorCode.ErrorCodeBuilder()
137                   .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
138                   .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG).build());
139         }
140         other = result;
141       }
142     }
143   }
144
145   @Override
146   public int hashCode() {
147     int result = choices != null ? choices.hashCode() : 0;
148     result = 31 * result + (other != null ? other.hashCode() : 0);
149     result = 31 * result + (results != null ? results.hashCode() : 0);
150     return result;
151   }
152
153   @Override
154   public boolean equals(Object obj) {
155     if (this == obj) {
156       return true;
157     }
158     if (obj == null || getClass() != obj.getClass()) {
159       return false;
160     }
161
162     MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
163
164     if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
165       return false;
166     }
167     if (other != null ? !other.equals(that.other) : that.other != null) {
168       return false;
169     }
170     return results != null ? results.equals(that.results) : that.results == null;
171
172   }
173 }