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  package org.apache.commons.cli.help;
18  
19  import java.io.IOException;
20  
21  /**
22   * An abstract implementation of {@link HelpAppendable} that writes output to an {@link Appendable} instance.
23   * <p>
24   * This class is the superclass of all classes that filter help appendables. These appendable sit on top of an existing appendable (the <em>underlying</em>
25   * appendable) which it uses as its basic sink of data, but possibly transforming the data along the way or providing additional functionality.
26   * </p>
27   * <p>
28   * The class {@code FilterHelpAppendable} itself simply overrides all methods of {@code HelpAppendable} with versions that pass all requests to the underlying
29   * appendable. Subclasses of {@code FilterHelpAppendable} may further override some of these methods as well as provide additional methods and fields.
30   * </p>
31   * <p>
32   * <em>Implementation Note</em>: This class is similar to FilterOutputStream in relation to OutputStream. We could further split FilterHelpAppendable into a
33   * FilterAppendable but that seems like YAGNI.
34   * </p>
35   *
36   * @since 1.10.0
37   */
38  public abstract class FilterHelpAppendable implements HelpAppendable {
39  
40      /**
41       * The underlying appendable to be filtered.
42       */
43      protected final Appendable output;
44  
45      /**
46       * Constructs an appendable filter built on top of the specified underlying appendable.
47       *
48       * @param output the underlying appendable to be assigned to the field {@code this.output} for later use, or {@code null} if this instance is to be created
49       *               without an underlying stream.
50       */
51      protected FilterHelpAppendable(final Appendable output) {
52          this.output = output;
53      }
54  
55      @Override
56      public FilterHelpAppendable append(final char ch) throws IOException {
57          output.append(ch);
58          return this;
59      }
60  
61      @Override
62      public FilterHelpAppendable append(final CharSequence text) throws IOException {
63          output.append(text);
64          return this;
65      }
66  
67      @Override
68      public FilterHelpAppendable append(final CharSequence csq, final int start, final int end) throws IOException {
69          output.append(csq, start, end);
70          return this;
71      }
72  }