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.file.builder;
018
019import org.springframework.batch.item.file.MultiResourceItemWriter;
020import org.springframework.batch.item.file.ResourceAwareItemWriterItemStream;
021import org.springframework.batch.item.file.ResourceSuffixCreator;
022import org.springframework.core.io.Resource;
023import org.springframework.util.Assert;
024
025/**
026 * A builder implementation for the {@link MultiResourceItemWriter}.
027 *
028 * @author Glenn Renfro
029 * @author Glenn Renfro
030 * @since 4.0
031 * @see MultiResourceItemWriter
032 */
033public class MultiResourceItemWriterBuilder<T> {
034
035        private Resource resource;
036
037        private ResourceAwareItemWriterItemStream<? super T> delegate;
038
039        private int itemCountLimitPerResource = Integer.MAX_VALUE;
040
041        private ResourceSuffixCreator suffixCreator;
042
043        private boolean saveState = true;
044
045        private String name;
046
047        /**
048         * Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport}
049         * should be persisted within the {@link org.springframework.batch.item.ExecutionContext}
050         * for restart purposes.
051         *
052         * @param saveState defaults to true
053         * @return The current instance of the builder.
054         */
055        public MultiResourceItemWriterBuilder<T> saveState(boolean saveState) {
056                this.saveState = saveState;
057
058                return this;
059        }
060
061        /**
062         * The name used to calculate the key within the
063         * {@link org.springframework.batch.item.ExecutionContext}. Required if
064         * {@link #saveState(boolean)} is set to true.
065         *
066         * @param name name of the reader instance
067         * @return The current instance of the builder.
068         * @see org.springframework.batch.item.ItemStreamSupport#setName(String)
069         */
070        public MultiResourceItemWriterBuilder<T> name(String name) {
071                this.name = name;
072
073                return this;
074        }
075
076        /**
077         * Allows customization of the suffix of the created resources based on the index.
078         *
079         * @param suffixCreator the customizable ResourceSuffixCreator to use.
080         * @return The current instance of the builder.
081         * @see MultiResourceItemWriter#setResourceSuffixCreator(ResourceSuffixCreator)
082         */
083        public MultiResourceItemWriterBuilder<T> resourceSuffixCreator(ResourceSuffixCreator suffixCreator) {
084                this.suffixCreator = suffixCreator;
085
086                return this;
087        }
088
089        /**
090         * After this limit is exceeded the next chunk will be written into newly created
091         * resource.
092         *
093         * @param itemCountLimitPerResource the max numbers of items to be written per chunk.
094         * @return The current instance of the builder.
095         * @see MultiResourceItemWriter#setItemCountLimitPerResource(int)
096         */
097        public MultiResourceItemWriterBuilder<T> itemCountLimitPerResource(int itemCountLimitPerResource) {
098                this.itemCountLimitPerResource = itemCountLimitPerResource;
099
100                return this;
101        }
102
103        /**
104         * Delegate used for actual writing of the output.
105         * @param delegate The delegate to use for writing.
106         * @return The current instance of the builder.
107         * @see MultiResourceItemWriter#setDelegate(ResourceAwareItemWriterItemStream)
108         */
109        public MultiResourceItemWriterBuilder<T> delegate(ResourceAwareItemWriterItemStream<? super T> delegate) {
110                this.delegate = delegate;
111
112                return this;
113        }
114
115        /**
116         * Prototype for output resources. Actual output files will be created in the same
117         * directory and use the same name as this prototype with appended suffix (according
118         * to {@link MultiResourceItemWriter#setResourceSuffixCreator(ResourceSuffixCreator)}.
119         *
120         * @param resource the prototype resource to use as the basis for creating resources.
121         * @return The current instance of the builder.
122         * @see MultiResourceItemWriter#setResource(Resource)
123         */
124        public MultiResourceItemWriterBuilder<T> resource(Resource resource) {
125                this.resource = resource;
126
127                return this;
128        }
129
130        /**
131         * Builds the {@link MultiResourceItemWriter}.
132         *
133         * @return a {@link MultiResourceItemWriter}
134         */
135        public MultiResourceItemWriter<T> build() {
136                Assert.notNull(this.resource, "resource is required.");
137                Assert.notNull(this.delegate, "delegate is required.");
138
139                if(this.saveState) {
140                        org.springframework.util.Assert.hasText(this.name, "A name is required when saveState is true.");
141                }
142
143                MultiResourceItemWriter<T> writer = new MultiResourceItemWriter<>();
144                writer.setResource(this.resource);
145                writer.setDelegate(this.delegate);
146                writer.setItemCountLimitPerResource(this.itemCountLimitPerResource);
147                if(this.suffixCreator != null) {
148                        writer.setResourceSuffixCreator(this.suffixCreator);
149                }
150                writer.setSaveState(this.saveState);
151                writer.setName(this.name);
152
153                return writer;
154        }
155
156}