/*
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;
import com.jstatcom.util.UString;
/**
* An integer representation that conforms to the JSCData
* interface. Values are stored as Integer objects.
*
* 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 JSCInt extends AbstractJSCData {
private final String name;
private Integer value = null;
private JSCDataEvent clearEvent = null;
private JSCDataEvent notEmptyEvent = null;
/**
* XML format field for (de)serialization.
*/
public static final XmlFormat JSCInt_XML = new XmlFormat(
JSCInt.class) {
public void format(JSCInt data, XmlElement xml) {
xml.setAttribute("name", data.name);
synchronized (data) {
xml.setAttribute("value", data.value + "");
}
}
public JSCInt parse(XmlElement xml) {
String nam = xml.getAttribute("name") + "";
String numberString = xml.getAttribute("value") + "";
Double newNumber = null;
if (!numberString.equalsIgnoreCase("null"))
newNumber = UString.parseToNumber(numberString);
Integer newInt = null;
if (newNumber != null)
newInt = new Integer(newNumber.intValue());
return new JSCInt(nam, newInt);
}
};
/**
* Creates an empty JSCInt.
*
* @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 JSCInt(String name) {
this(name, null);
}
/**
* Creates a JSCInt from an integer value.
*
* @param name
* the identifier of that object
* @param value
* the int to be used as value
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCInt(String name, int value) {
this(name, new Integer(value));
}
/**
* Creates a JSCInt from a boolean value.
*
* @param name
* the identifier of that object
* @param value
* if true, set integer value to 1, if
* false set integer value to 0
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCInt(String name, boolean value) {
this(name, new Integer(value ? 1 : 0));
}
/**
* Creates a JSCInt from value.
*
* @param name
* the identifier of that object
* @param value
* the Integer to be used as value
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCInt(String name, Integer 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() {
Integer 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 JSCInt copy() {
return new JSCInt(name, value);
}
/**
* Gets the stored integer as a double.
*
* @return the value as double
* @throws IllegalStateException
* if object is empty
*/
public synchronized double doubleVal() {
if (value == null)
throw new IllegalStateException("JSCData is empty: " + toString()
+ ".");
return value.doubleValue();
}
/**
* Gets the stored Integer value.
*
* @return a Integer or null if empty
*/
public synchronized Integer getInt() {
return value;
}
/**
* Gets the stored integer.
*
* @return the int values
* @throws IllegalStateException
* if object is empty
*/
public synchronized int intVal() {
if (value == null)
throw new IllegalStateException("JSCData is empty: " + toString()
+ ".");
return value.intValue();
}
/**
* Gets whether this is empty. This method returns
* true, if 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 JSCInt))
return false;
JSCInt otherNumber = (JSCInt) o;
boolean ret = true;
synchronized (this) {
synchronized (o) {
if (value != null)
ret = value.equals(otherNumber.getInt());
else
ret = otherNumber.getInt() == null;
}
}
return ret;
}
/**
* @see com.jstatcom.model.JSCData
*/
public String name() {
return name;
}
/**
* Sets the value of this to newValue and fires a
* JSCDataEvent of type:
*
* JSCDataEventTypes.VALUE_CHANGED if
* !newValue.equals(getNumber())
* 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 setInt(Integer newValue) {
if (newValue == null) {
clear();
return;
}
boolean wasEmpty = false;
Integer 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);
}
}
/**
* Sets the value of this to newInt. Fires a
* JSCDataEvent of type:
*
* JSCDataEventTypes.VALUE_CHANGED if the new value is
* not equal to the previous
* JSCDataEventTypes.EMPTY_STATE if this was empty
* before
*
*
* @param newInt
* the new int
*/
public void setVal(int newInt) {
setInt(new Integer(newInt));
}
/**
* Gets the type of this JSCData object.
*
* @return JSCTypes.INT
*/
public JSCTypes type() {
return JSCTypes.INT;
}
/**
* Gets the underlying value, which is stored as a Double.
*
* @return the value object or null if this is
* empty
*/
public synchronized Object value() {
return value;
}
/**
* Gets whether the stored value is not zero.
*
* @return true if not equal to zero, false
* otherwise
* @throws IllegalStateException
* if object is empty
*/
public synchronized boolean isNonZero() {
if (value == null)
throw new IllegalStateException("JSCData is empty: " + toString()
+ ".");
return value.intValue() != 0;
}
}
Ads help maintain this website.