001/* 002 * Copyright 2002-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * https://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 */ 013package org.springframework.batch.item; 014 015import org.springframework.core.convert.converter.Converter; 016import org.springframework.expression.Expression; 017import org.springframework.expression.ExpressionParser; 018import org.springframework.expression.spel.standard.SpelExpressionParser; 019 020/** 021 * An implementation of {@link Converter} that uses SpEL to map a Value to a key 022 * @author David Turanski 023 * @since 2.2 024 */ 025public class SpELItemKeyMapper<K,V> implements Converter<V,K> { 026 private final ExpressionParser parser = new SpelExpressionParser(); 027 private final Expression parsedExpression; 028 029 public SpELItemKeyMapper(String keyExpression) { 030 parsedExpression = parser.parseExpression(keyExpression); 031 } 032 /* (non-Javadoc) 033 * @see org.springframework.batch.item.ItemKeyMapper#mapKey(java.lang.Object) 034 */ 035 @SuppressWarnings("unchecked") 036 @Override 037 public K convert(V item) { 038 return (K) parsedExpression.getValue(item); 039 } 040}