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 @UDT(keyspace = "dox", name = "choice_or_other")
30 public class ChoiceOrOther<E extends Enum<E>> {
32 public static final String CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
33 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
34 public static final String CHOICE_OR_OTHER_INVALID_ENUM_MSG =
35 "Enum used as part of ChoiceOrOther type must contain the value 'Other'";
36 public static final String OTHER_ENUM_VALUE = "Other";
44 private String result;
46 public ChoiceOrOther() {
50 * Instantiates a new Choice or other.
52 * @param choice the choice
53 * @param other the other
55 public ChoiceOrOther(E choice, String other) {
58 result = resolveResult();
61 public E getChoice() {
65 public void setChoice(E choice) {
70 public String getOther() {
74 public void setOther(String other) {
78 public String getResult() {
85 * @param result the result
87 public void setResult(String result) {
90 this.result = resolveResult();
97 private String resolveResult() {
98 return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
104 * @param enumClass the enum class
106 public void resolveEnum(Class<E> enumClass) {
107 if (choice != null || result == null) {
112 choice = E.valueOf(enumClass, result);
113 } catch (IllegalArgumentException e0) {
115 choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
116 } catch (IllegalArgumentException ex) {
117 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
118 .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
119 .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG)
120 .withCategory(ErrorCategory.APPLICATION).build());
127 public boolean equals(Object obj) {
131 if (obj == null || getClass() != obj.getClass()) {
135 ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
137 if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
140 if (other != null ? !other.equals(that.other) : that.other != null) {
143 return result != null ? result.equals(that.result) : that.result == null;
148 public int hashCode() {
149 int result1 = choice != null ? choice.hashCode() : 0;
150 result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
151 result1 = 31 * result1 + (result != null ? result.hashCode() : 0);