All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.anteros.bean.validation.constraints.GreaterThan Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package br.com.anteros.bean.validation.constraints;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.math.BigDecimal;
import java.util.Comparator;

import br.com.anteros.bean.validation.constraints.validators.GreaterThanValidator;
import br.com.anteros.validation.api.Constraint;
import br.com.anteros.validation.api.Payload;

@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = GreaterThanValidator.class)
public @interface GreaterThan {
	
	String message() default "{br.com.anteros.bean.validation.constraints.GreaterThan.message}";

    Class[] groups() default {};

    Class[] payload() default {};

    String field();

    String greaterThan();

    Class> comparator() default DefaultComparator.class;

    @Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @interface List {
        GreaterThan [] value();
    }
    
    public class DefaultComparator implements Comparator {

    	@Override
    	public int compare(Object o1, Object o2) {
    		if (o1 instanceof Number) {
    			BigDecimal b1 = new BigDecimal(((Number)o1).doubleValue());
    		    BigDecimal b2 = new BigDecimal(((Number)o2).doubleValue());
    		    return b1.compareTo(b2);
    		} else if (o1 instanceof String) {
    			return ((String)o1).compareTo(((String)o2));
    		}
    		return 0;
    	}
    	
    }
}