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 * Constructs a new instance for subclasses. 192 */ 193 public AbstractOriginSupplier() { 194 // empty 195 } 196 197 /** 198 * Checks whether the origin is null. 199 * 200 * @return the origin. 201 * @throws IllegalStateException if the {@code origin} is {@code null}. 202 */ 203 protected AbstractOrigin<?, ?> checkOrigin() { 204 if (origin == null) { 205 throw new IllegalStateException("origin == null"); 206 } 207 return origin; 208 } 209 210 /** 211 * Gets the origin. 212 * 213 * @return the origin. 214 */ 215 protected AbstractOrigin<?, ?> getOrigin() { 216 return origin; 217 } 218 219 /** 220 * Tests whether the origin is null. 221 * 222 * @return whether the origin is null. 223 */ 224 protected boolean hasOrigin() { 225 return origin != null; 226 } 227 228 /** 229 * Sets a new origin. 230 * 231 * @param origin the new origin. 232 * @return {@code this} instance. 233 */ 234 public B setByteArray(final byte[] origin) { 235 return setOrigin(newByteArrayOrigin(origin)); 236 } 237 238 /** 239 * Sets a new origin. 240 * 241 * @param origin the new origin. 242 * @return {@code this} instance. 243 * @since 2.13.0 244 */ 245 public B setCharSequence(final CharSequence origin) { 246 return setOrigin(newCharSequenceOrigin(origin)); 247 } 248 249 /** 250 * Sets a new origin. 251 * 252 * @param origin the new origin. 253 * @return {@code this} instance. 254 */ 255 public B setFile(final File origin) { 256 return setOrigin(newFileOrigin(origin)); 257 } 258 259 /** 260 * Sets a new origin. 261 * 262 * @param origin the new origin. 263 * @return {@code this} instance. 264 */ 265 public B setFile(final String origin) { 266 return setOrigin(newFileOrigin(origin)); 267 } 268 269 /** 270 * Sets a new origin. 271 * 272 * @param origin the new origin. 273 * @return {@code this} instance. 274 */ 275 public B setInputStream(final InputStream origin) { 276 return setOrigin(newInputStreamOrigin(origin)); 277 } 278 279 /** 280 * Sets a new origin. 281 * 282 * @param origin the new origin. 283 * @return {@code this} instance. 284 */ 285 protected B setOrigin(final AbstractOrigin<?, ?> origin) { 286 this.origin = origin; 287 return asThis(); 288 } 289 290 /** 291 * Sets a new origin. 292 * 293 * @param origin the new origin. 294 * @return {@code this} instance. 295 */ 296 public B setOutputStream(final OutputStream origin) { 297 return setOrigin(newOutputStreamOrigin(origin)); 298 } 299 300 /** 301 * Sets a new origin. 302 * 303 * @param origin the new origin. 304 * @return {@code this} instance. 305 */ 306 public B setPath(final Path origin) { 307 return setOrigin(newPathOrigin(origin)); 308 } 309 310 /** 311 * Sets a new origin. 312 * 313 * @param origin the new origin. 314 * @return {@code this} instance. 315 */ 316 public B setPath(final String origin) { 317 return setOrigin(newPathOrigin(origin)); 318 } 319 320 /** 321 * Sets a new origin. 322 * 323 * @param origin the new origin. 324 * @return {@code this} instance. 325 * @since 2.18.0 326 */ 327 public B setRandomAccessFile(final IORandomAccessFile origin) { 328 return setOrigin(newRandomAccessFileOrigin(origin)); 329 } 330 331 /** 332 * Sets a new origin. 333 * 334 * @param origin the new origin. 335 * @return {@code this} instance. 336 * @since 2.18.0 337 */ 338 public B setRandomAccessFile(final RandomAccessFile origin) { 339 return setOrigin(newRandomAccessFileOrigin(origin)); 340 } 341 342 /** 343 * Sets a new origin. 344 * 345 * @param origin the new origin. 346 * @return {@code this} instance. 347 */ 348 public B setReader(final Reader origin) { 349 return setOrigin(newReaderOrigin(origin)); 350 } 351 352 /** 353 * Sets a new origin. 354 * 355 * @param origin the new origin. 356 * @return {@code this} instance. 357 */ 358 public B setURI(final URI origin) { 359 return setOrigin(newURIOrigin(origin)); 360 } 361 362 /** 363 * Sets a new origin. 364 * 365 * @param origin the new origin. 366 * @return {@code this} instance. 367 */ 368 public B setWriter(final Writer origin) { 369 return setOrigin(newWriterOrigin(origin)); 370 } 371}