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.Reader;
024import java.io.Writer;
025import java.net.URI;
026import java.nio.file.Path;
027import java.nio.file.Paths;
028
029import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
030import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin;
031import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
032import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin;
033import org.apache.commons.io.build.AbstractOrigin.OutputStreamOrigin;
034import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
035import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin;
036import org.apache.commons.io.build.AbstractOrigin.URIOrigin;
037import org.apache.commons.io.build.AbstractOrigin.WriterOrigin;
038
039/**
040 * Abstracts building an instance of {@code T}.
041 *
042 * @param <T> the type of instances to build.
043 * @param <B> the type of builder subclass.
044 * @since 2.12.0
045 */
046public abstract class AbstractOriginSupplier<T, B extends AbstractOriginSupplier<T, B>> extends AbstractSupplier<T, B> {
047
048    /**
049     * Constructs a new byte array origin for a byte array.
050     *
051     * @param origin the byte array.
052     * @return a new byte array origin.
053     */
054    protected static ByteArrayOrigin newByteArrayOrigin(final byte[] origin) {
055        return new ByteArrayOrigin(origin);
056    }
057
058    /**
059     * Constructs a new CharSequence origin for a CharSequence.
060     *
061     * @param origin the CharSequence.
062     * @return a new file origin.
063     * @since 2.13.0
064     */
065    protected static CharSequenceOrigin newCharSequenceOrigin(final CharSequence origin) {
066        return new CharSequenceOrigin(origin);
067    }
068
069    /**
070     * Constructs a new file origin for a file.
071     *
072     * @param origin the file.
073     * @return a new file origin.
074     */
075    protected static FileOrigin newFileOrigin(final File origin) {
076        return new FileOrigin(origin);
077    }
078
079    /**
080     * Constructs a new file origin for a file path.
081     *
082     * @param origin the file path.
083     * @return a new file origin.
084     */
085    protected static FileOrigin newFileOrigin(final String origin) {
086        return new FileOrigin(new File(origin));
087    }
088
089    /**
090     * Constructs a new input stream origin for a file.
091     *
092     * @param origin the input stream.
093     * @return a new input stream origin.
094     */
095    protected static InputStreamOrigin newInputStreamOrigin(final InputStream origin) {
096        return new InputStreamOrigin(origin);
097    }
098
099    /**
100     * Constructs a new output stream origin for a file.
101     *
102     * @param origin the output stream.
103     * @return a new output stream origin.
104     */
105    protected static OutputStreamOrigin newOutputStreamOrigin(final OutputStream origin) {
106        return new OutputStreamOrigin(origin);
107    }
108
109    /**
110     * Constructs a new path origin for a file.
111     *
112     * @param origin the path.
113     * @return a new path origin.
114     */
115    protected static PathOrigin newPathOrigin(final Path origin) {
116        return new PathOrigin(origin);
117    }
118
119    /**
120     * Constructs a new path name origin for a path name.
121     *
122     * @param origin the path name.
123     * @return a new path name origin.
124     */
125    protected static PathOrigin newPathOrigin(final String origin) {
126        return new PathOrigin(Paths.get(origin));
127    }
128
129    /**
130     * Constructs a new reader origin for a reader.
131     *
132     * @param origin the reader.
133     * @return a new reader origin.
134     */
135    protected static ReaderOrigin newReaderOrigin(final Reader origin) {
136        return new ReaderOrigin(origin);
137    }
138
139    /**
140     * Constructs a new reader origin for a URI.
141     *
142     * @param origin the URI.
143     * @return a new URI origin.
144     */
145    protected static URIOrigin newURIOrigin(final URI origin) {
146        return new URIOrigin(origin);
147    }
148
149    /**
150     * Constructs a new writer origin for a file.
151     *
152     * @param origin the writer.
153     * @return a new writer .
154     */
155    protected static WriterOrigin newWriterOrigin(final Writer origin) {
156        return new WriterOrigin(origin);
157    }
158
159    /**
160     * The underlying origin.
161     */
162    private AbstractOrigin<?, ?> origin;
163
164    /**
165     * Checks whether the origin is null.
166     *
167     * @return the origin.
168     * @throws IllegalStateException if the {@code origin} is {@code null}.
169     */
170    protected AbstractOrigin<?, ?> checkOrigin() {
171        if (origin == null) {
172            throw new IllegalStateException("origin == null");
173        }
174        return origin;
175    }
176
177    /**
178     * Gets the origin.
179     *
180     * @return the origin.
181     */
182    protected AbstractOrigin<?, ?> getOrigin() {
183        return origin;
184    }
185
186    /**
187     * Tests whether the origin is null.
188     *
189     * @return whether the origin is null.
190     */
191    protected boolean hasOrigin() {
192        return origin != null;
193    }
194
195    /**
196     * Sets a new origin.
197     *
198     * @param origin the new origin.
199     * @return {@code this} instance.
200     */
201    public B setByteArray(final byte[] origin) {
202        return setOrigin(newByteArrayOrigin(origin));
203    }
204
205    /**
206     * Sets a new origin.
207     *
208     * @param origin the new origin.
209     * @return {@code this} instance.
210     * @since 2.13.0
211     */
212    public B setCharSequence(final CharSequence origin) {
213        return setOrigin(newCharSequenceOrigin(origin));
214    }
215
216    /**
217     * Sets a new origin.
218     *
219     * @param origin the new origin.
220     * @return {@code this} instance.
221     */
222    public B setFile(final File origin) {
223        return setOrigin(newFileOrigin(origin));
224    }
225
226    /**
227     * Sets a new origin.
228     *
229     * @param origin the new origin.
230     * @return {@code this} instance.
231     */
232    public B setFile(final String origin) {
233        return setOrigin(newFileOrigin(origin));
234    }
235
236    /**
237     * Sets a new origin.
238     *
239     * @param origin the new origin.
240     * @return {@code this} instance.
241     */
242    public B setInputStream(final InputStream origin) {
243        return setOrigin(newInputStreamOrigin(origin));
244    }
245
246    /**
247     * Sets a new origin.
248     *
249     * @param origin the new origin.
250     * @return {@code this} instance.
251     */
252    protected B setOrigin(final AbstractOrigin<?, ?> origin) {
253        this.origin = origin;
254        return asThis();
255    }
256
257    /**
258     * Sets a new origin.
259     *
260     * @param origin the new origin.
261     * @return {@code this} instance.
262     */
263    public B setOutputStream(final OutputStream origin) {
264        return setOrigin(newOutputStreamOrigin(origin));
265    }
266
267    /**
268     * Sets a new origin.
269     *
270     * @param origin the new origin.
271     * @return {@code this} instance.
272     */
273    public B setPath(final Path origin) {
274        return setOrigin(newPathOrigin(origin));
275    }
276
277    /**
278     * Sets a new origin.
279     *
280     * @param origin the new origin.
281     * @return {@code this} instance.
282     */
283    public B setPath(final String origin) {
284        return setOrigin(newPathOrigin(origin));
285    }
286
287    /**
288     * Sets a new origin.
289     *
290     * @param origin the new origin.
291     * @return {@code this} instance.
292     */
293    public B setReader(final Reader origin) {
294        return setOrigin(newReaderOrigin(origin));
295    }
296
297    /**
298     * Sets a new origin.
299     *
300     * @param origin the new origin.
301     * @return {@code this} instance.
302     */
303    public B setURI(final URI origin) {
304        return setOrigin(newURIOrigin(origin));
305    }
306
307    /**
308     * Sets a new origin.
309     *
310     * @param origin the new origin.
311     * @return {@code this} instance.
312     */
313    public B setWriter(final Writer origin) {
314        return setOrigin(newWriterOrigin(origin));
315    }
316}