001/* 002 * Copyright 2017 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.item.support.builder; 018 019import java.util.List; 020 021import org.springframework.batch.item.ItemWriter; 022import org.springframework.batch.item.support.CompositeItemWriter; 023import org.springframework.util.Assert; 024 025/** 026 * Creates a fully qualified CompositeItemWriter. 027 * 028 * @author Glenn Renfro 029 * 030 * @since 4.0 031 */ 032public class CompositeItemWriterBuilder<T> { 033 private List<ItemWriter<? super T>> delegates; 034 035 private boolean ignoreItemStream = false; 036 037 /** 038 * Establishes the policy whether to call the open, close, or update methods for the 039 * item writer delegates associated with the CompositeItemWriter. 040 * 041 * @param ignoreItemStream if false the delegates' open, close, or update methods will 042 * be called when the corresponding methods on the CompositeItemWriter are called. If 043 * true the delegates' open, close, nor update methods will not be called (default is false). 044 * @return this instance for method chaining. 045 * 046 * @see CompositeItemWriter#setIgnoreItemStream(boolean) 047 */ 048 public CompositeItemWriterBuilder<T> ignoreItemStream(boolean ignoreItemStream) { 049 this.ignoreItemStream = ignoreItemStream; 050 051 return this; 052 } 053 054 /** 055 * The list of item writers to use as delegates. Items are written to each of the 056 * delegates. 057 * 058 * @param delegates the list of delegates to use. The delegates list must not be null 059 * nor be empty. 060 * @return this instance for method chaining. 061 * 062 * @see CompositeItemWriter#setDelegates(List) 063 */ 064 public CompositeItemWriterBuilder<T> delegates(List<ItemWriter<? super T>> delegates) { 065 this.delegates = delegates; 066 067 return this; 068 } 069 070 /** 071 * Returns a fully constructed {@link CompositeItemWriter}. 072 * 073 * @return a new {@link CompositeItemWriter} 074 */ 075 public CompositeItemWriter<T> build() { 076 Assert.notNull(delegates, "A list of delegates is required."); 077 Assert.notEmpty(delegates, "The delegates list must have one or more delegates."); 078 079 CompositeItemWriter<T> writer = new CompositeItemWriter<>(); 080 writer.setDelegates(this.delegates); 081 writer.setIgnoreItemStream(this.ignoreItemStream); 082 return writer; 083 } 084}