View Javadoc
1   package org.pojomatic.annotations;
2   
3   import java.util.Arrays;
4   import java.util.Collections;
5   import java.util.EnumSet;
6   import java.util.Set;
7   
8   import org.pojomatic.Pojomatic;
9   import org.pojomatic.internal.PropertyRole;
10  
11  /**
12   * A policy for defining which sets of {@link Pojomatic} operations
13   * ({@code equals}, {@code hashCode} and {@code toString}) should use a property.
14   * This is set using {@link Property}.
15   * @see DefaultPojomaticPolicy
16   */
17  public enum PojomaticPolicy {
18  
19    /**
20     * Use the property for both {@code hashCode} and {@code equals}.
21     * Anything included in {@code public int hashCode()} should also be included in
22     * {@code public boolean equals(Object)} to preserve the general
23     * contract of {@link Object#hashCode()}.
24     *
25     * @see Object#hashCode()
26     * @see Object#equals(Object)
27     */
28    HASHCODE_EQUALS(PropertyRole.HASH_CODE, PropertyRole.EQUALS),
29  
30    /**
31     * Use the property for both {@code equals} and {@code toString}.
32     *
33     * @see Object#equals(Object)
34     * @see Object#toString()
35     */
36    EQUALS_TO_STRING(PropertyRole.EQUALS, PropertyRole.TO_STRING),
37  
38  
39    /**
40     * Use the property for {@code equals} only.
41     *
42     * @see Object#equals(Object)
43     */
44    EQUALS(PropertyRole.EQUALS),
45  
46    /**
47     * Use the property for both {@code toString} only.
48     *
49     * @see Object#toString()
50     */
51    TO_STRING(PropertyRole.TO_STRING),
52  
53    /**
54     * Use the property for {@code hashCode}, {@code equals} and {@code toString}.
55     */
56    ALL(PropertyRole.EQUALS, PropertyRole.HASH_CODE, PropertyRole.TO_STRING),
57  
58    /**
59     * Do not use the property for any of {@code hashCode}, {@code equals} or {@code toString}.
60     */
61    NONE(),
62  
63    /**
64     * Use the default policy specified via the {@code @AutoProperty} annotation, or
65     * {@code ALL} if none was specified.
66     */
67    DEFAULT {
68      @Override public Set<PropertyRole> getRoles() {
69        return null;
70      }
71    };
72  
73  
74    /**
75     * @return the roles this specified by this policy.  Will be {@code null} for {@code DEFAULT}.
76     */
77    public Set<PropertyRole> getRoles() {
78      return roles;
79    }
80  
81    private PojomaticPolicy(PropertyRole... roles) {
82      Set<PropertyRole> roleSet = EnumSet.noneOf(PropertyRole.class);
83      roleSet.addAll(Arrays.asList(roles));
84      this.roles = Collections.unmodifiableSet(roleSet);
85    }
86  
87    private final Set<PropertyRole> roles;
88  }