001/* 002 * Copyright 2018 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.json.builder; 018 019import org.springframework.batch.item.json.JsonItemReader; 020import org.springframework.batch.item.json.JsonObjectReader; 021import org.springframework.core.io.Resource; 022import org.springframework.util.Assert; 023import org.springframework.util.StringUtils; 024 025/** 026 * A builder for {@link JsonItemReader}. 027 * 028 * @param <T> type of the target item 029 * 030 * @author Mahmoud Ben Hassine 031 * @since 4.1 032 */ 033public class JsonItemReaderBuilder<T> { 034 035 private JsonObjectReader<T> jsonObjectReader; 036 037 private Resource resource; 038 039 private String name; 040 041 private boolean strict = true; 042 043 private boolean saveState = true; 044 045 private int maxItemCount = Integer.MAX_VALUE; 046 047 private int currentItemCount; 048 049 /** 050 * Set the {@link JsonObjectReader} to use to read and map Json objects to domain objects. 051 * @param jsonObjectReader to use 052 * @return The current instance of the builder. 053 * @see JsonItemReader#setJsonObjectReader(JsonObjectReader) 054 */ 055 public JsonItemReaderBuilder<T> jsonObjectReader(JsonObjectReader<T> jsonObjectReader) { 056 this.jsonObjectReader = jsonObjectReader; 057 058 return this; 059 } 060 061 /** 062 * The {@link Resource} to be used as input. 063 * @param resource the input to the reader. 064 * @return The current instance of the builder. 065 * @see JsonItemReader#setResource(Resource) 066 */ 067 public JsonItemReaderBuilder<T> resource(Resource resource) { 068 this.resource = resource; 069 070 return this; 071 } 072 073 /** 074 * The name used to calculate the key within the 075 * {@link org.springframework.batch.item.ExecutionContext}. Required if 076 * {@link #saveState(boolean)} is set to true. 077 * @param name name of the reader instance 078 * @return The current instance of the builder. 079 * @see org.springframework.batch.item.ItemStreamSupport#setName(String) 080 */ 081 public JsonItemReaderBuilder<T> name(String name) { 082 this.name = name; 083 084 return this; 085 } 086 087 /** 088 * Setting this value to true indicates that it is an error if the input 089 * does not exist and an exception will be thrown. Defaults to true. 090 * @param strict indicates the input resource must exist 091 * @return The current instance of the builder. 092 * @see JsonItemReader#setStrict(boolean) 093 */ 094 public JsonItemReaderBuilder<T> strict(boolean strict) { 095 this.strict = strict; 096 097 return this; 098 } 099 100 /** 101 * Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} 102 * should be persisted within the {@link org.springframework.batch.item.ExecutionContext} 103 * for restart purposes. 104 * @param saveState defaults to true 105 * @return The current instance of the builder. 106 */ 107 public JsonItemReaderBuilder<T> saveState(boolean saveState) { 108 this.saveState = saveState; 109 110 return this; 111 } 112 113 /** 114 * Configure the max number of items to be read. 115 * @param maxItemCount the max items to be read 116 * @return The current instance of the builder. 117 * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int) 118 */ 119 public JsonItemReaderBuilder<T> maxItemCount(int maxItemCount) { 120 this.maxItemCount = maxItemCount; 121 122 return this; 123 } 124 125 /** 126 * Index for the current item. Used on restarts to indicate where to start from. 127 * @param currentItemCount current index 128 * @return The current instance of the builder. 129 * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setCurrentItemCount(int) 130 */ 131 public JsonItemReaderBuilder<T> currentItemCount(int currentItemCount) { 132 this.currentItemCount = currentItemCount; 133 134 return this; 135 } 136 137 /** 138 * Validate the configuration and build a new {@link JsonItemReader}. 139 * @return a new instance of the {@link JsonItemReader} 140 */ 141 public JsonItemReader<T> build() { 142 Assert.notNull(this.jsonObjectReader, "A json object reader is required."); 143 Assert.notNull(this.resource, "A resource is required."); 144 if (this.saveState) { 145 Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); 146 } 147 148 JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader); 149 reader.setName(this.name); 150 reader.setStrict(this.strict); 151 reader.setSaveState(this.saveState); 152 reader.setMaxItemCount(this.maxItemCount); 153 reader.setCurrentItemCount(this.currentItemCount); 154 155 return reader; 156 } 157}