2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorlicense.dao.types;
23 import com.datastax.driver.mapping.annotations.Transient;
24 import com.datastax.driver.mapping.annotations.UDT;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.ErrorCategory;
27 import org.openecomp.sdc.common.errors.ErrorCode;
29 import java.util.HashSet;
32 @UDT(keyspace = "dox", name = "multi_choice_or_other")
33 public class MultiChoiceOrOther<E extends Enum<E>> {
34 public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
35 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
36 public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
37 "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
38 public static final String OTHER_ENUM_VALUE = "Other";
41 private Set<E> choices;
46 private Set<String> results;
48 public MultiChoiceOrOther() {
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 results = new HashSet<>();
100 if (choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
103 for (E choice : choices) {
104 results.add(choice.name());
114 * @param enumClass the enum class
116 public void resolveEnum(Class<E> enumClass) {
117 if (choices != null || results == null || results.size() == 0) {
121 choices = new HashSet<>();
122 if (results.size() > 1) {
123 for (String result : results) {
124 choices.add(E.valueOf(enumClass, result));
127 String result = results.iterator().next();
129 choices.add(E.valueOf(enumClass, result));
130 } catch (IllegalArgumentException illegalArgumentException) {
132 choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
133 } catch (IllegalArgumentException ex) {
134 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
135 .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
136 .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG)
137 .withCategory(ErrorCategory.APPLICATION).build());
145 public boolean equals(Object obj) {
149 if (obj == null || getClass() != obj.getClass()) {
153 MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
155 if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
158 if (other != null ? !other.equals(that.other) : that.other != null) {
161 return results != null ? results.equals(that.results) : that.results == null;
166 public int hashCode() {
167 int result = choices != null ? choices.hashCode() : 0;
168 result = 31 * result + (other != null ? other.hashCode() : 0);
169 result = 31 * result + (results != null ? results.hashCode() : 0);