001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io.build; 019 020import java.io.File; 021import java.io.InputStream; 022import java.io.OutputStream; 023import java.io.RandomAccessFile; 024import java.io.Reader; 025import java.io.Writer; 026import java.net.URI; 027import java.nio.file.Path; 028import java.nio.file.Paths; 029 030import org.apache.commons.io.IORandomAccessFile; 031import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin; 032import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin; 033import org.apache.commons.io.build.AbstractOrigin.FileOrigin; 034import org.apache.commons.io.build.AbstractOrigin.IORandomAccessFileOrigin; 035import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin; 036import org.apache.commons.io.build.AbstractOrigin.OutputStreamOrigin; 037import org.apache.commons.io.build.AbstractOrigin.PathOrigin; 038import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin; 039import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin; 040import org.apache.commons.io.build.AbstractOrigin.URIOrigin; 041import org.apache.commons.io.build.AbstractOrigin.WriterOrigin; 042 043/** 044 * Abstracts building an instance of {@code T}. 045 * 046 * @param <T> the type of instances to build. 047 * @param <B> the type of builder subclass. 048 * @since 2.12.0 049 */ 050public abstract class AbstractOriginSupplier<T, B extends AbstractOriginSupplier<T, B>> extends AbstractSupplier<T, B> { 051 052 /** 053 * Constructs a new byte array origin for a byte array. 054 * 055 * @param origin the byte array. 056 * @return a new byte array origin. 057 */ 058 protected static ByteArrayOrigin newByteArrayOrigin(final byte[] origin) { 059 return new ByteArrayOrigin(origin); 060 } 061 062 /** 063 * Constructs a new CharSequence origin for a CharSequence. 064 * 065 * @param origin the CharSequence. 066 * @return a new file origin. 067 * @since 2.13.0 068 */ 069 protected static CharSequenceOrigin newCharSequenceOrigin(final CharSequence origin) { 070 return new CharSequenceOrigin(origin); 071 } 072 073 /** 074 * Constructs a new file origin for a file. 075 * 076 * @param origin the file. 077 * @return a new file origin. 078 */ 079 protected static FileOrigin newFileOrigin(final File origin) { 080 return new FileOrigin(origin); 081 } 082 083 /** 084 * Constructs a new file origin for a file path. 085 * 086 * @param origin the file path. 087 * @return a new file origin. 088 */ 089 protected static FileOrigin newFileOrigin(final String origin) { 090 return new FileOrigin(new File(origin)); 091 } 092 093 /** 094 * Constructs a new input stream origin for a file. 095 * 096 * @param origin the input stream. 097 * @return a new input stream origin. 098 */ 099 protected static InputStreamOrigin newInputStreamOrigin(final InputStream origin) { 100 return new InputStreamOrigin(origin); 101 } 102 103 /** 104 * Constructs a new output stream origin for a file. 105 * 106 * @param origin the output stream. 107 * @return a new output stream origin. 108 */ 109 protected static OutputStreamOrigin newOutputStreamOrigin(final OutputStream origin) { 110 return new OutputStreamOrigin(origin); 111 } 112 113 /** 114 * Constructs a new path origin for a file. 115 * 116 * @param origin the path. 117 * @return a new path origin. 118 */ 119 protected static PathOrigin newPathOrigin(final Path origin) { 120 return new PathOrigin(origin); 121 } 122 123 /** 124 * Constructs a new path name origin for a path name. 125 * 126 * @param origin the path name. 127 * @return a new path name origin. 128 */ 129 protected static PathOrigin newPathOrigin(final String origin) { 130 return new PathOrigin(Paths.get(origin)); 131 } 132 133 /** 134 * Constructs a new RandomAccessFile origin for a RandomAccessFile. 135 * 136 * @param origin the reader. 137 * @return a new reader origin. 138 * @since 2.18.0 139 */ 140 protected static IORandomAccessFileOrigin newRandomAccessFileOrigin(final IORandomAccessFile origin) { 141 return new IORandomAccessFileOrigin(origin); 142 } 143 144 /** 145 * Constructs a new RandomAccessFile origin for a RandomAccessFile. 146 * 147 * @param origin the reader. 148 * @return a new reader origin. 149 * @since 2.18.0 150 */ 151 protected static RandomAccessFileOrigin newRandomAccessFileOrigin(final RandomAccessFile origin) { 152 return new RandomAccessFileOrigin(origin); 153 } 154 155 /** 156 * Constructs a new reader origin for a reader. 157 * 158 * @param origin the reader. 159 * @return a new reader origin. 160 */ 161 protected static ReaderOrigin newReaderOrigin(final Reader origin) { 162 return new ReaderOrigin(origin); 163 } 164 165 /** 166 * Constructs a new reader origin for a URI. 167 * 168 * @param origin the URI. 169 * @return a new URI origin. 170 */ 171 protected static URIOrigin newURIOrigin(final URI origin) { 172 return new URIOrigin(origin); 173 } 174 175 /** 176 * Constructs a new writer origin for a file. 177 * 178 * @param origin the writer. 179 * @return a new writer . 180 */ 181 protected static WriterOrigin newWriterOrigin(final Writer origin) { 182 return new WriterOrigin(origin); 183 } 184 185 /** 186 * The underlying origin. 187 */ 188 private AbstractOrigin<?, ?> origin; 189 190 /** 191 * Checks whether the origin is null. 192 * 193 * @return the origin. 194 * @throws IllegalStateException if the {@code origin} is {@code null}. 195 */ 196 protected AbstractOrigin<?, ?> checkOrigin() { 197 if (origin == null) { 198 throw new IllegalStateException("origin == null"); 199 } 200 return origin; 201 } 202 203 /** 204 * Gets the origin. 205 * 206 * @return the origin. 207 */ 208 protected AbstractOrigin<?, ?> getOrigin() { 209 return origin; 210 } 211 212 /** 213 * Tests whether the origin is null. 214 * 215 * @return whether the origin is null. 216 */ 217 protected boolean hasOrigin() { 218 return origin != null; 219 } 220 221 /** 222 * Sets a new origin. 223 * 224 * @param origin the new origin. 225 * @return {@code this} instance. 226 */ 227 public B setByteArray(final byte[] origin) { 228 return setOrigin(newByteArrayOrigin(origin)); 229 } 230 231 /** 232 * Sets a new origin. 233 * 234 * @param origin the new origin. 235 * @return {@code this} instance. 236 * @since 2.13.0 237 */ 238 public B setCharSequence(final CharSequence origin) { 239 return setOrigin(newCharSequenceOrigin(origin)); 240 } 241 242 /** 243 * Sets a new origin. 244 * 245 * @param origin the new origin. 246 * @return {@code this} instance. 247 */ 248 public B setFile(final File origin) { 249 return setOrigin(newFileOrigin(origin)); 250 } 251 252 /** 253 * Sets a new origin. 254 * 255 * @param origin the new origin. 256 * @return {@code this} instance. 257 */ 258 public B setFile(final String origin) { 259 return setOrigin(newFileOrigin(origin)); 260 } 261 262 /** 263 * Sets a new origin. 264 * 265 * @param origin the new origin. 266 * @return {@code this} instance. 267 */ 268 public B setInputStream(final InputStream origin) { 269 return setOrigin(newInputStreamOrigin(origin)); 270 } 271 272 /** 273 * Sets a new origin. 274 * 275 * @param origin the new origin. 276 * @return {@code this} instance. 277 */ 278 protected B setOrigin(final AbstractOrigin<?, ?> origin) { 279 this.origin = origin; 280 return asThis(); 281 } 282 283 /** 284 * Sets a new origin. 285 * 286 * @param origin the new origin. 287 * @return {@code this} instance. 288 */ 289 public B setOutputStream(final OutputStream origin) { 290 return setOrigin(newOutputStreamOrigin(origin)); 291 } 292 293 /** 294 * Sets a new origin. 295 * 296 * @param origin the new origin. 297 * @return {@code this} instance. 298 */ 299 public B setPath(final Path origin) { 300 return setOrigin(newPathOrigin(origin)); 301 } 302 303 /** 304 * Sets a new origin. 305 * 306 * @param origin the new origin. 307 * @return {@code this} instance. 308 */ 309 public B setPath(final String origin) { 310 return setOrigin(newPathOrigin(origin)); 311 } 312 313 /** 314 * Sets a new origin. 315 * 316 * @param origin the new origin. 317 * @return {@code this} instance. 318 * @since 2.18.0 319 */ 320 public B setRandomAccessFile(final IORandomAccessFile origin) { 321 return setOrigin(newRandomAccessFileOrigin(origin)); 322 } 323 324 /** 325 * Sets a new origin. 326 * 327 * @param origin the new origin. 328 * @return {@code this} instance. 329 * @since 2.18.0 330 */ 331 public B setRandomAccessFile(final RandomAccessFile origin) { 332 return setOrigin(newRandomAccessFileOrigin(origin)); 333 } 334 335 /** 336 * Sets a new origin. 337 * 338 * @param origin the new origin. 339 * @return {@code this} instance. 340 */ 341 public B setReader(final Reader origin) { 342 return setOrigin(newReaderOrigin(origin)); 343 } 344 345 /** 346 * Sets a new origin. 347 * 348 * @param origin the new origin. 349 * @return {@code this} instance. 350 */ 351 public B setURI(final URI origin) { 352 return setOrigin(newURIOrigin(origin)); 353 } 354 355 /** 356 * Sets a new origin. 357 * 358 * @param origin the new origin. 359 * @return {@code this} instance. 360 */ 361 public B setWriter(final Writer origin) { 362 return setOrigin(newWriterOrigin(origin)); 363 } 364}