View Javadoc
1   /*
2    * Copyright 2006 Ralf Joachim
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.javasource;
15  
16  /**
17   * Represents a line of code, used by JSourceCode class.
18   *
19   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
20   * @version $Revision: 6668 $ $Date: 2005-05-08 12:32:06 -0600 (Sun, 08 May 2005) $
21   * @since 1.1
22   */
23  public final class JCodeStatement {
24    // --------------------------------------------------------------------------
25  
26    /** Contents of this code statement. */
27    private final StringBuffer _value;
28  
29    /** Indentation depth of this statement. */
30    private short _indentSize = JSourceCode.DEFAULT_INDENT_SIZE;
31  
32    // --------------------------------------------------------------------------
33  
34    JCodeStatement() {
35      super();
36  
37      _value = new StringBuffer();
38    }
39  
40    JCodeStatement(final String statement) {
41      this();
42  
43      _value.append(statement);
44    }
45  
46    JCodeStatement(final String statement, final short indentSize) {
47      this(statement);
48  
49      _indentSize = indentSize;
50    }
51  
52    // --------------------------------------------------------------------------
53  
54    void append(final String segment) {
55      _value.append(segment);
56    }
57  
58    short getIndent() {
59      return _indentSize;
60    }
61  
62    String getStatement() {
63      return _value.toString();
64    }
65  
66    // --------------------------------------------------------------------------
67  
68    /**
69     * {@inheritDoc}
70     */
71    public String toString() {
72      if (_value.length() == 0) {
73        return "";
74      }
75      StringBuilder sb = new StringBuilder(_indentSize + _value.length());
76      for (int i = 0; i < _indentSize; i++) {
77        sb.append(' ');
78      }
79      sb.append(_value);
80      return sb.toString();
81    }
82  
83    // --------------------------------------------------------------------------
84  }