/* Copyright 2004-2006 David N. Welton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.hecl;
/**
* The StringThing class is the internal representation of string
* types. This is somewhat special, as all types in Hecl may be represented as
* strings.
*
* @author David N. Welton
* @version 1.0
*/
public class StringThing implements RealThing {
private StringBuffer val;
/**
* Creates a new, empty StringThing instance.
*
*/
public StringThing() {
this((String)null);
}
/**
* Creates a new StringThing instance from a string.
*
* @param s
* a String value
*/
public StringThing(String s) {
val = s != null ? new StringBuffer(s) : new StringBuffer();
}
/**
* Creates a new StringThing instance from a stringbuffer.
*
* @param sb
* a StringBuffer value
*/
public StringThing(StringBuffer sb) {
val = sb;
}
@Override
public String thingclass() {
return "string";
}
/**
* The create method creates and returns a newly allocated
* Thing with a StringThing internal representation.
*
* @param s A String value which may be null.
* @return A Thing value
*/
public static Thing create(String s) {
return new Thing(new StringThing(s != null ? s : ""));
}
/**
* The setStringFromAny method transforms the Thing into a
* string type.
*
* @param thing
* a Thing value
* @throws HeclException
*/
private static void setStringFromAny(Thing thing) {
RealThing realthing = thing.getVal();
if (!(realthing instanceof StringThing)) {
thing.setVal(new StringThing(thing.toString()));
}
}
/**
* get returns a string representation of a given Thing,
* transforming the thing into a string type at the same time.
*
* @param thing
* a Thing value
* @return a String value
*/
public static String get(Thing thing) {
setStringFromAny(thing);
return thing.toString();
}
/**
* deepcopy copies the string.
*
* @return a RealThing value
*/
@Override
public RealThing deepcopy() {
return new StringThing(new StringBuffer(val.toString()));
}
/**
* getStringRep returns its internal value.
*
* @return a String value
*/
@Override
public String getStringRep() {
return val.toString();
}
/**
* append takes a character and appends it to the string.
*
* @param ch
* a char value
*/
public void append(char ch) {
val.append(ch);
}
/**
* append appends a string to the string.
*
* @param str
* a String value
*/
public void append(String str) {
val.append(str);
}
}
Ads help maintain this website.