package jas2.swingstudio;
import java.util.*;
import jas2.util.rmi.*;
import java.rmi.*;
import jas2.hist.*;
import jas2.jds.interfaces.*;
public class JASRebinAdaptor
extends Observable
implements Rebinnable1DHistogramData,Observer,HasStyle, HasStatistics, ExtendedStatistics
{
public JASRebinAdaptor(RemoteRebinnable1DHistogramData source, RMIDestination obs)
{
obs.addObserver(this);
m_remote = source;
}
public void update(Observable obs, Object arg)
{
HistogramUpdate hu = (HistogramUpdate) arg;
if (debug) System.out.println("JASRebinAdaptor: update "+hu);
setChanged();
synchronized (this)
{
if (hu.isDataUpdate() ) { cacheStats = null; cacheData = null; }
if (hu.isRangeUpdate()) { info = null; axisLabels = null; }
if (hu.isTitleUpdate()) { info = null; }
if (hu.isReset()) { info = null; cacheStats = null; cacheData = null; axisLabels = null; }
}
notifyObservers(hu);
}
/**
* Fetch binned data from remote data source. Also caches
* result to speed up subsequent requests for the same
* binned data.
*/
public double[][] rebin(int bins, double min, double max, boolean wantErrors, boolean hurry)
{
// Is cached information valid?
if (cacheData != null)
{
if (bins != cacheBins ||
min != cacheMin ||
max != cacheMax ||
(wantErrors && !cacheErrors)) cacheData = null;
}
double[][] result = cacheData;
if (result == null)
{
try
{
synchronized (this)
{
result = m_remote.remoteRebin(bins,min,max,wantErrors,hurry);
cacheData = result;
}
if (hurry) return result; // no caching
cacheBins = bins;
cacheMin = min;
cacheMax = max;
cacheErrors = wantErrors;
}
catch (ServerRuntimeException e)
{
System.out.println("Server Runtime exception!!");
e.detail.printStackTrace();
}
catch (RemoteException e)
{
System.out.println("Terrible problem during remote rebin!!");
e.printStackTrace();
}
}
else if (debug) { System.out.println("Data found in cache"); }
return result; // Not cacheData which may be null!
}
public double getMin()
{
// need a local copy of myinfo to avoid it being zeroed by another thread
Remote1DHistogramInfo myinfo = info;
if (myinfo == null) myinfo=setInfo();
return myinfo.m_min;
}
public double getMax()
{
// need a local copy of myinfo to avoid it being zeroed by another thread
Remote1DHistogramInfo myinfo = info;
if (myinfo == null) myinfo=setInfo();
return myinfo.m_max;
}
public int getBins()
{
// need a local copy of myinfo to avoid it being zeroed by another thread
Remote1DHistogramInfo myinfo = info;
if (myinfo == null) myinfo=setInfo();
return myinfo.m_bins;
}
public int getAxisType()
{
// need a local copy of myinfo to avoid it being zeroed by another thread
Remote1DHistogramInfo myinfo = info;
if (myinfo == null) myinfo=setInfo();
return myinfo.m_axisType;
}
public boolean isRebinnable()
{
// need a local copy of myinfo to avoid it being zeroed by another thread
Remote1DHistogramInfo myinfo = info;
if (myinfo == null) myinfo=setInfo();
return myinfo.m_isRebinnable;
}
public String getTitle()
{
// need a local copy of myinfo to avoid it being zeroed by another thread
Remote1DHistogramInfo myinfo = info;
if (myinfo == null) myinfo=setInfo();
return myinfo.m_title;
}
public String[] getAxisLabels()
{
// need a local copy to avoid it being zeroed by another thread
String myAxisLabels[] = axisLabels;
if (myAxisLabels == null) myAxisLabels = getRemoteAxisLabels();
return myAxisLabels;
}
public JASHistStyle getStyle()
{
try
{
return m_remote.getStyle();
}
catch (RemoteException e)
{
System.out.println(e); // BUG
return null;
}
}
private String[] getRemoteAxisLabels()
{
String[] myAxisLabels;
try
{
myAxisLabels = m_remote.getRemoteAxisLabels();
axisLabels = myAxisLabels;
}
catch (RemoteException e)
{
System.out.println(e); // BUG
return null;
}
return myAxisLabels;
}
private Remote1DHistogramInfo setInfo()
{
Remote1DHistogramInfo myinfo;
try
{
myinfo = m_remote.getRemoteInfo();
info = myinfo;
}
catch (RemoteException e)
{
System.out.println(e); // BUG
return null;
}
return myinfo;
}
public String toString()
{
try
{
return m_remote.getDescription();
}
catch (RemoteException e)
{
return super.toString();
}
}
public Statistics getStatistics()
{
// This must return null if the remote datasource doesnt actually have
// any statistics
Statistics stats = cacheStats;
if (stats == null) stats = getStats();
return (stats == null) ? null : this;
}
public String[] getStatisticNames()
{
Statistics stats = cacheStats;
if (stats == null) stats = getStats();
return stats.getStatisticNames();
}
public double getStatistic(String name)
{
Statistics stats = cacheStats;
if (stats == null) stats = getStats();
return stats.getStatistic(name);
}
public Object getExtendedStatistic(String name)
{
Statistics stats = cacheStats;
if (stats == null) stats = getStats();
if (stats instanceof ExtendedStatistics) return ((ExtendedStatistics) stats).getExtendedStatistic(name);
return null;
}
private Statistics getStats()
{
try
{
Statistics stats = m_remote.getStatistics();
cacheStats = stats;
return stats;
}
catch (RemoteException e)
{
System.out.println(e); // BUG
return null;
}
}
static DataSource getAdaptor(Object obj, RMIEventDelivery ed, RMIDestination dest) throws RemoteException
{
if (obj instanceof RemoteRebinnable1DHistogramData) return new JASRebinAdaptor((RemoteRebinnable1DHistogramData) obj,dest);
else if (obj instanceof RemoteScatterSource ) return new JASScatterAdaptor((RemoteScatterSource) obj,dest,ed);
else if (obj instanceof RemoteRebinnable2DHistogramData) return new JAS2DRebinAdaptor((RemoteRebinnable2DHistogramData) obj,dest,ed);
else throw new RuntimeException("Unknown RemoteDataSource");
}
private double[][] cacheData;
private Statistics cacheStats;
private int cacheBins;
private double cacheMin;
private double cacheMax;
private boolean cacheErrors;
private Remote1DHistogramInfo info;
private String[] axisLabels;
private RemoteRebinnable1DHistogramData m_remote;
private static final boolean debug = System.getProperty("debugCache") != null;
}
Ads help maintain this website.