/* ========================
* 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-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Nicolas Brodu
*
* $Id: RandomSourceProvider.java,v 1.2 2004/01/29 11:09:42 brodu Exp $
*
* Changes
* -------
* 27-Jan-2004 : Creation date (NB);
*
*/
package jsynoptic.builtin;
import simtools.data.DataSource;
import simtools.data.DataSourceCollection;
import simtools.data.DataSourcePool;
import simtools.data.DataSourceProvider;
/**
* Used in serializing/deserializing random data sources
*/
public class RandomSourceProvider implements DataSourceProvider {
/* (non-Javadoc)
* @see simtools.data.DataSourceProvider#getOptionalInformation(simtools.data.DataSource, simtools.data.DataSourceCollection)
*/
public Object getOptionalInformation(DataSource ds, DataSourceCollection dsc) {
if (!(ds instanceof RandomSource)) return null;
RandomSource r = (RandomSource)ds;
Double param1 = new Double(r.param1);
Double param2 = new Double(r.param2);
String name = r.getInformation().label;
Long seed = r.seed; // may be null
Long nSamples = new Long(r.maxIndex+1); // may be 0
return new Object[] {name, seed, param1, param2, nSamples};
}
/* (non-Javadoc)
* @see simtools.data.DataSourceProvider#provide(java.lang.String, java.lang.String, java.lang.Object, simtools.data.DataSourcePool)
*/
public DataSource provide(String id, String dscId, Object optionalInformation, DataSourcePool pool) {
// mind this provider's own business
if ((!id.startsWith(GaussianRandomSource.ID_PREFIX)) && (!id.startsWith(UniformRandomSource.ID_PREFIX)))
return null;
if (!(optionalInformation instanceof Object[])) return null;
Object[] info = (Object[])optionalInformation;
if (info.length!=5) return null;
DataSource ret = null;
if (id.startsWith(GaussianRandomSource.ID_PREFIX)) {
if (info[1]==null) ret = new GaussianRandomSource((String)info[0], ((Double)info[2]).doubleValue(), ((Double)info[3]).doubleValue(), ((Long)info[4]).longValue());
else ret = new GaussianRandomSource((String)info[0], ((Long)info[1]).longValue(), ((Double)info[2]).doubleValue(), ((Double)info[3]).doubleValue(), ((Long)info[4]).longValue());
}
else if (id.startsWith(UniformRandomSource.ID_PREFIX)) {
if (info[1]==null) ret = new UniformRandomSource((String)info[0], ((Double)info[2]).doubleValue(), ((Double)info[3]).doubleValue(), ((Long)info[4]).longValue());
else ret = new UniformRandomSource((String)info[0], ((Long)info[1]).longValue(), ((Double)info[2]).doubleValue(), ((Double)info[3]).doubleValue(), ((Long)info[4]).longValue());
}
if (pool!=null) pool.addDataSource(ret);
return ret;
}
}