push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-api / src / main / java / org / openecomp / sdc / vendorlicense / dao / types / ChoiceOrOther.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.dao.types;
22
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;
28
29 @UDT(keyspace = "dox", name = "choice_or_other")
30 public class ChoiceOrOther<E extends Enum<E>> {
31
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";
37
38   @Transient
39   private E choice;
40
41   @Transient
42   private String other;
43
44   private String result;
45
46   public ChoiceOrOther() {
47   }
48
49   /**
50    * Instantiates a new Choice or other.
51    *
52    * @param choice the choice
53    * @param other  the other
54    */
55   public ChoiceOrOther(E choice, String other) {
56     this.choice = choice;
57     this.other = other;
58     result = resolveResult();
59   }
60
61   public E getChoice() {
62     return choice;
63   }
64
65   public void setChoice(E choice) {
66
67     this.choice = choice;
68   }
69
70   public String getOther() {
71     return other;
72   }
73
74   public void setOther(String other) {
75     this.other = other;
76   }
77
78   public String getResult() {
79     return result;
80   }
81
82   /**
83    * Sets result.
84    *
85    * @param result the result
86    */
87   public void setResult(String result) {
88     if (choice != null) {
89       if (result == null) {
90         this.result = resolveResult();
91       }
92     } else {
93       this.result = result;
94     }
95   }
96
97   private String resolveResult() {
98     return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
99   }
100
101   /**
102    * Resolve enum.
103    *
104    * @param enumClass the enum class
105    */
106   public void resolveEnum(Class<E> enumClass) {
107     if (choice != null || result == null) {
108       return;
109     }
110
111     try {
112       choice = E.valueOf(enumClass, result);
113     } catch (IllegalArgumentException e0) {
114       try {
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());
121       }
122       other = result;
123     }
124   }
125
126   @Override
127   public boolean equals(Object obj) {
128     if (this == obj) {
129       return true;
130     }
131     if (obj == null || getClass() != obj.getClass()) {
132       return false;
133     }
134
135     ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
136
137     if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
138       return false;
139     }
140     if (other != null ? !other.equals(that.other) : that.other != null) {
141       return false;
142     }
143     return result != null ? result.equals(that.result) : that.result == null;
144
145   }
146
147   @Override
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);
152     return result1;
153   }
154 }