View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 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.Collection;
18  import java.util.List;
19  import java.util.Map;
20  import java.util.regex.Pattern;
21  
22  import net.sf.oval.ConstraintTarget;
23  import net.sf.oval.Validator;
24  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
25  import net.sf.oval.context.OValContext;
26  import net.sf.oval.internal.util.ArrayUtils;
27  
28  /**
29   * @author Sebastian Thomschke
30   */
31  public class NotMatchPatternCheck extends AbstractAnnotationCheck<NotMatchPattern>
32  {
33  	private static final long serialVersionUID = 1L;
34  
35  	private final List<Pattern> patterns = getCollectionFactory().createList(2);
36  
37  	/**
38  	 * {@inheritDoc}
39  	 */
40  	@Override
41  	public void configure(final NotMatchPattern constraintAnnotation)
42  	{
43  		super.configure(constraintAnnotation);
44  
45  		synchronized (patterns)
46  		{
47  			patterns.clear();
48  			final String[] stringPatterns = constraintAnnotation.pattern();
49  			final int[] f = constraintAnnotation.flags();
50  			for (int i = 0, l = stringPatterns.length; i < l; i++)
51  			{
52  				final int flag = f.length > i ? f[i] : 0;
53  				final Pattern p = Pattern.compile(stringPatterns[i], flag);
54  				patterns.add(p);
55  			}
56  			requireMessageVariablesRecreation();
57  		}
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	@Override
64  	protected Map<String, String> createMessageVariables()
65  	{
66  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
67  		messageVariables.put("pattern", patterns.size() == 1 ? patterns.get(0).toString() : patterns.toString());
68  		return messageVariables;
69  	}
70  
71  	/**
72  	 * {@inheritDoc}
73  	 */
74  	@Override
75  	protected ConstraintTarget[] getAppliesToDefault()
76  	{
77  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
78  	}
79  
80  	/**
81  	 * @return the pattern
82  	 */
83  	public Pattern[] getPatterns()
84  	{
85  		synchronized (patterns)
86  		{
87  			return patterns.toArray(new Pattern[patterns.size()]);
88  		}
89  	}
90  
91  	/**
92  	 * {@inheritDoc}
93  	 */
94  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
95  			final Validator validator)
96  	{
97  		if (valueToValidate == null) return true;
98  
99  		for (final Pattern p : patterns)
100 			if (p.matcher(valueToValidate.toString()).matches()) return false;
101 		return true;
102 	}
103 
104 	/**
105 	 * @param pattern the pattern to set
106 	 */
107 	public void setPattern(final Pattern pattern)
108 	{
109 		synchronized (patterns)
110 		{
111 			patterns.clear();
112 			patterns.add(pattern);
113 		}
114 		requireMessageVariablesRecreation();
115 	}
116 
117 	/**
118 	 * @param pattern the pattern to set
119 	 */
120 	public void setPattern(final String pattern, final int flags)
121 	{
122 		synchronized (patterns)
123 		{
124 			patterns.clear();
125 			patterns.add(Pattern.compile(pattern, flags));
126 		}
127 		requireMessageVariablesRecreation();
128 	}
129 
130 	/**
131 	 * @param patterns the patterns to set
132 	 */
133 	public void setPatterns(final Collection<Pattern> patterns)
134 	{
135 		synchronized (this.patterns)
136 		{
137 			this.patterns.clear();
138 			this.patterns.addAll(patterns);
139 		}
140 		requireMessageVariablesRecreation();
141 	}
142 
143 	/**
144 	 * @param patterns the patterns to set
145 	 */
146 	public void setPatterns(final Pattern... patterns)
147 	{
148 		synchronized (this.patterns)
149 		{
150 			this.patterns.clear();
151 			ArrayUtils.addAll(this.patterns, patterns);
152 		}
153 		requireMessageVariablesRecreation();
154 	}
155 }