/*
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.ts;
import java.util.ArrayList;
import java.util.List;
import javolution37.javolution.xml.XmlElement;
import javolution37.javolution.xml.XmlFormat;
import com.jstatcom.model.JSCData;
import com.jstatcom.model.JSCTypes;
import com.jstatcom.project.ProjectData;
import com.jstatcom.project.ProjectDataTypes;
/**
* Implementation of ProjectData for the time series data type.
* It stores the data for a project and can be (de)serialized.
*
* @author Markus Kraetzig
*/
public final class TSProjectData implements ProjectData {
private JSCData[] dataArray = new JSCData[0];
private String description = "";
/**
* XML format field for (de)serialization.
*/
public static final XmlFormat TSProjectData_XML = new XmlFormat(
TSProjectData.class) {
public void format(TSProjectData data, XmlElement xml) {
xml.setAttribute("description", data.getDescription());
List list = new ArrayList();
JSCData[] array = data.getJSCData();
for (int i = 0; i < array.length; i++)
list.add(array[i]);
for (JSCData el : list)
xml.add(el);
}
public TSProjectData parse(XmlElement xml) {
String description = (xml.getAttribute("description") + "");
List dataList = new ArrayList();
for (int i = 0; xml.hasNext(); i++)
dataList.add((JSCData) xml.getNext());
// implemented by respective type
TSProjectData data = new TSProjectData();
data.setDescription(description);
data.setJSCData(dataList.toArray(new JSCData[dataList.size()]));
return data;
}
};
/**
* Default constructor.
*/
public TSProjectData() {
}
/**
* Gets the data description.
*
* @return descriptive string
*/
public String getDescription() {
return description;
}
/**
* Sets the data description. If the string argument is null
* then "" is used instead.
*
* @param dataDescription
* a descriptive string
*/
public void setDescription(String dataDescription) {
if (dataDescription == null)
dataDescription = "";
this.description = dataDescription;
}
/**
* Checks whether data matches given type.
* The index parameter is only for generating messages.
*
* @param data
* @param type
* @param index
* @throws RuntimeException
* if type check fails
*/
private void checkData(JSCData data, JSCTypes type, int index) {
if (data == null)
throw new RuntimeException(
"Error reading TS project data. Null data at array index "
+ index + ".");
if (!(data.type() == type && !data.isEmpty()))
throw new RuntimeException(
"Error reading TS project data. Expected nonempty " + type
+ " at array index " + index + ", but was "
+ data.type() + " (empty=" + data.isEmpty() + ").");
}
/**
* Sets the data array for the underlying data. The array is a vector with
* 4*(number of time series) elements which must be in the following order:
*
*
* JSCString - string with the name of the 1st time
* series
* JSCString - string representing the data type of the
* 1st time series, for example "endogenous"
* JSCDate - start date of the 1st time series
* JSCNArray - data vector of the 1st time series
* - ... the same for the remaining time series
*
*
* Usage Note:
* The argument data array is not copied by this method but the reference is
* used. Clients should make sure that the data is not modified afterwards.
* This is reasonable because usually the data is not referenced from
* outside the calling method.
*
* @param dataArray
* the array with the data to be stored
* @throws IllegalArgumentException
* if (dataArray == null)
* @throws RuntimeException
* if the elements in the data array are not of the correct
* type, or if there are null elements, or if the
* number of array elements is not a multiple of 4
*/
public void setJSCData(JSCData[] dataArray) {
if (dataArray == null)
throw new IllegalArgumentException("Argument was null");
int tsCount = dataArray.length / 4;
if (dataArray.length % 4 != 0)
throw new RuntimeException("Wrong number of array elements: "
+ dataArray.length + " not a multiple of 4.");
for (int i = 0, k = 0; i < tsCount; i++) {
JSCData data_k = dataArray[k++];
checkData(data_k, JSCTypes.STRING, k - 1);
data_k = dataArray[k++];
checkData(data_k, JSCTypes.STRING, k - 1);
data_k = dataArray[k++];
checkData(data_k, JSCTypes.DATE, k - 1);
data_k = dataArray[k++];
checkData(data_k, JSCTypes.NARRAY, k - 1);
}
this.dataArray = dataArray;
}
/**
* Gets the project data as an array of JSCData objects. For
* the ordering of the elements of this array, see the
* setJSCData method.
*
* Usage Note:
* The returned data array is a reference to the array used by this object.
* Clients should not modify it.
*
* @return reference to the data array
* @see #setJSCData(JSCData[])
*/
public JSCData[] getJSCData() {
return dataArray;
}
/**
* Gets the time series data type.
*
* @return ProjectDataTypes.TS_DATA
*/
public ProjectDataTypes getProjectDataType() {
return ProjectDataTypes.TS_DATA;
}
}
Ads help maintain this website.