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

A
Andy Clement 已提交
17 18 19 20
package org.springframework.expression.spel.ast;

import java.util.ArrayList;
import java.util.Collection;
21
import java.util.HashMap;
A
Andy Clement 已提交
22 23 24
import java.util.List;
import java.util.Map;

25
import org.springframework.core.convert.TypeDescriptor;
A
Andy Clement 已提交
26
import org.springframework.expression.EvaluationException;
27
import org.springframework.expression.TypedValue;
28
import org.springframework.expression.spel.ExpressionState;
29
import org.springframework.expression.spel.SpelEvaluationException;
30
import org.springframework.expression.spel.SpelMessage;
A
Andy Clement 已提交
31 32 33 34 35 36 37 38 39 40

/**
 * Represents selection over a map or collection. For example: {1,2,3,4,5,6,7,8,9,10}.?{#isEven(#this) == 'y'} returns
 * [2, 4, 6, 8, 10]
 * 
 * Basically a subset of the input data is returned based on the evaluation of the expression supplied as selection
 * criteria.
 * 
 * @author Andy Clement
 */
41
public class Selection extends SpelNodeImpl {
A
Andy Clement 已提交
42

43 44 45
	public final static int ALL = 0; // ?[]
	public final static int FIRST = 1; // ^[]
	public final static int LAST = 2; // $[]
A
Andy Clement 已提交
46 47

	private final int variant;
48
	private final boolean nullSafe;
A
Andy Clement 已提交
49

50 51 52
	public Selection(boolean nullSafe, int variant,int pos,SpelNodeImpl expression) {
		super(pos,expression); 
		this.nullSafe = nullSafe;
A
Andy Clement 已提交
53 54 55
		this.variant = variant;
	}

56
	@SuppressWarnings("unchecked")
A
Andy Clement 已提交
57
	@Override
58 59 60 61
	public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
		TypedValue op = state.getActiveContextObject();
		Object operand = op.getValue();
		
62
		SpelNodeImpl selectionCriteria = children[0];
A
Andy Clement 已提交
63 64
		if (operand instanceof Map) {
			Map<?, ?> mapdata = (Map<?, ?>) operand;
65 66
			// TODO don't lose generic info for the new map
			Map<Object,Object> result = new HashMap<Object,Object>();
67
			Object lastKey = null;
68
			for (Map.Entry entry : mapdata.entrySet()) {
A
Andy Clement 已提交
69
				try {
70
					lastKey = entry.getKey();
71
					TypedValue kvpair = new TypedValue(entry,TypeDescriptor.valueOf(Map.Entry.class));
A
Andy Clement 已提交
72
					state.pushActiveContextObject(kvpair);
73
					Object o = selectionCriteria.getValueInternal(state).getValue();
A
Andy Clement 已提交
74 75
					if (o instanceof Boolean) {
						if (((Boolean) o).booleanValue() == true) {
76
							if (variant == FIRST) {
77
								result.put(entry.getKey(),entry.getValue());
78 79
								return new TypedValue(result);
							}
80
							result.put(entry.getKey(),entry.getValue());
A
Andy Clement 已提交
81 82
						}
					} else {
83
						throw new SpelEvaluationException(selectionCriteria.getStartPosition(),
84
								SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);// ,selectionCriteria.stringifyAST());
A
Andy Clement 已提交
85 86 87 88
					}
				} finally {
					state.popActiveContextObject();
				}
89 90
			}
			if ((variant == FIRST || variant == LAST) && result.size() == 0) {
91
				return new TypedValue(null,TypeDescriptor.NULL);
92 93 94
			}
			if (variant == LAST) {
				Map resultMap = new HashMap();
95 96
				Object lastValue = result.get(lastKey);
				resultMap.put(lastKey,lastValue);
97
				return new TypedValue(resultMap,TypeDescriptor.valueOf(Map.class));
A
Andy Clement 已提交
98
			}
99
			return new TypedValue(result,op.getTypeDescriptor());
A
Andy Clement 已提交
100 101 102 103 104 105 106
		} else if (operand instanceof Collection) {
			List<Object> data = new ArrayList<Object>();
			data.addAll((Collection<?>) operand);
			List<Object> result = new ArrayList<Object>();
			int idx = 0;
			for (Object element : data) {
				try {
107
					state.pushActiveContextObject(new TypedValue(element,TypeDescriptor.valueOf(op.getTypeDescriptor().getElementType())));
A
Andy Clement 已提交
108
					state.enterScope("index", idx);
109
					Object o = selectionCriteria.getValueInternal(state).getValue();
A
Andy Clement 已提交
110 111
					if (o instanceof Boolean) {
						if (((Boolean) o).booleanValue() == true) {
112
							if (variant == FIRST) {
113
								return new TypedValue(element,TypeDescriptor.valueOf(op.getTypeDescriptor().getElementType()));
114
							}
A
Andy Clement 已提交
115 116 117
							result.add(element);
						}
					} else {
118
						throw new SpelEvaluationException(selectionCriteria.getStartPosition(),
119
								SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);// ,selectionCriteria.stringifyAST());
A
Andy Clement 已提交
120 121 122 123 124 125 126 127
					}
					idx++;
				} finally {
					state.exitScope();
					state.popActiveContextObject();
				}
			}
			if ((variant == FIRST || variant == LAST) && result.size() == 0) {
128
				return TypedValue.NULL_TYPED_VALUE;
A
Andy Clement 已提交
129 130
			}
			if (variant == LAST) {
131
				return new TypedValue(result.get(result.size() - 1),TypeDescriptor.valueOf(op.getTypeDescriptor().getElementType()));
A
Andy Clement 已提交
132
			}
133
			return new TypedValue(result,op.getTypeDescriptor());
A
Andy Clement 已提交
134
		} else {
A
Andy Clement 已提交
135 136 137 138
			if (operand==null) {
				if (nullSafe) { 
					return TypedValue.NULL_TYPED_VALUE;
				} else {
139
					throw new SpelEvaluationException(getStartPosition(), SpelMessage.INVALID_TYPE_FOR_SELECTION,
A
Andy Clement 已提交
140 141
							"null");
				}
142
			} else {
143
				throw new SpelEvaluationException(getStartPosition(), SpelMessage.INVALID_TYPE_FOR_SELECTION,
A
Andy Clement 已提交
144 145
						operand.getClass().getName());
			}				
A
Andy Clement 已提交
146 147 148 149 150
		}
	}

	@Override
	public String toStringAST() {
J
Juergen Hoeller 已提交
151
		StringBuilder sb = new StringBuilder();
A
Andy Clement 已提交
152 153
		switch (variant) {
		case ALL:
154
			sb.append("?[");
A
Andy Clement 已提交
155 156
			break;
		case FIRST:
157
			sb.append("^[");
A
Andy Clement 已提交
158 159
			break;
		case LAST:
160
			sb.append("$[");
A
Andy Clement 已提交
161 162
			break;
		}
163
		return sb.append(getChild(0).toStringAST()).append("]").toString();
A
Andy Clement 已提交
164 165 166
	}

}