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;
Builds constraint implementation from constraint annotations.
public class ConstraintFactory
{
    
Create a list of constraints based on the provided annotations.
    public static List<ConstraintcreateConstraints(Annotation... annotations)
    {
        final ArrayList<Constraintconstraints = Lists.newArrayList();
        for (Annotation annotation : annotations)
        {
            if (isConstraintAnnotation(annotation.annotationType()))
            {
                constraints.add(createImplementation(annotation));
            }
        }
        return constraints;
    }

    
Returns true if the provided annotation is a constraint annotation.
    static boolean isConstraintAnnotation(Class<? extends Annotationann)
    {
        return ann.isAnnotationPresent(IsConstraint.class);
    }

    
Creates a class implementing the provided constraint annotation.
    {
        final IsConstraint constraintAnnotation = ann.annotationType().getAnnotation(
            IsConstraint.class);
        final Class<?> implClass = constraintAnnotation.implementation();
        if (!Constraint.class.isAssignableFrom(implClass))
        {
            throw new IllegalArgumentException("Implementation class "
                + implClass.getClass().getName() + " must implement "
                + Constraint.class.getName());
        }
        Constraint instance;
        try
        {
            instance = (ConstraintimplClass.newInstance();
        }
        catch (Exception e)
        {
            throw Throwables.propagate(e);
        }
        instance.populate(ann);
        return instance;
    }
New to GrepCode? Check out our FAQ X