Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 
 /*
  * Carrot2 project.
  *
  * Copyright (C) 2002-2012, Dawid Weiss, Stanisław Osiński.
  * All rights reserved.
  *
  * Refer to the full license file "carrot2.LICENSE"
  * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */
package org.carrot2.util.attribute.constraint;
A base class for implementing constraints.
public abstract class Constraint
{
    
Annotation corresponding to this constraint
    protected Annotation annotation;

    
Checks if the provided value meets this constraint.
    protected abstract boolean isMet(Object value);

    
Checks if the provided value can be assigned to the type defined by clazz. If not, an java.lang.IllegalArgumentException will be thrown. null values are assignable to any class.
    protected void checkAssignableFrom(Object valueClass<?>... clazz)
    {
        /*
         * Null is assignable to anything.
         */
        if (value == null)
        {
            return;
        }
        if (clazz.length == 0)
        {
            throw new IllegalArgumentException("Classes array must not be empty");
        }
        
        for (Class<?> c : clazz)
        {
            if (c.isAssignableFrom(value.getClass()))
            {
                return;
            }
        }
        throw new IllegalArgumentException("Expected an instance of "
            + classesToString(clazz) + " but found " + value.getClass().getName());
    }
    private static String classesToString(Class<?>...classes)
    {
        if (classes.length == 1)
        {
            return classes[0].getName();
        }
        else
        {
            StringBuilder s = new StringBuilder("any of");
            s.append(classes[0].getName());
            for (int i = 1; i < classes.lengthi++)
            {
                s.append(", ");
                s.append(classes[i].getName());
            }
            return s.toString();
        }
    }

    
TODO: remove this method and replace with a constructor accepting Annotation this constraint is bound to.
    public final void populate(Annotation annotation)
    {
        this. = annotation;
        populateCustom(annotation);
    }
    protected void populateCustom(Annotation annotation)
    {
    }
New to GrepCode? Check out our FAQ X