/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.model;
import javolution37.javolution.xml.XmlElement;
import javolution37.javolution.xml.XmlFormat;
/**
* A string wrapper that conforms to the JSCData interface.
*
* Whenever the value of this data object changes, registered
* JSCDataListeners are notified via a JSCDataEvent.
* If not otherwise stated, the event objects always contain the values before
* the change and after the change.
*
* This class is thread-safe. All access to mutable data is synchronized. All
* events are fired outside the synchronized blocks.
*
* @author Markus Kraetzig
*/
public final class JSCString extends AbstractJSCData {
private final String name;
private String value = null;
private JSCDataEvent clearEvent = null;
private JSCDataEvent notEmptyEvent = null;
/**
* XML format field for (de)serialization.
*/
public static final XmlFormat JSCString_XML = new XmlFormat(
JSCString.class) {
public void format(JSCString data, XmlElement xml) {
xml.setAttribute("name", data.name);
synchronized (data) {
xml.setAttribute("value", data.value + "");
}
}
public JSCString parse(XmlElement xml) {
String nam = xml.getAttribute("name") + "";
String valueString = xml.getAttribute("value") + "";
String newString = null;
if (!valueString.equalsIgnoreCase("null"))
newString = valueString;
return new JSCString(nam, newString);
}
};
/**
* Creates an empty JSCString.
*
* @param name
* the identifier of that object
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCString(String name) {
this(name, null);
}
/**
* Creates a JSCString from value.
*
* @param name
* the identifier of that object
* @param value
* the String to be used as value
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCString(String name, String value) {
super();
if (name == null)
throw new IllegalArgumentException("Argument was null.");
name = JSCConstants.checkNameThrowEx(name);
this.name = name;
this.value = value;
}
/**
* Clears this data object. Fires:
*
* JSCDataEventTypes.EMPTY_STATE event
* JSCDataEventTypes.VALUE_CHANGED event
*
* if it was not empty before. After calling this method
* isEmpty returns true.
*/
public void clear() {
String oldValue;
synchronized (this) {
if (value == null) // Nothing changes.
return;
oldValue = value;
value = null;
}
// Dispatch event outside synchronized block.
if (clearEvent == null)
clearEvent = JSCDataEvent.valueOfEmptyState(this, true);
if (eventSupport == null)
return;
getEventSupport().dispatchEvent(clearEvent);
getEventSupport().dispatchEvent(
JSCDataEvent.valueOfChanged(this, oldValue, null));
}
/**
* @see com.jstatcom.model.JSCData
*/
public synchronized JSCString copy() {
return new JSCString(name, value);
}
/**
* Gets whether this is empty. This method returns
* true, if value returns null.
* It does not check whether the value string has a length > 0.
*
* @return true if empty
*/
public synchronized boolean isEmpty() {
return value == null;
}
/**
* @see com.jstatcom.model.JSCData
*/
public synchronized boolean isEqual(JSCData o) {
if (o == this)
return true;
if (!(o instanceof JSCString))
return false;
JSCString otherString = (JSCString) o;
boolean ret = true;
synchronized (this) {
synchronized (o) {
if (value != null)
ret = value.equals(otherString.string());
else
ret = otherString.string() == null;
}
}
return ret;
}
/**
* @see com.jstatcom.model.JSCData
*/
public String name() {
return name;
}
/**
* Sets the value of this to newValue. Fires a
* JSCDataEvent of type:
*
* JSCDataEventTypes.VALUE_CHANGED if
* !newValue.equals(getString())
* JSCDataEventTypes.EMPTY_STATE if
* newValue != null and this was empty before
* JSCDataEventTypes.EMPTY_STATE if
* newValue == null and this was not empty before
*
*
* @param newValue
* the new value
*/
public void setVal(String newValue) {
if (newValue == null) {
clear();
return;
}
boolean wasEmpty = false;
String oldValue = null;
synchronized (this) {
// Nothing changed.
if (newValue.equals(value))
return;
wasEmpty = isEmpty();
// No copy needed, because Double is immutable.
oldValue = value;
value = newValue;
}
// Dispatch event outside synchronized block.
if (eventSupport == null)
return;
getEventSupport().dispatchEvent(
JSCDataEvent.valueOfChanged(this, oldValue, newValue));
if (wasEmpty) {
if (notEmptyEvent == null)
notEmptyEvent = JSCDataEvent.valueOfEmptyState(this, false);
getEventSupport().dispatchEvent(notEmptyEvent);
}
}
/**
* Gets the stored string value.
*
* @return a String or null if empty
*/
public synchronized String string() {
return value;
}
/**
* Gets the type of this JSCData object.
*
* @return JSCTypes.STRING
*/
public JSCTypes type() {
return JSCTypes.STRING;
}
/**
* Gets the underlying value, which is stored as a String.
*
* @return the value object or null if this is
* empty
*/
public synchronized Object value() {
return value;
}
}
Ads help maintain this website.