View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.binary;
19  
20  import java.util.function.Supplier;
21  
22  /**
23   * Builds input and output stream instances in {@link BaseNCodec} format.
24   *
25   * @param <T> the stream type to build.
26   * @param <C> A {@link BaseNCodec} subclass.
27   * @param <B> the builder subclass.
28   * @since 1.20.0
29   */
30  public abstract class AbstractBaseNCodecStreamBuilder<T, C extends BaseNCodec, B extends AbstractBaseNCodecStreamBuilder<T, C, B>> implements Supplier<T> {
31  
32      private C baseNCodec;
33      private boolean encode;
34  
35      /**
36       * Constructs a new instance.
37       */
38      public AbstractBaseNCodecStreamBuilder() {
39          baseNCodec = newBaseNCodec();
40      }
41  
42      @SuppressWarnings("unchecked")
43      B asThis() {
44          return (B) this;
45      }
46  
47      /**
48       * Gets the codec to encode/decode a stream.
49       *
50       * @return the codec to encode/decode a stream.
51       */
52      protected C getBaseNCodec() {
53          return baseNCodec;
54      }
55  
56      /**
57       * Gets whether to encode or decode a stream.
58       *
59       * @return whether to encode or decode a stream.
60       */
61      protected boolean getEncode() {
62          return encode;
63      }
64  
65      /**
66       * Creates a new BaseNCodec subclass of type C.
67       *
68       * @return a new BaseNCodec subclass of type C.
69       */
70      protected abstract C newBaseNCodec();
71  
72      /**
73       * Sets a BaseNCodec subclass of type C.
74       *
75       * @param baseNCodec a BaseNCodec subclass of type C.
76       * @return {@code this} instance.
77       */
78      public B setBaseNCodec(final C baseNCodec) {
79          this.baseNCodec = baseNCodec != null ? baseNCodec : newBaseNCodec();
80          return asThis();
81      }
82  
83      /**
84       * Sets whether we should encode all data read (true), or if false if we should decode.
85       *
86       * @param encode encode or decode.
87       * @return {@code this} instance.
88       */
89      public B setEncode(final boolean encode) {
90          this.encode = encode;
91          return asThis();
92      }
93  }