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 MemberOfCheck extends AbstractAnnotationCheck<MemberOf> {
028: private static final long serialVersionUID = 1L;
029:
030: private boolean ignoreCase;
031: private List<String> members;
032: private transient List<String> membersLowerCase;
033:
034: @Override
035: public void configure(final MemberOf constraintAnnotation) {
036: super .configure(constraintAnnotation);
037: setIgnoreCase(constraintAnnotation.ignoreCase());
038: setMembers(constraintAnnotation.value());
039: }
040:
041: /**
042: * @return the members
043: */
044: public List<String> getMembers() {
045: final List<String> v = CollectionFactoryHolder.getFactory()
046: .createList();
047: v.addAll(members);
048: return v;
049: }
050:
051: private List<String> getMembersLowerCase() {
052: if (membersLowerCase == null) {
053: membersLowerCase = CollectionFactoryHolder.getFactory()
054: .createList(members.size());
055: for (final String val : members) {
056: membersLowerCase.add(val.toLowerCase());
057: }
058: }
059: return membersLowerCase;
060: }
061:
062: @Override
063: public Map<String, String> getMessageVariables() {
064: final Map<String, String> messageVariables = CollectionFactoryHolder
065: .getFactory().createMap(2);
066: messageVariables
067: .put("ignoreCase", Boolean.toString(ignoreCase));
068: messageVariables.put("members", StringUtils.implode(members,
069: ","));
070: return messageVariables;
071: }
072:
073: /**
074: * @return the ignoreCase
075: */
076: public boolean isIgnoreCase() {
077: return ignoreCase;
078: }
079:
080: public boolean isSatisfied(final Object validatedObject,
081: final Object value, final OValContext context,
082: final Validator validator) {
083: if (value == null)
084: return true;
085:
086: if (ignoreCase)
087: return getMembersLowerCase().contains(
088: value.toString().toLowerCase());
089:
090: return members.contains(value.toString());
091: }
092:
093: /**
094: * @param ignoreCase the ignoreCase to set
095: */
096: public void setIgnoreCase(final boolean ignoreCase) {
097: this .ignoreCase = ignoreCase;
098: }
099:
100: /**
101: * @param members the members to set
102: */
103: public void setMembers(final List<String> members) {
104: this .members = CollectionFactoryHolder.getFactory()
105: .createList();
106: this .members.addAll(members);
107: membersLowerCase = null;
108: }
109:
110: /**
111: * @param members the members to set
112: */
113: public void setMembers(final String... members) {
114: this.members = CollectionFactoryHolder.getFactory()
115: .createList();
116: ArrayUtils.addAll(this.members, members);
117: membersLowerCase = null;
118: }
119: }
|