/*
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.ts.TSDate;
/**
* Adapter to TSDate objects that conforms to the
* JSCData interface. Everytime something changes, a new
* TSDate object is created, because date objects are immutable.
*
* 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
* @see com.jstatcom.ts.TSDate
*/
public final class JSCDate extends AbstractJSCData {
private TSDate tsDate = null;
private final String name;
private JSCDataEvent clearEvent = null;
private JSCDataEvent notEmptyEvent = null;
/**
* XML format field for (de)serialization.
*/
public static final XmlFormat JSCDate_XML = new XmlFormat(
JSCDate.class) {
public void format(JSCDate data, XmlElement xml) {
xml.setAttribute("name", data.name);
synchronized (data) {
xml.setAttribute("value", data.tsDate + "");
}
}
public JSCDate parse(XmlElement xml) {
String nam = xml.getAttribute("name") + "";
String dateString = xml.getAttribute("value") + "";
TSDate newDate = null;
if (!dateString.equalsIgnoreCase("null"))
newDate = TSDate.valueOf(dateString);
return new JSCDate(nam, newDate);
}
};
/**
* Creates an empty JSCDate.
*
* @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 JSCDate(String name) {
this(name, null);
}
/**
* Creates a JSCDate for a given TSDate.
*
* @param name
* the identifier of that object
* @param tsDate
* the date value
* @throws IllegalArgumentException
* if name is not valid or
* if (name == null)
*
* @see com.jstatcom.model.JSCConstants#isValidName
*/
public JSCDate(String name, TSDate tsDate) {
super();
if (name == null)
throw new IllegalArgumentException("Argument was null.");
name = JSCConstants.checkNameThrowEx(name);
this.name = name;
this.tsDate = tsDate;
}
/**
* Adds i periods to this date.
*
* @param i
* number of periods, can also be negative, nothing changes
* if (i == 0)
* @throws IllegalStateException
* isEmpty
*/
public void addPeriods(int i) {
TSDate oldValue = null;
TSDate newValue = null;
synchronized (this) {
// Nothing changed.
if (tsDate == null)
throw new IllegalStateException("JSCDate is empty.");
if (i == 0)
return;
// No copy needed, because TSDate is immutable.
oldValue = tsDate;
tsDate = tsDate.addPeriods(i);
newValue = tsDate;
}
// Dispatch event outside synchronized block.
if (eventSupport == null)
return;
getEventSupport().dispatchEvent(
JSCDataEvent.valueOfChanged(this, oldValue, newValue));
}
/**
* 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() {
TSDate oldValue;
synchronized (this) {
if (tsDate == null) // Nothing changes.
return;
oldValue = tsDate;
tsDate = null;
}
if (eventSupport == null)
return;
// 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 JSCDate copy() {
return new JSCDate(name, tsDate);
}
/**
* Gets the stored date object.
*
* @return the used TSDate or null if empty
*/
public synchronized TSDate getTSDate() {
return tsDate;
}
/**
* Gets whether this is empty. This method returns
* true, if value returns null.
*
* @return true if empty
*/
public synchronized boolean isEmpty() {
return tsDate == null;
}
/**
* @see com.jstatcom.model.JSCData
*/
public boolean isEqual(JSCData o) {
if (o == this)
return true;
if (!(o instanceof JSCDate))
return false;
JSCDate otherDate = (JSCDate) o;
boolean ret = true;
synchronized (this) {
synchronized (o) {
if (tsDate != null)
ret = tsDate.equals(otherDate.getTSDate());
else
ret = otherDate.getTSDate() == null;
}
}
return ret;
}
/**
* @see com.jstatcom.model.JSCData
*/
public String name() {
return name;
}
/**
* Sets a new value to this date adapter. Fires a JSCDataEvent
* of type:
*
* JSCDataEventTypes.VALUE_CHANGED if
* !newDate.equals(getTSDate())
* JSCDataEventTypes.EMPTY_STATE if
* newDate != null and this was empty before
* JSCDataEventTypes.EMPTY_STATE if
* newDate == null and this was not empty before
*
*
* @param newDate
* the new date, if null it is similar to
* clear
*/
public void setVal(TSDate newDate) {
if (newDate == null) {
clear();
return;
}
boolean wasEmpty = false;
TSDate oldValue = null;
synchronized (this) {
// Nothing changed.
if (newDate.equals(tsDate))
return;
wasEmpty = isEmpty();
// No copy needed, because TSDate is immutable.
oldValue = tsDate;
tsDate = newDate;
}
// Dispatch event outside synchronized block.
if (eventSupport == null)
return;
getEventSupport().dispatchEvent(
JSCDataEvent.valueOfChanged(this, oldValue, newDate));
if (wasEmpty) {
if (notEmptyEvent == null)
notEmptyEvent = JSCDataEvent.valueOfEmptyState(this, false);
getEventSupport().dispatchEvent(notEmptyEvent);
}
}
/**
* Gets the type of this JSCData object.
*
* @return JSCTypes.DATE
*/
public JSCTypes type() {
return JSCTypes.DATE;
}
/**
* Gets the underlying value, which is stored as a TSDate.
*
* @return the value object or null if this is
* empty
*/
public synchronized Object value() {
return tsDate;
}
}
Ads help maintain this website.