diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/dao/types/ChoiceOrOther.java')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/dao/types/ChoiceOrOther.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/dao/types/ChoiceOrOther.java b/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/dao/types/ChoiceOrOther.java new file mode 100644 index 0000000000..ca7a09b0df --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/dao/types/ChoiceOrOther.java @@ -0,0 +1,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; + } +} |