2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 231   Methods: 17
NCLOC: 107   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BodyBuilder.java 100% 100% 100% 100%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // 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
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.hivemind.service;
 16   
 17    import java.text.MessageFormat;
 18   
 19    /**
 20    * Utility class for assembling the <em>body</em> used with Javassist as a method or catch block.
 21    *
 22    * @author Howard Lewis Ship
 23    */
 24   
 25    public class BodyBuilder
 26    {
 27    /**
 28    * Feels right for the size of a typical body.
 29    */
 30    private static final int DEFAULT_LENGTH = 200;
 31   
 32    private static final char QUOTE = '"';
 33   
 34    private StringBuffer _buffer = new StringBuffer(DEFAULT_LENGTH);
 35   
 36    private static final String INDENT = " ";
 37   
 38    private int _nestingDepth = 0;
 39   
 40    private boolean _atNewLine = true;
 41   
 42    /**
 43    * Clears the builder, returning it to its initial, empty state.
 44    */
 45  4448 public void clear()
 46    {
 47  4448 _nestingDepth = 0;
 48  4448 _atNewLine = true;
 49  4448 _buffer.setLength(0);
 50    }
 51   
 52    /**
 53    * Adds text to the current line, without terminating the line.
 54    */
 55  28685 public void add(String text)
 56    {
 57  28685 indent();
 58   
 59  28685 _buffer.append(text);
 60    }
 61   
 62    /**
 63    * Adds text to the current line, without terminating the line.
 64    *
 65    * @param pattern
 66    * a string pattern, used with
 67    * {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
 68    * @param arguments
 69    * arguments used witht the format string
 70    */
 71   
 72  1031 public void add(String pattern, Object[] arguments)
 73    {
 74  1031 add(MessageFormat.format(pattern, arguments));
 75    }
 76   
 77    /**
 78    * Convience for {@link #add(String, Object[])}
 79    */
 80   
 81  1 public void add(String pattern, Object arg0)
 82    {
 83  1 add(pattern, new Object[]
 84    { arg0 });
 85    }
 86   
 87    /**
 88    * Convience for {@link #add(String, Object[])}
 89    */
 90   
 91  1029 public void add(String pattern, Object arg0, Object arg1)
 92    {
 93  1029 add(pattern, new Object[]
 94    { arg0, arg1 });
 95    }
 96   
 97    /**
 98    * Convience for {@link #add(String, Object[])}
 99    */
 100   
 101  1 public void add(String pattern, Object arg0, Object arg1, Object arg2)
 102    {
 103  1 add(pattern, new Object[]
 104    { arg0, arg1, arg2 });
 105    }
 106   
 107    /**
 108    * Adds text to the current line then terminates the line.
 109    *
 110    * @param pattern
 111    * a string pattern, used with
 112    * {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
 113    * @param arguments
 114    * arguments used witht the format string
 115    */
 116   
 117  3 public void addln(String pattern, Object[] arguments)
 118    {
 119  3 addln(MessageFormat.format(pattern, arguments));
 120    }
 121   
 122    /**
 123    * Convience for {@link #addln(String, Object[])}
 124    */
 125   
 126  1 public void addln(String pattern, Object arg0)
 127    {
 128  1 addln(pattern, new Object[]
 129    { arg0 });
 130    }
 131   
 132    /**
 133    * Convience for {@link #addln(String, Object[])}.
 134    */
 135   
 136  1 public void addln(String pattern, Object arg0, Object arg1)
 137    {
 138  1 addln(pattern, new Object[]
 139    { arg0, arg1 });
 140    }
 141   
 142    /**
 143    * Convience for {@link #addln(String, Object[])}.
 144    */
 145   
 146  1 public void addln(String pattern, Object arg0, Object arg1, Object arg2)
 147    {
 148  1 addln(pattern, new Object[]
 149    { arg0, arg1, arg2 });
 150    }
 151   
 152    /**
 153    * Adds the text to the current line, surrounded by double quotes.
 154    * <em>Does not escape quotes in the text</em>.
 155    */
 156   
 157  55 public void addQuoted(String text)
 158    {
 159  55 indent();
 160  55 _buffer.append(QUOTE);
 161  55 _buffer.append(text);
 162  55 _buffer.append(QUOTE);
 163    }
 164   
 165    /**
 166    * Adds the text to the current line, and terminates the line.
 167    */
 168   
 169  11618 public void addln(String text)
 170    {
 171  11618 add(text);
 172   
 173  11618 newline();
 174    }
 175   
 176  32698 private void newline()
 177    {
 178  32698 _buffer.append("\n");
 179  32698 _atNewLine = true;
 180    }
 181   
 182    /**
 183    * Begins a new block. Emits a "{", properly indented, on a new line.
 184    */
 185  8516 public void begin()
 186    {
 187  8516 if (!_atNewLine)
 188  996 newline();
 189   
 190  8516 indent();
 191  8516 _buffer.append("{");
 192  8516 newline();
 193   
 194  8516 _nestingDepth++;
 195    }
 196   
 197    /**
 198    * Ends the current block. Emits a "}", propertly indented, on a new line.
 199    */
 200  8516 public void end()
 201    {
 202  8516 if (!_atNewLine)
 203  3052 newline();
 204   
 205  8516 _nestingDepth--;
 206   
 207  8516 indent();
 208  8516 _buffer.append("}");
 209   
 210  8516 newline();
 211    }
 212   
 213  45772 private void indent()
 214    {
 215  45772 if (_atNewLine)
 216    {
 217  33732 for (int i = 0; i < _nestingDepth; i++)
 218  23626 _buffer.append(INDENT);
 219   
 220  33732 _atNewLine = false;
 221    }
 222    }
 223   
 224    /**
 225    * Returns the current contents of the buffer.
 226    */
 227  7562 public String toString()
 228    {
 229  7562 return _buffer.toString();
 230    }
 231    }