001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 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 *******************************************************************************/ 013 package net.sf.oval.constraint; 014 015 import static net.sf.oval.Validator.getCollectionFactory; 016 017 import java.util.List; 018 import java.util.Locale; 019 import java.util.Map; 020 021 import net.sf.oval.ConstraintTarget; 022 import net.sf.oval.Validator; 023 import net.sf.oval.configuration.annotation.AbstractAnnotationCheck; 024 import net.sf.oval.context.OValContext; 025 import net.sf.oval.internal.util.ArrayUtils; 026 import net.sf.oval.internal.util.StringUtils; 027 028 /** 029 * @author Sebastian Thomschke 030 */ 031 public class MemberOfCheck extends AbstractAnnotationCheck<MemberOf> 032 { 033 private static final long serialVersionUID = 1L; 034 035 private boolean ignoreCase; 036 private List<String> members; 037 private transient List<String> membersLowerCase; 038 039 /** 040 * {@inheritDoc} 041 */ 042 @Override 043 public void configure(final MemberOf constraintAnnotation) 044 { 045 super.configure(constraintAnnotation); 046 setIgnoreCase(constraintAnnotation.ignoreCase()); 047 setMembers(constraintAnnotation.value()); 048 } 049 050 /** 051 * {@inheritDoc} 052 */ 053 @Override 054 protected Map<String, String> createMessageVariables() 055 { 056 final Map<String, String> messageVariables = getCollectionFactory().createMap(2); 057 messageVariables.put("ignoreCase", Boolean.toString(ignoreCase)); 058 messageVariables.put("members", StringUtils.implode(members, ",")); 059 return messageVariables; 060 } 061 062 /** 063 * {@inheritDoc} 064 */ 065 @Override 066 protected ConstraintTarget[] getAppliesToDefault() 067 { 068 return new ConstraintTarget[]{ConstraintTarget.VALUES}; 069 } 070 071 /** 072 * @return the members 073 */ 074 public List<String> getMembers() 075 { 076 final List<String> v = getCollectionFactory().createList(); 077 v.addAll(members); 078 return v; 079 } 080 081 private List<String> getMembersLowerCase() 082 { 083 if (membersLowerCase == null) 084 { 085 membersLowerCase = getCollectionFactory().createList(members.size()); 086 for (final String val : members) 087 membersLowerCase.add(val.toLowerCase(Locale.getDefault())); 088 } 089 return membersLowerCase; 090 } 091 092 /** 093 * @return the ignoreCase 094 */ 095 public boolean isIgnoreCase() 096 { 097 return ignoreCase; 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, 104 final Validator validator) 105 { 106 if (valueToValidate == null) return true; 107 108 if (ignoreCase) 109 return getMembersLowerCase().contains(valueToValidate.toString().toLowerCase(Locale.getDefault())); 110 111 return members.contains(valueToValidate.toString()); 112 } 113 114 /** 115 * @param ignoreCase the ignoreCase to set 116 */ 117 public void setIgnoreCase(final boolean ignoreCase) 118 { 119 this.ignoreCase = ignoreCase; 120 requireMessageVariablesRecreation(); 121 } 122 123 /** 124 * @param members the members to set 125 */ 126 public void setMembers(final List<String> members) 127 { 128 this.members = getCollectionFactory().createList(); 129 this.members.addAll(members); 130 membersLowerCase = null; 131 requireMessageVariablesRecreation(); 132 } 133 134 /** 135 * @param members the members to set 136 */ 137 public void setMembers(final String... members) 138 { 139 this.members = getCollectionFactory().createList(); 140 ArrayUtils.addAll(this.members, members); 141 membersLowerCase = null; 142 requireMessageVariablesRecreation(); 143 } 144 }