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.lang.reflect.Array; 018 import java.util.Collection; 019 import java.util.Map; 020 021 import net.sf.oval.Validator; 022 import net.sf.oval.configuration.annotation.AbstractAnnotationCheck; 023 import net.sf.oval.context.OValContext; 024 025 /** 026 * @author Sebastian Thomschke 027 */ 028 public class SizeCheck extends AbstractAnnotationCheck<Size> 029 { 030 private static final long serialVersionUID = 1L; 031 032 private int min; 033 private int max; 034 035 /** 036 * {@inheritDoc} 037 */ 038 @Override 039 public void configure(final Size constraintAnnotation) 040 { 041 super.configure(constraintAnnotation); 042 setMax(constraintAnnotation.max()); 043 setMin(constraintAnnotation.min()); 044 } 045 046 /** 047 * {@inheritDoc} 048 */ 049 @Override 050 protected Map<String, String> createMessageVariables() 051 { 052 final Map<String, String> messageVariables = getCollectionFactory().createMap(2); 053 messageVariables.put("max", Integer.toString(max)); 054 messageVariables.put("min", Integer.toString(min)); 055 return messageVariables; 056 } 057 058 /** 059 * @return the max 060 */ 061 public int getMax() 062 { 063 return max; 064 } 065 066 /** 067 * @return the min 068 */ 069 public int getMin() 070 { 071 return min; 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, 078 final Validator validator) 079 { 080 if (valueToValidate == null) return true; 081 082 if (valueToValidate instanceof Collection< ? >) 083 { 084 final int size = ((Collection< ? >) valueToValidate).size(); 085 return size >= min && size <= max; 086 } 087 if (valueToValidate instanceof Map< ? , ? >) 088 { 089 final int size = ((Map< ? , ? >) valueToValidate).size(); 090 return size >= min && size <= max; 091 } 092 if (valueToValidate.getClass().isArray()) 093 { 094 final int size = Array.getLength(valueToValidate); 095 return size >= min && size <= max; 096 } 097 final String str = valueToValidate.toString(); 098 final int size = str.length(); 099 return size >= min && size <= max; 100 } 101 102 /** 103 * @param max the max to set 104 */ 105 public void setMax(final int max) 106 { 107 this.max = max; 108 requireMessageVariablesRecreation(); 109 } 110 111 /** 112 * @param min the min to set 113 */ 114 public void setMin(final int min) 115 { 116 this.min = min; 117 requireMessageVariablesRecreation(); 118 } 119 }