001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.parboiled.support;
018
019/**
020 * Simple specialization of a {@link Var} for StringBuilders.
021 * Provides a few convenience helper methods.
022 */
023public class StringBuilderVar extends Var<StringBuilder> {
024
025    /**
026     * Initializes a new StringVar with a null initial value.
027     */
028    public StringBuilderVar() {
029    }
030
031    /**
032     * Initializes a new StringBuilderVar with the given initial StringBuilder instance.
033     *
034     * @param value the initial value
035     */
036    public StringBuilderVar(StringBuilder value) {
037        super(value);
038    }
039
040    /**
041     * Returns true if the wrapped string is either null or empty.
042     *
043     * @return true if the wrapped string is either null or empty
044     */
045    public boolean isEmpty() {
046        return get() == null || get().length() == 0;
047    }
048
049    /**
050     * @return the String representation of the underlying StringBuilder.
051     */
052    public String getString() {
053        return get() == null ? "" : get().toString();
054    }
055    
056    /**
057     * @return the char[] representation of the underlying StringBuilder.
058     */
059    public char[] getChars() {
060        if (get() == null) return new char[0];
061        StringBuilder sb = get();
062        char[] buf = new char[sb.length()];
063        sb.getChars(0, buf.length, buf, 0);
064        return buf;
065    }
066
067    /**
068     * Appends the given string.
069     * If this instance is currently uninitialized the given string is used for initialization.
070     *
071     * @param text the text to append
072     * @return true
073     */
074    public boolean append(String text) {
075        if (get() == null) return set(new StringBuilder(text));
076        get().append(text);
077        return true;
078    }
079
080    /**
081     * Appends the given string.
082     * If this instance is currently uninitialized the given string is used for initialization.
083     *
084     * @param text the text to append
085     * @return this instance
086     */
087    public StringBuilderVar appended(String text) {
088        append(text);
089        return this;
090    }
091    
092    /**
093     * Appends the given char.
094     * If this instance is currently uninitialized the given char is used for initialization.
095     *
096     * @param c the char to append
097     * @return true
098     */
099    public boolean append(char c) {
100        if (get() == null) return set(new StringBuilder().append(c));
101        get().append(c);
102        return true;
103    }
104
105    /**
106     * Appends the given char.
107     * If this instance is currently uninitialized the given char is used for initialization.
108     *
109     * @param c the char to append
110     * @return this instance
111     */
112    public StringBuilderVar appended(char c) {
113        append(c);
114        return this;
115    }
116    
117    /**
118     * Clears the contents of the wrapped StringBuilder.
119     * If the instance is currently unintialized this method does nothing. 
120     * @return true
121     */
122    public boolean clearContents() {
123        if (get() != null) get().setLength(0);
124        return true;
125    }
126    
127    /**
128     * Clears the contents of the wrapped StringBuilder.
129     * If the instance is currently unintialized this method does nothing. 
130     * @return this instance
131     */
132    public StringBuilderVar contentsCleared() {
133        if (get() != null) get().setLength(0);
134        return this;
135    }
136}
137