/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program 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 program 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
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2004, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: TimeStampedDataSource.java,v 1.22 2008/06/19 13:25:26 ogor Exp $
*
* Changes
* -------
* 11 jan. 2005 : Initial public release (CC);
*
*/
package simtools.data.async;
import java.io.IOException;
import java.util.AbstractList;
import java.util.Collection;
import simtools.data.DataException;
import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.data.UnsupportedOperation;
/**
* A data source to put together the value and its sampling date It is designed
* to collect asynchronous data sources in one collection Each data has its owwn
* sampling date, there is no simple way to correlate the data belonging to this
* king of data collection since the data index can not be used to get 2
* synchronised values.
*
* @author Claude Cazenave
*/
public abstract class TimeStampedDataSource extends DataSource {
private TimeSource _time;
private String _name;
private Double _min = null;
private Double _max = null;
private boolean _minmax = false;
private DataInfo _info = null;
private final TimeStampedDataSourceCollection _collection;
protected AuxiliaryCollection _auxiliaries;
/**
* @param label
* The data source name
* @param id
* The data source id
* @param c
* The collection holding this data
* @throws IOException
* @throws TimeStampedDataSourceCollection.InvalidFormatException
*/
public TimeStampedDataSource(String label, String id, TimeStampedDataSourceCollection c) throws IOException,
TimeStampedDataSourceCollection.InvalidFormatException {
setName(id);
_collection = c;
setInfo(new DataInfo(label, id, "", ""));
setTime(null); // a concrete class is required
_auxiliaries = new AuxiliaryCollection();
}
/**
* Create an empty data source with same references than current data source.
* A such empty data source can be replaced later when new collections have been added to pool
* @return an empty data source
*/
protected DataSource createEmptyDataSource() {
try {
return new TimeStampedEmptyDataSource(DataInfo.getId(this), DataInfo.getId(_collection), null); // by default
} catch (IOException e) {
} catch (TimeStampedDataSourceCollection.InvalidFormatException e) {
}
return null;
}
public abstract double getStart() throws DataException;
public abstract double getEnd() throws DataException;
protected abstract void computeMinMax() throws UnsupportedOperation;
/*
* (non-Javadoc)
*
* @see simtools.data.ValueProvider#getValue(long)
*/
public abstract Object getValue(long index) throws DataException;
/*
* (non-Javadoc)
*
* @see simtools.data.ValueProvider#getDoubleValue(long)
*/
public abstract double getDoubleValue(long index) throws DataException;
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getStartIndex()
*/
public abstract long getStartIndex() throws UnsupportedOperation;
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getLastIndex()
*/
public abstract long getLastIndex() throws UnsupportedOperation;
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getDoubleMin()
*/
public double getDoubleMin() throws DataException {
if (!isMinmax()) {
computeMinMax();
}
return ((Double) getMin()).doubleValue();
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getDoubleMin()
*/
public double getDoubleMax() throws DataException {
if (!isMinmax()) {
computeMinMax();
}
return ((Double) getMax()).doubleValue();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return getName();
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getInformation()
*/
public DataInfo getInformation() {
return getInfo();
}
/**
* @return Returns the _collection.
*/
public TimeStampedDataSourceCollection getCollection() {
return _collection;
}
/**
* @param time
* The time to set.
*/
public void setTime(TimeSource time) {
_time = time;
}
/**
* @return Returns the time.
*/
public TimeSource getTime() {
return _time;
}
/**
* @param name
* The name to set.
*/
void setName(String name) {
_name = name;
}
/**
* @return Returns the name.
*/
protected String getName() {
return _name;
}
/**
* @param minmax
* The minmax to set.
*/
protected void setMinmax(boolean minmax) {
_minmax = minmax;
}
/**
* @return Returns the minmax.
*/
boolean isMinmax() {
return _minmax;
}
/**
* @param info
* The info to set.
*/
protected void setInfo(DataInfo info) {
_info = info;
}
/**
* @return Returns the info.
*/
protected DataInfo getInfo() {
return _info;
}
/**
* @param min
* The min to set.
*/
protected void setMin(Double min) {
_min = min;
}
/**
* @return Returns the min.
*/
public Object getMin() {
return _min;
}
/**
* @param max
* The max to set.
*/
protected void setMax(Double max) {
_max = max;
}
/**
* @return Returns the max.
*/
public Object getMax() {
return _max;
}
/**
* @return true if it is valid
*/
public abstract boolean isValid();
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#auxiliarySources()
*/
public Collection getAuxiliarySources() {
return _auxiliaries;
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#isCompound()
*/
public boolean isCompound() {
return true;
}
public abstract class TimeSource extends DataSource {
/*
* (non-Javadoc)
*
* @see simtools.data.ValueProvider#getValue(long)
*/
protected DataInfo info;
public TimeSource(String name) {
info = new DataInfo(
DataInfo.getLabel(TimeStampedDataSource.this) + "." + name,
DataInfo.getId(TimeStampedDataSource.this) + "." + name);
}
public TimeSource() {
this("time");
}
public abstract Object getValue(long index) throws DataException;
/*
* (non-Javadoc)
*
* @see simtools.data.ValueProvider#getDoubleValue(long)
*/
public abstract double getDoubleValue(long index) throws DataException;
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getStartIndex()
*/
public long getStartIndex() throws UnsupportedOperation {
return TimeStampedDataSource.this.getStartIndex();
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getLastIndex()
*/
public long getLastIndex() throws UnsupportedOperation {
return TimeStampedDataSource.this.getLastIndex();
}
/* (non-Javadoc)
* @see simtools.data.DataSource#getMin()
*/
public Object getMin() throws UnsupportedOperation {
try {
return new Double(TimeStampedDataSource.this.getStart());
} catch (DataException e) {
throw new UnsupportedOperation(e.getMessage());
}
}
/* (non-Javadoc)
* @see simtools.data.DataSource#getMax()
*/
public Object getMax() throws UnsupportedOperation {
try {
return new Double(TimeStampedDataSource.this.getEnd());
} catch (DataException e) {
throw new UnsupportedOperation(e.getMessage());
}
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#sortedOrder()
*/
public int sortedOrder() {
return 1;
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getInformation()
*/
public DataInfo getInformation() {
return info;
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#isAuxiliary()
*/
public boolean isAuxiliary() {
return true;
}
}
class AuxiliaryCollection extends AbstractList {
/*
* (non-Javadoc)
*
* @see java.util.AbstractList#get(int)
*/
public Object get(int index) {
if (index == 0) {
return _time;
}
return null;
}
/*
* (non-Javadoc)
*
* @see java.util.AbstractCollection#size()
*/
public int size() {
return 1;
}
}
/*
* (non-Javadoc)
*
* @see simtools.data.DataSource#getDataSourceInformationClass()
*/
public String getDataSourceInformationClass() {
return "simtools.ui.TimeStampedDataSourceInformation";
}
}