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 Strings. Provides a few convenience helper methods. 021 */ 022public class StringVar extends Var<String> { 023 024 /** 025 * Initializes a new StringVar with a null initial value. 026 */ 027 public StringVar() { 028 } 029 030 /** 031 * Initializes a new StringVar with the given initial value. 032 * 033 * @param value the initial value 034 */ 035 public StringVar(String value) { 036 super(value); 037 } 038 039 /** 040 * Returns true if the wrapped string is either null or empty. 041 * 042 * @return true if the wrapped string is either null or empty 043 */ 044 public boolean isEmpty() { 045 return get() == null || get().length() == 0; 046 } 047 048 /** 049 * Appends the given string. 050 * If this instance is currently uninitialized the given string is used for initialization. 051 * 052 * @param text the text to append 053 * @return true 054 */ 055 public boolean append(String text) { 056 return set(get() == null ? text : get().concat(text)); 057 } 058 059 /** 060 * Appends the given string. 061 * If this instance is currently uninitialized the given string is used for initialization. 062 * 063 * @param text the text to append 064 * @return this instance 065 */ 066 public StringVar appended(String text) { 067 append(text); 068 return this; 069 } 070 071 /** 072 * Appends the given char. 073 * If this instance is currently uninitialized the given char is used for initialization. 074 * 075 * @param c the char to append 076 * @return true 077 */ 078 public boolean append(char c) { 079 return set(get() == null ? String.valueOf(c) : get() + c); 080 } 081 082 /** 083 * Appends the given char. 084 * If this instance is currently uninitialized the given string is used for initialization. 085 * 086 * @param c the char to append 087 * @return this instance 088 */ 089 public StringVar appended(char c) { 090 append(c); 091 return this; 092 } 093} 094