001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.api.repository.typerelation;
017
018import org.kuali.rice.core.api.mo.common.Coded;
019import org.kuali.rice.core.api.util.jaxb.EnumStringAdapter;
020
021import java.util.HashSet;
022import java.util.Set;
023
024/**
025 * Enumeration for RelationshipTypes.  USAGE_ALLOWED or UNKNOWN.
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 *
029 */
030public enum RelationshipType implements Coded {
031
032    /**
033     * use this flag with the static factory to get a {@link RelationshipType} Unknown
034     */
035    UNKNOWN("U"),
036
037    /**
038     * use this flag with the static factory to get a {@link RelationshipType} Usage Allowed
039     */
040    USAGE_ALLOWED("A");
041
042    private final String code;
043
044    /**
045     * Create a RelationshipType of the given code
046     * @param code code the RelationshipType should be of.
047     */
048    private RelationshipType(String code) {
049        this.code = code;
050    }
051
052    /**
053     * Returns the operator code for this evaluation operator.
054     *
055     * @return the operatorCode
056     */
057    @Override
058    public String getCode() {
059        return code;
060    }
061
062    /**
063     * Set of valid type codes
064     */
065    public static final Set<String> VALID_TYPE_CODES = new HashSet<String>();
066    static {
067        for (RelationshipType relationshipType : values()) {
068            VALID_TYPE_CODES.add(relationshipType.getCode());
069        }
070    }
071
072    /**
073     * Create a RelationshipType for the given code
074     * @param code to type the RelationshipType
075     * @return RelationshipType of the given code
076     * @throws IllegalArgumentException if the given code does not exist
077     */
078    public static RelationshipType fromCode(String code) {
079        if (code == null) {
080            return null;
081        }
082        for (RelationshipType relationshipType : values()) {
083            if (relationshipType.code.equals(code)) {
084                return relationshipType;
085            }
086        }
087        throw new IllegalArgumentException("Failed to locate the RelationshipType with the given code: " + code);
088    }
089
090    static final class Adapter extends EnumStringAdapter<RelationshipType> {
091        @Override
092        protected Class<RelationshipType> getEnumClass() {
093            return RelationshipType.class;
094        }
095
096    }
097
098}