View Javadoc

    1   package sharin.sql.formatter;
    2
    3   import java.util.ArrayList;
    4   import java.util.List;
    5
    6   class BasicResultBuffer {
    7
    8       private final List<StringBuilder> builderList = new ArrayList<StringBuilder>();
    9
    10      private final StringBuilder result = new StringBuilder();
    11
    12      public BasicResultBuffer() {
    13          builderList.add(null);
    14      }
    15
    16      public void append(String s) {
    17          StringBuilder builder = null;
    18          int size = builderList.size();
    19
    20          if (size >= 1) {
    21              builder = builderList.get(size - 1);
    22          }
    23
    24          if (builder == null) {
    25              builder = result;
    26          }
    27
    28          append(builder, s);
    29      }
    30
    31      private void append(StringBuilder builder, String s) {
    32
    33          if (s == null || s.length() == 0) {
    34              return;
    35          }
    36
    37          if (builder.length() > 0) {
    38              builder.append(' ');
    39          }
    40
    41          builder.append(s);
    42      }
    43
    44      public void flush() {
    45
    46          for (int i = 0; i < builderList.size(); i++) {
    47              StringBuilder builder = builderList.get(i);
    48
    49              if (builder != null) {
    50                  append(result, builder.toString());
    51                  builderList.set(i, null);
    52              }
    53          }
    54      }
    55
    56      public void begin() {
    57          builderList.add(new StringBuilder());
    58      }
    59
    60      public void prepend(String s) {
    61          int size = builderList.size();
    62
    63          if (size >= 2 && builderList.get(size - 2) == null) {
    64              append(s);
    65          }
    66      }
    67
    68      public void end() {
    69          int size = builderList.size();
    70
    71          if (size >= 1) {
    72              builderList.remove(size - 1);
    73          }
    74      }
    75
    76      public String getResult() {
    77          return result.toString();
    78      }
    79  }