2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorlicense.dao.types;
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;
25 import java.util.HashSet;
28 @UDT(keyspace = "dox", name = "multi_choice_or_other")
29 public class MultiChoiceOrOther<E extends Enum<E>> {
31 private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
32 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
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'";
37 public static final String OTHER_ENUM_VALUE = "Other";
40 private Set<E> choices;
45 private Set<String> results;
47 public MultiChoiceOrOther() {
48 // Default constructor
52 * Instantiates a new Multi choice or other.
54 * @param choices the choices
55 * @param other the other
57 public MultiChoiceOrOther(Set<E> choices, String other) {
58 this.choices = choices;
60 results = resolveResult();
63 public Set<E> getChoices() {
67 public void setChoices(Set<E> choices) {
68 this.choices = choices;
71 public String getOther() {
75 public void setOther(String other) {
79 public Set<String> getResults() {
86 * @param results the results
88 public void setResults(Set<String> results) {
89 if (choices != null) {
90 if (results == null) {
91 this.results = resolveResult();
94 this.results = results;
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())) {
104 for (E choice : choices) {
105 results.add(choice.name());
116 * @param enumClass the enum class
118 public void resolveEnum(Class<E> enumClass) {
119 if (choices != null || CollectionUtils.isEmpty(results)) {
123 choices = new HashSet<>();
124 if (results.size() > 1) {
125 for (String result : results) {
126 choices.add(E.valueOf(enumClass, result));
129 String result = results.iterator().next();
131 choices.add(E.valueOf(enumClass, result));
132 } catch (IllegalArgumentException exception) {
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());
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);
154 public boolean equals(Object obj) {
158 if (obj == null || getClass() != obj.getClass()) {
162 MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
164 if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
167 if (other != null ? !other.equals(that.other) : that.other != null) {
170 return results != null ? results.equals(that.results) : that.results == null;