001 package org.apache.commons.contract.constraints; 002 003 import java.util.Locale; 004 005 import org.apache.commons.contract.Context; 006 import org.apache.commons.i18n.bundles.ErrorBundle; 007 import org.apache.commons.i18n.bundles.TextBundle; 008 009 public class LocaleConstraints implements Constraints { 010 public static final LocaleConstraints UNCONSTRAINED = new LocaleConstraints(); 011 012 public void validate(Object value, Context context) throws ValidationException { 013 } 014 015 public Object cast(Object value, Context context) throws CastException { 016 if ( value instanceof Locale) { 017 return (Locale)value; 018 } 019 try { 020 return new Locale(StringConstraints.UNCONSTRAINED.cast(value, null).toString()); 021 } catch ( CastException exception ) { 022 throw new CastException(new ErrorBundle("uncastableLocale", new Object[] { value })); 023 } 024 } 025 026 public TextBundle verboseConstraints() { 027 return new TextBundle("unconstrainedLocale"); 028 } 029 }