001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.constraint;
013:
014: import java.util.List;
015: import java.util.Map;
016:
017: import net.sf.oval.Validator;
018: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
019: import net.sf.oval.context.OValContext;
020: import net.sf.oval.internal.CollectionFactoryHolder;
021: import net.sf.oval.internal.util.ArrayUtils;
022: import net.sf.oval.internal.util.StringUtils;
023:
024: /**
025: * @author Sebastian Thomschke
026: */
027: public class NotMemberOfCheck extends
028: AbstractAnnotationCheck<NotMemberOf> {
029: private static final long serialVersionUID = 1L;
030:
031: private boolean ignoreCase;
032: private List<String> members;
033: private transient List<String> membersLowerCase;
034:
035: @Override
036: public void configure(final NotMemberOf constraintAnnotation) {
037: super .configure(constraintAnnotation);
038: setIgnoreCase(constraintAnnotation.ignoreCase());
039: setMembers(constraintAnnotation.value());
040: }
041:
042: /**
043: * @return the members
044: */
045: public List<String> getMembers() {
046: final List<String> v = CollectionFactoryHolder.getFactory()
047: .createList();
048: v.addAll(members);
049: return v;
050: }
051:
052: private List<String> getMembersLowerCase() {
053: if (membersLowerCase == null) {
054: membersLowerCase = CollectionFactoryHolder.getFactory()
055: .createList(members.size());
056: for (final String val : members) {
057: membersLowerCase.add(val.toLowerCase());
058: }
059: }
060: return membersLowerCase;
061: }
062:
063: @Override
064: public Map<String, String> getMessageVariables() {
065: final Map<String, String> messageVariables = CollectionFactoryHolder
066: .getFactory().createMap(2);
067: messageVariables
068: .put("ignoreCase", Boolean.toString(ignoreCase));
069: messageVariables.put("members", StringUtils.implode(members,
070: ","));
071: return messageVariables;
072: }
073:
074: /**
075: * @return the ignoreCase
076: */
077: public boolean isIgnoreCase() {
078: return ignoreCase;
079: }
080:
081: public boolean isSatisfied(final Object validatedObject,
082: final Object value, final OValContext context,
083: final Validator validator) {
084: if (value == null)
085: return true;
086:
087: if (ignoreCase)
088: return !getMembersLowerCase().contains(
089: value.toString().toLowerCase());
090:
091: return !members.contains(value.toString());
092: }
093:
094: /**
095: * @param ignoreCase the ignoreCase to set
096: */
097: public void setIgnoreCase(final boolean ignoreCase) {
098: this .ignoreCase = ignoreCase;
099: }
100:
101: /**
102: * @param members the members to set
103: */
104: public void setMembers(final List<String> members) {
105: this .members = CollectionFactoryHolder.getFactory()
106: .createList();
107: this .members.addAll(members);
108: membersLowerCase = null;
109: }
110:
111: /**
112: * @param members the members to set
113: */
114: public void setMembers(final String... members) {
115: this.members = CollectionFactoryHolder.getFactory()
116: .createList();
117: ArrayUtils.addAll(this.members, members);
118: membersLowerCase = null;
119: }
120: }
|