KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > expression > support > CollectionAddingExpression


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.expression.support;
17
18 import java.util.Collection JavaDoc;
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 /**
29  * A settable expression that adds non-null values to a collection.
30  * @author Keith Donald
31  */

32 public class CollectionAddingExpression implements SettableExpression {
33
34     /**
35      * The expression that resolves a mutable collection reference.
36      */

37     private Expression collectionExpression;
38
39     /**
40      * Creates a collection adding property expression.
41      * @param collectionExpression the collection expression
42      */

43     public CollectionAddingExpression(Expression collectionExpression) {
44         this.collectionExpression = collectionExpression;
45     }
46
47     public Object JavaDoc evaluate(Object JavaDoc target, EvaluationContext context) throws EvaluationException {
48         return collectionExpression.evaluate(target, context);
49     }
50
51     public void evaluateToSet(Object JavaDoc target, Object JavaDoc value, EvaluationContext context) throws EvaluationException {
52         Object JavaDoc result = evaluate(target, context);
53         if (result == null) {
54             throw new EvaluationException(new SetValueAttempt(this, target, value, null),
55                     new IllegalArgumentException JavaDoc("The collection expression evaluated to a [null] reference"));
56         }
57         Assert.isInstanceOf(Collection JavaDoc.class, result, "Not a collection: ");
58         if (value != null) {
59             // add the value to the collection
60
((Collection JavaDoc)result).add(value);
61         }
62     }
63
64     public String JavaDoc toString() {
65         return new ToStringCreator(this).append("collectionExpression", collectionExpression).toString();
66     }
67 }
Popular Tags