View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2015 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import static net.sf.oval.Validator.*;
16  
17  import java.util.List;
18  import java.util.Map;
19  
20  import net.sf.oval.ConstraintTarget;
21  import net.sf.oval.Validator;
22  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
23  import net.sf.oval.context.OValContext;
24  import net.sf.oval.internal.util.ArrayUtils;
25  import net.sf.oval.internal.util.StringUtils;
26  
27  /**
28   * @author Sebastian Thomschke
29   */
30  public class MemberOfCheck extends AbstractAnnotationCheck<MemberOf>
31  {
32  	private static final long serialVersionUID = 1L;
33  
34  	private boolean ignoreCase;
35  	private List<String> members;
36  	private transient List<String> membersLowerCase;
37  
38  	/**
39  	 * {@inheritDoc}
40  	 */
41  	@Override
42  	public void configure(final MemberOf constraintAnnotation)
43  	{
44  		super.configure(constraintAnnotation);
45  		setIgnoreCase(constraintAnnotation.ignoreCase());
46  		setMembers(constraintAnnotation.value());
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	@Override
53  	protected Map<String, String> createMessageVariables()
54  	{
55  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
56  		messageVariables.put("ignoreCase", Boolean.toString(ignoreCase));
57  		messageVariables.put("members", StringUtils.implode(members, ","));
58  		return messageVariables;
59  	}
60  
61  	/**
62  	 * {@inheritDoc}
63  	 */
64  	@Override
65  	protected ConstraintTarget[] getAppliesToDefault()
66  	{
67  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
68  	}
69  
70  	/**
71  	 * @return the members
72  	 */
73  	public List<String> getMembers()
74  	{
75  		final List<String> v = getCollectionFactory().createList();
76  		v.addAll(members);
77  		return v;
78  	}
79  
80  	private List<String> getMembersLowerCase()
81  	{
82  		if (membersLowerCase == null)
83  		{
84  			membersLowerCase = getCollectionFactory().createList(members.size());
85  			for (final String val : members)
86  			{
87  				membersLowerCase.add(val.toLowerCase(Validator.getLocaleProvider().getLocale()));
88  			}
89  		}
90  		return membersLowerCase;
91  	}
92  
93  	/**
94  	 * @return the ignoreCase
95  	 */
96  	public boolean isIgnoreCase()
97  	{
98  		return ignoreCase;
99  	}
100 
101 	/**
102 	 * {@inheritDoc}
103 	 */
104 	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
105 			final Validator validator)
106 	{
107 		if (valueToValidate == null) return true;
108 
109 		if (ignoreCase)
110 			return getMembersLowerCase().contains(valueToValidate.toString().toLowerCase(Validator.getLocaleProvider().getLocale()));
111 
112 		return members.contains(valueToValidate.toString());
113 	}
114 
115 	/**
116 	 * @param ignoreCase the ignoreCase to set
117 	 */
118 	public void setIgnoreCase(final boolean ignoreCase)
119 	{
120 		this.ignoreCase = ignoreCase;
121 		requireMessageVariablesRecreation();
122 	}
123 
124 	/**
125 	 * @param members the members to set
126 	 */
127 	public void setMembers(final List<String> members)
128 	{
129 		this.members = getCollectionFactory().createList();
130 		this.members.addAll(members);
131 		membersLowerCase = null;
132 		requireMessageVariablesRecreation();
133 	}
134 
135 	/**
136 	 * @param members the members to set
137 	 */
138 	public void setMembers(final String... members)
139 	{
140 		this.members = getCollectionFactory().createList();
141 		ArrayUtils.addAll(this.members, members);
142 		membersLowerCase = null;
143 		requireMessageVariablesRecreation();
144 	}
145 }