1 16 package org.springframework.binding.expression.support; 17 18 import java.util.Collection ; 19 20 import org.springframework.binding.expression.EvaluationContext; 21 import org.springframework.binding.expression.EvaluationException; 22 import org.springframework.binding.expression.Expression; 23 import org.springframework.binding.expression.SetValueAttempt; 24 import org.springframework.binding.expression.SettableExpression; 25 import org.springframework.core.style.ToStringCreator; 26 import org.springframework.util.Assert; 27 28 32 public class CollectionAddingExpression implements SettableExpression { 33 34 37 private Expression collectionExpression; 38 39 43 public CollectionAddingExpression(Expression collectionExpression) { 44 this.collectionExpression = collectionExpression; 45 } 46 47 public Object evaluate(Object target, EvaluationContext context) throws EvaluationException { 48 return collectionExpression.evaluate(target, context); 49 } 50 51 public void evaluateToSet(Object target, Object value, EvaluationContext context) throws EvaluationException { 52 Object result = evaluate(target, context); 53 if (result == null) { 54 throw new EvaluationException(new SetValueAttempt(this, target, value, null), 55 new IllegalArgumentException ("The collection expression evaluated to a [null] reference")); 56 } 57 Assert.isInstanceOf(Collection .class, result, "Not a collection: "); 58 if (value != null) { 59 ((Collection )result).add(value); 61 } 62 } 63 64 public String toString() { 65 return new ToStringCreator(this).append("collectionExpression", collectionExpression).toString(); 66 } 67 } | Popular Tags |