1 | /* |
2 | * Copyright 2002-2013 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
5 | * the License. You may obtain a copy of the License at |
6 | * |
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
11 | * specific language governing permissions and limitations under the License. |
12 | */ |
13 | package org.springframework.batch.item; |
14 | |
15 | import org.springframework.core.convert.converter.Converter; |
16 | import org.springframework.expression.Expression; |
17 | import org.springframework.expression.ExpressionParser; |
18 | import org.springframework.expression.spel.standard.SpelExpressionParser; |
19 | |
20 | /** |
21 | * An implementation of {@link Converter} that uses SpEL to map a Value to a key |
22 | * @author David Turanski |
23 | * @since 2.2 |
24 | */ |
25 | public class SpELItemKeyMapper<K,V> implements Converter<V,K> { |
26 | private final ExpressionParser parser = new SpelExpressionParser(); |
27 | private final Expression parsedExpression; |
28 | |
29 | public SpELItemKeyMapper(String keyExpression) { |
30 | parsedExpression = parser.parseExpression(keyExpression); |
31 | } |
32 | /* (non-Javadoc) |
33 | * @see org.springframework.batch.item.ItemKeyMapper#mapKey(java.lang.Object) |
34 | */ |
35 | @SuppressWarnings("unchecked") |
36 | @Override |
37 | public K convert(V item) { |
38 | return (K) parsedExpression.getValue(item); |
39 | } |
40 | } |