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