001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.krad.datadictionary.validation.constraint.resolver;
017    
018    import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
019    import org.kuali.rice.krad.datadictionary.validation.capability.LengthConstrainable;
020    import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
021    
022    import java.util.Collections;
023    import java.util.List;
024    
025    /**
026     * An object that returns the constrainable definition itself as a list for a definition implementing the capability
027     * {@link Constrainable}.
028     * This definition must also implement the interface {@link Constraint}, or a ClassCastException will be thrown.
029     *
030     * An example is {@link LengthConstrainable}, where members of the definition itself need to be made available to the
031     * ConstraintProcessor.
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class DefinitionConstraintResolver<T extends Constrainable> implements ConstraintResolver<T> {
036    
037        @Override
038        public <C extends Constraint> List<C> resolve(T definition) throws ClassCastException {
039            if (definition instanceof Constraint) {
040                @SuppressWarnings("unchecked") C constraint = (C) definition;
041                return Collections.singletonList(constraint);
042            }
043            /*        else if(definiton instanceof InputField){
044                C constraint = (C)definition.get
045            }*/
046            throw new ClassCastException(
047                    "DefinitionConstraintResolver can only be used for a definition that implements both Constraint and Constrainable, or derives from a class that does.");
048        }
049    
050    }