001 package org.apache.commons.contract.constraints; 002 003 import java.util.ArrayList; 004 import java.util.Iterator; 005 import java.util.List; 006 import java.util.Locale; 007 008 import org.apache.commons.contract.Context; 009 import org.apache.commons.i18n.bundles.ErrorBundle; 010 import org.apache.commons.i18n.bundles.TextBundle; 011 012 public class ListConstraints implements Constraints { 013 protected Constraints entryValueDescriptor; 014 015 public ListConstraints() { 016 entryValueDescriptor = Unconstrained.UNCONSTRAINED; 017 } 018 019 public ListConstraints(Constraints entryValueDescriptor) { 020 this.entryValueDescriptor = entryValueDescriptor; 021 } 022 023 public void setEntryValueDescriptor(Constraints entryValueDescriptor) { 024 this.entryValueDescriptor = entryValueDescriptor; 025 } 026 027 public Constraints getEntryValueDescriptor() { 028 return entryValueDescriptor; 029 } 030 031 public Object cast(Object value, Context context) throws CastException { 032 List list = new ArrayList(); 033 if ( value instanceof String[] ) { 034 for ( int i = 0; i < ((String[])value).length; i++ ) { 035 list.add(entryValueDescriptor.cast(((String [])value)[i], context)); 036 } 037 } else if ( value instanceof List ) { 038 for ( Iterator i = ((List)value).iterator(); i.hasNext(); ) { 039 Object entry = i.next(); 040 if ( entry instanceof Evaluatable ) { 041 try { 042 entry = ((Evaluatable)entry).evaluate(context); 043 } catch (Exception e) { 044 throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e); 045 } 046 } 047 list.add(entryValueDescriptor.cast(entry, context)); 048 } 049 } else if ( value instanceof Object[] ) { 050 for ( int i = 0; i < ((Object [])value).length; i++ ) { 051 Object entry = ((Object [])value)[i]; 052 if ( entry instanceof Evaluatable ) { 053 try { 054 entry = ((Evaluatable)entry).evaluate(context); 055 } catch (Exception e) { 056 throw new CastException(new ErrorBundle("evaluatingAnyFailed"), e); 057 } 058 } 059 list.add(entryValueDescriptor.cast(entry, context)); 060 } 061 } else { 062 throw new CastException(new ErrorBundle("uncastableList", new Object[] { value })); 063 } 064 return list; 065 } 066 067 public void validate(Object value, Context context) throws ValidationException { 068 try { 069 List list = (List)value; 070 for ( Iterator i = list.iterator(); i.hasNext();) { 071 Object entry = i.next(); 072 if ( entry != null ) { 073 entryValueDescriptor.validate(entry, context); 074 } 075 } 076 } catch ( ValidationException exception ) { 077 throw new ValidationException(new ErrorBundle("invalidListEntry", new Object[] { value }), exception); 078 } 079 } 080 081 public TextBundle verboseConstraints() { 082 if ( entryValueDescriptor == Unconstrained.UNCONSTRAINED ) { 083 return new TextBundle("unconstrainedList"); 084 } else { 085 TextBundle verbosedEntryContrstaints = entryValueDescriptor.verboseConstraints(); 086 return new TextBundle("constrainedList", new String[] { verbosedEntryContrstaints.getText(Locale.getDefault()) }); 087 } 088 } 089 }