01699355f5112d36455cb98b3e53b79253d6fbac
[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.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.common.errors.ErrorCode;
23
24 @UDT(keyspace = "dox", name = "choice_or_other")
25 public class ChoiceOrOther<E extends Enum<E>> {
26
27   private static final String CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
28       "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
29   private static final String CHOICE_OR_OTHER_INVALID_ENUM_MSG =
30       "Enum used as part of ChoiceOrOther type must contain the value 'Other'";
31   public static final String OTHER_ENUM_VALUE = "Other";
32
33   @Transient
34   private E choice;
35
36   @Transient
37   private String other;
38
39   private String result;
40
41   public ChoiceOrOther() {
42     // the default constructor is used to instantiate by reflection
43   }
44
45   /**
46    * Instantiates a new Choice or other.
47    *
48    * @param choice the choice
49    * @param other  the other
50    */
51   public ChoiceOrOther(E choice, String other) {
52     this.choice = choice;
53     this.other = other;
54     result = resolveResult();
55   }
56
57   public E getChoice() {
58     return choice;
59   }
60
61   public void setChoice(E choice) {
62
63     this.choice = choice;
64   }
65
66   public String getOther() {
67     return other;
68   }
69
70   public void setOther(String other) {
71     this.other = other;
72   }
73
74   public String getResult() {
75     return result;
76   }
77
78   /**
79    * Sets result.
80    *
81    * @param result the result
82    */
83   public void setResult(String result) {
84     if (choice != null) {
85       if (result == null) {
86         this.result = resolveResult();
87       }
88     } else {
89       this.result = result;
90     }
91   }
92
93   private String resolveResult() {
94     return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
95   }
96
97   /**
98    * Resolve enum.
99    *
100    * @param enumClass the enum class
101    */
102   public void resolveEnum(Class<E> enumClass) {
103     if (choice != null || result == null) {
104       return;
105     }
106
107     try {
108       choice = E.valueOf(enumClass, result);
109     } catch (IllegalArgumentException exception) {
110       try {
111         choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
112       } catch (IllegalArgumentException ex) {
113         throw new CoreException(new ErrorCode.ErrorCodeBuilder()
114                 .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
115                 .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG).build());
116       }
117       other = result;
118     }
119   }
120
121   @Override
122   public int hashCode() {
123     int result1 = choice != null ? choice.hashCode() : 0;
124     result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
125     result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
126     return result1;
127   }
128
129   @Override
130   public boolean equals(Object obj) {
131     if (this == obj) {
132       return true;
133     }
134     if (obj == null || getClass() != obj.getClass()) {
135       return false;
136     }
137
138     ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
139
140     if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
141       return false;
142     }
143     if (other != null ? !other.equals(that.other) : that.other != null) {
144       return false;
145     }
146     return result != null ? result.equals(that.result) : that.result == null;
147
148   }
149 }