public static void validateMembership ( Annotation propertySet ) { Class c = propertySet . annotationType ( ) ; MembershipRule rule = ( MembershipRule ) c . getAnnotation ( MembershipRule . class ) ; if ( rule == null ) return ; MembershipRuleValues ruleValue = rule . value ( ) ; String [ ] memberNames = rule . memberNames ( ) ; Method [ ] members = getMembers ( c , memberNames ) ; int i = getNumOfMembersSet ( propertySet , members ) ; if ( ruleValue == MembershipRuleValues . ALL_IF_ANY ) { if ( i != 0 && i != members . length ) throw new IllegalArgumentException ( "The membership rule on " + propertySet . toString ( ) + " is not satisfied. Either all members must be set or none is set" ) ; } else if ( ruleValue == MembershipRuleValues . EXACTLY_ONE ) { if ( i != 1 ) throw new IllegalArgumentException ( "The membership rule on " + propertySet . toString ( ) + " is not satisfied. Exactly one member must be set" ) ; } else if ( ruleValue == MembershipRuleValues . AT_LEAST_ONE ) { if ( i < 1 ) throw new IllegalArgumentException ( "The membership rule on " + propertySet . toString ( ) + " is not satisfied. At least one member must be set" ) ; } else if ( ruleValue == MembershipRuleValues . AT_MOST_ONE ) { if ( i > 1 ) throw new IllegalArgumentException ( "The membership rule on " + propertySet . toString ( ) + " is not satisfied. At most one member may be set" ) ; } }
This method ensures the membership constraints defined on a property set is satisfied .