/*
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;
/**
* An object wrapper that conforms to the JSCData interface.
* Arbitrary objects can be stored. This class is meant to store special purpose
* objects that are not easily generalized. It can be used to easily pass
* references to those objects via the symbol table.
*
* Because nothing is known about the underlying value except that it is an
* Object, no special purpose methods are provided.
*
* Example:
* An example usage would be to store an object that represents the state of a
* certain model, say ModelState. This state object could be
* immutable and this wrapper notifies listeners whenever a new
* ModelState has been set. JSCVoid makes it also
* possible to assign a Symbol for that state object and to store
* it in a SymbolTable.
*
* Usage Note:
* Instances can only be (de)serialized, if the stored value is itself XML
* serializable, see javolution37.javolution.xml.XmlFormat.
*
* Comparisons between the stored values rely on the equals
* method of Object. If a logical check for equality is
* preferred to a pure reference check, then the class of the value object
* should overwrite equal (and hashCode to keep
* the contract).
*
* Whenever the value of this data object changes (by setting a new object),
* 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 JSCVoid extends AbstractJSCData {
private Object value = null;
private final String name;
private JSCDataEvent clearEvent = null;
private JSCDataEvent notEmptyEvent = null;
/**
* XML format field for (de)serialization.
*/
public static final XmlFormat JSCVoid_XML = new XmlFormat(
JSCVoid.class) {
public void format(JSCVoid data, XmlElement xml) {
synchronized (data) {
xml.setAttribute("name", data.name);
if (data.value != null)
xml.add(data.value);
}
}
public JSCVoid parse(XmlElement xml) {
String nam = xml.getAttribute("name") + "";
Object data = null;
if (xml.hasNext())
data = xml.getNext();
return new JSCVoid(nam, data);
}
};
/**
* Creates an empty JSCVoid.
*
* @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 JSCVoid(String name) {
this(name, null);
}
/**
* Creates an empty JSCVoid with a default name.
*
*/
public JSCVoid() {
this(JSCTypes.VOID + "_DEFAULT");
}
/**
* Creates a JSCVoid from value.
*
* @param name
* the identifier of that object
* @param value
* the Object to be used as value
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCVoid(String name, Object 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() {
Object 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 JSCVoid copy() {
return new JSCVoid(name, value);
}
/**
* Gets whether this is empty. If this method returns
* true, then value returns null.
*
* @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 JSCVoid))
return false;
JSCVoid otherVoid = (JSCVoid) o;
boolean ret = true;
synchronized (this) {
synchronized (o) {
if (value != null)
ret = value.equals(otherVoid.value());
else
ret = otherVoid.value() == 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(value())
* 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(Object newValue) {
if (newValue == null) {
clear();
return;
}
boolean wasEmpty = false;
Object oldValue = null;
synchronized (this) {
// Nothing changed.
if (newValue.equals(value))
return;
wasEmpty = isEmpty();
oldValue = value;
value = newValue;
}
// Dispatch event outside synchronized block.
if (eventSupport == null)
return;
if (getEventSupport().getListenersForType(
JSCDataEventTypes.VALUE_CHANGED).size() > 0
&& !newValue.equals(oldValue))
getEventSupport().dispatchEvent(
JSCDataEvent.valueOfChanged(this, oldValue, newValue));
if (wasEmpty) {
if (notEmptyEvent == null)
notEmptyEvent = JSCDataEvent.valueOfEmptyState(this, false);
getEventSupport().dispatchEvent(notEmptyEvent);
}
}
/**
* Gets the type of this JSCData object.
*
* @return JSCTypes.VOID
*/
public JSCTypes type() {
return JSCTypes.VOID;
}
/**
* Gets the underlying value, which is stored as an Object.
*
* @return the value object or null if this is
* empty
*/
public synchronized Object value() {
return value;
}
}
Ads help maintain this website.