001/* 002 * Copyright 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.sample.domain.trade.internal; 018 019import org.springframework.batch.item.ItemProcessor; 020import org.springframework.batch.item.validator.ValidationException; 021import org.springframework.batch.sample.domain.trade.Trade; 022 023/** 024 * Processes the Trade - throwing validation errors if necessary. 025 */ 026public class TradeProcessor implements ItemProcessor<Trade, Trade> { 027 028 private int failure = -1; 029 030 private int index = 0; 031 032 private Trade failedItem = null; 033 034 /** 035 * Public setter for the the index on which failure should occur. 036 * 037 * @param failure the failure to set 038 */ 039 public void setValidationFailure(int failure) { 040 this.failure = failure; 041 } 042 043 @Override 044 public Trade process(Trade item) throws Exception { 045 if ((failedItem == null && index++ == failure) || (failedItem != null && failedItem.equals(item))) { 046 failedItem = item; 047 throw new ValidationException("Some bad data for " + failedItem); 048 } 049 return item; 050 } 051}