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.data.builder;
018
019import org.springframework.batch.item.data.GemfireItemWriter;
020import org.springframework.core.convert.converter.Converter;
021import org.springframework.data.gemfire.GemfireTemplate;
022import org.springframework.util.Assert;
023
024/**
025 * A builder implementation for the {@link GemfireItemWriter}
026 *
027 * @author Glenn Renfro
028 * @since 4.0
029 * @see GemfireItemWriterBuilder
030 */
031public class GemfireItemWriterBuilder<K, V> {
032
033        private GemfireTemplate template;
034
035        private Converter<V, K> itemKeyMapper;
036
037        private boolean delete;
038
039        /**
040         * Establishes the GemfireTemplate the writer should use.
041         * @param template the {@link GemfireTemplate} to set.
042         * @return The current instance of the builder.
043         * @see GemfireItemWriter#setTemplate(GemfireTemplate)
044         */
045        public GemfireItemWriterBuilder<K, V> template(GemfireTemplate template) {
046                this.template = template;
047
048                return this;
049        }
050
051        /**
052         * Set the {@link Converter} to use to derive the key from the item.
053         *
054         * @param itemKeyMapper the Converter to use.
055         * @return The current instance of the builder.
056         * @see GemfireItemWriter#setItemKeyMapper(Converter)
057         */
058        public GemfireItemWriterBuilder<K, V> itemKeyMapper(Converter<V, K> itemKeyMapper) {
059                this.itemKeyMapper = itemKeyMapper;
060
061                return this;
062        }
063
064        /**
065         * Indicates if the items being passed to the writer are to be saved or removed from
066         * the data store. If set to false (default), the items will be saved. If set to true,
067         * the items will be removed.
068         *
069         * @param delete removal indicator.
070         * @return The current instance of the builder.
071         * @see GemfireItemWriter#setDelete(boolean)
072         */
073        public GemfireItemWriterBuilder<K, V> delete(boolean delete) {
074                this.delete = delete;
075
076                return this;
077        }
078
079
080        /**
081         * Validates and builds a {@link GemfireItemWriter}.
082         *
083         * @return a {@link GemfireItemWriter}
084         */
085        public GemfireItemWriter<K, V> build() {
086                Assert.notNull(this.template, "template is required.");
087                Assert.notNull(this.itemKeyMapper, "itemKeyMapper is required.");
088
089                GemfireItemWriter<K, V> writer = new GemfireItemWriter<>();
090                writer.setTemplate(this.template);
091                writer.setItemKeyMapper(this.itemKeyMapper);
092                writer.setDelete(this.delete);
093                return writer;
094        }
095}