001    
002    /*
003     * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004     *
005     * $Id: Sorted.java 144 2011-10-13 22:31:52Z archie.cobbs $
006     */
007    
008    package org.dellroad.stuff.validation;
009    
010    import java.lang.annotation.Documented;
011    import java.lang.annotation.ElementType;
012    import java.lang.annotation.Retention;
013    import java.lang.annotation.RetentionPolicy;
014    import java.lang.annotation.Target;
015    import java.util.Comparator;
016    
017    import javax.validation.Constraint;
018    import javax.validation.Payload;
019    
020    /**
021     * Validation constraint that checks elements are sorted.
022     * Applies to non-primitive arrays, collections and maps; for maps, the keys are examined.
023     * If any element is null, it is skipped.
024     */
025    @Documented
026    @Constraint(validatedBy = SortedValidator.class)
027    @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
028    @Retention(RetentionPolicy.RUNTIME)
029    public @interface Sorted {
030    
031        String message() default "Collection is not sorted";
032    
033        Class<?>[] groups() default {};
034    
035        Class<? extends Payload>[] payload() default {};
036    
037        /**
038         * Specifies a {@link java.util.Comparator} to use. If none is specified, the natural sort ordering is used.
039         * The class must have a default constructor.
040         */
041        Class<? extends Comparator> comparator() default Comparator.class;
042    
043        /**
044         * Configures whether the sorting should be strict, i.e., whether adjacent equal elements should be disallowed.
045         */
046        boolean strict() default true;
047    }
048