aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/dao/types/ChoiceOrOther.java
blob: ca7a09b0df1825cc38148b417f892dfda2d10c96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorlicense.dao.types;

import com.datastax.driver.mapping.annotations.Transient;
import com.datastax.driver.mapping.annotations.UDT;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCategory;
import org.openecomp.sdc.common.errors.ErrorCode;

@UDT(keyspace = "dox", name = "choice_or_other")
public class ChoiceOrOther<E extends Enum<E>> {

  public static final String CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
      "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
  public static final String CHOICE_OR_OTHER_INVALID_ENUM_MSG =
      "Enum used as part of ChoiceOrOther type must contain the value 'Other'";
  public static final String OTHER_ENUM_VALUE = "Other";

  @Transient
  private E choice;

  @Transient
  private String other;

  private String result;

  public ChoiceOrOther() {
  }

  /**
   * Instantiates a new Choice or other.
   *
   * @param choice the choice
   * @param other  the other
   */
  public ChoiceOrOther(E choice, String other) {
    this.choice = choice;
    this.other = other;
    result = resolveResult();
  }

  public E getChoice() {
    return choice;
  }

  public void setChoice(E choice) {

    this.choice = choice;
  }

  public String getOther() {
    return other;
  }

  public void setOther(String other) {
    this.other = other;
  }

  public String getResult() {
    return result;
  }

  /**
   * Sets result.
   *
   * @param result the result
   */
  public void setResult(String result) {
    if (choice != null) {
      if (result == null) {
        this.result = resolveResult();
      }
    } else {
      this.result = result;
    }
  }

  private String resolveResult() {
    return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
  }

  /**
   * Resolve enum.
   *
   * @param enumClass the enum class
   */
  public void resolveEnum(Class<E> enumClass) {
    if (choice != null || result == null) {
      return;
    }

    try {
      choice = E.valueOf(enumClass, result);
    } catch (IllegalArgumentException e0) {
      try {
        choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
      } catch (IllegalArgumentException ex) {
        throw new CoreException(new ErrorCode.ErrorCodeBuilder()
            .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
            .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG)
            .withCategory(ErrorCategory.APPLICATION).build());
      }
      other = result;
    }
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
      return false;
    }

    ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;

    if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
      return false;
    }
    if (other != null ? !other.equals(that.other) : that.other != null) {
      return false;
    }
    return result != null ? result.equals(that.result) : that.result == null;

  }

  @Override
  public int hashCode() {
    int result1 = choice != null ? choice.hashCode() : 0;
    result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
    result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
    return result1;
  }
}