package jas2.swingstudio;

import java.util.*;
import jas2.util.rmi.*; 
import java.rmi.*; 
import jas2.hist.*;
import jas2.jds.interfaces.*;

public class JAS2DRebinAdaptor
	extends Observable 
	implements Rebinnable2DHistogramData,Observer, HasSlices, HasStyle, HasStatistics, ExtendedStatistics
{
	public JAS2DRebinAdaptor(RemoteRebinnable2DHistogramData source, RMIDestination obs, RMIEventDelivery ed)
	{
		obs.addObserver(this);
		m_remote = source;
		m_ed = ed;
	}
	public void update(Observable obs, Object arg)
	{
		HistogramUpdate hu = (HistogramUpdate) arg;
		setChanged();
		synchronized (this) 
		{ 
			if (hu.isDataUpdate() ) { cacheStats = null; cacheData = null; }
			if (hu.isRangeUpdate()) { info = null; xAxisLabels = null; yAxisLabels = null; }
			if (hu.isTitleUpdate()) { info = null; }
			if (hu.isReset())       { info = null; cacheStats = null; cacheData = null; xAxisLabels = null; yAxisLabels = 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 xbins, double xmin, double xmax, int ybins, double ymin, double ymax, boolean wantErrors, boolean hurry, boolean overflow)
	{
		// Is cached information valid?

		if (cacheData != null)
		{
			if (!xAxisCache.equals(xbins,xmin,xmax) ||
				!yAxisCache.equals(ybins,ymin,ymax) ||
				(wantErrors && !cacheErrors)) cacheData = null;
		}
		double[][][] result = cacheData;
		if (result == null)
		{
			try 
			{
				synchronized (this)
				{	
					result = m_remote.remoteRebin(xbins,xmin,xmax,ybins,ymin,ymax,wantErrors,hurry,overflow);
					cacheData = result;
				}
				if (hurry) return result; // no caching

				xAxisCache.setCache(xbins,xmin,xmax);
				yAxisCache.setCache(ybins,ymin,ymax);
				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("2D Data found in cache"); }
		return result; // Not cacheData which may be null!
	}
	public double getXMin()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_xAxis.m_min;
	}
	public double getXMax()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_xAxis.m_max;
	}
	public double getYMin()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_yAxis.m_min;
	}
	public double getYMax()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_yAxis.m_max;
	}
	public int getXBins()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_xAxis.m_bins;
	}
	public int getYBins()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_yAxis.m_bins;
	}
	public int getXAxisType()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_xAxis.m_axisType;
	}
	public int getYAxisType()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_yAxis.m_axisType;
	}
	public String[] getXAxisLabels()
	{
		// need a local copy to avoid it being zeroed by another thread
		String myAxisLabels[] = xAxisLabels;
		if (myAxisLabels == null) myAxisLabels = getRemoteXAxisLabels();
		return myAxisLabels;
	}
	public String[] getYAxisLabels()
	{
		// need a local copy to avoid it being zeroed by another thread
		String myAxisLabels[] = yAxisLabels;
		if (myAxisLabels == null) myAxisLabels = getRemoteYAxisLabels();
		return myAxisLabels;
	}
	public boolean isRebinnable()
	{
		// need a local copy of myinfo to avoid it being zeroed by another thread
		Remote2DHistogramInfo 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
		Remote2DHistogramInfo myinfo = info;
		if (myinfo == null) myinfo=setInfo();
		return myinfo.m_title;
	}
	private String[] getRemoteXAxisLabels()
	{
		String[] myAxisLabels; 
		try
		{
			myAxisLabels = m_remote.getRemoteXAxisLabels();	
			xAxisLabels = myAxisLabels;
		}
		catch (RemoteException e)
		{
			System.out.println(e); // BUG
			return null;
		}
		return myAxisLabels;
	}
	private String[] getRemoteYAxisLabels()
	{
		String[] myAxisLabels; 
		try
		{
			myAxisLabels = m_remote.getRemoteYAxisLabels();	
			yAxisLabels = myAxisLabels;
		}
		catch (RemoteException e)
		{
			System.out.println(e); // BUG
			return null;
		}
		return myAxisLabels;
	}
	protected Remote2DHistogramInfo setInfo()
	{
		Remote2DHistogramInfo 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;
		}
	}
	public JASHistStyle getStyle()
	{
		try
		{
			return m_remote.getStyle();
		}
		catch (RemoteException e)
		{
			System.out.println(e); // BUG
			return null;
		}
	}
	private double[][][] cacheData;
	private boolean cacheErrors;
	private class AxisCache
	{
		int m_bins;
		double m_min;
		double m_max;
		void setCache(int bins, double min, double max)
		{
			m_bins = bins;
			m_min = min;
			m_max = max;
		}
		boolean equals(int bins, double min, double max)
		{
			return bins == m_bins && min == m_min && max == m_max;
		}
	}
	private AxisCache xAxisCache = new AxisCache();
	private AxisCache yAxisCache = new AxisCache();
	protected Remote2DHistogramInfo info;
	private Statistics cacheStats;
	private String[] xAxisLabels;
	private String[] yAxisLabels;
	private RemoteRebinnable2DHistogramData m_remote;
	private RMIEventDelivery m_ed;
	private static final boolean debug = System.getProperty("debugCache") != null;

	public int getNSlices()
	{
		try
		{
			return m_remote.getNSlices();
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
			return 0;
		}
	}

	public SliceParameters getSliceParameters(int n)
	{
		try
		{
			return new JASSliceAdaptor(m_remote.getSliceParameters(n));
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
			return null;
		}
	}

	public Rebinnable1DHistogramData getSlice(int n)
	{
		try
		{
			RMIDestination dest = m_ed.getRMIDestination();
			return new JASRebinAdaptor(m_remote.getSlice(n,dest),dest);
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
			return null;
		}
	}

	public boolean canAddRemoveSlices()
	{
		try
		{
			return m_remote.canAddRemoveSlices();
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
			return false;
		}
	}

	public int addSlice(double x, double y, double width, double height, double phi)
	{
		try
		{
			return m_remote.addSlice(x,y,width,height,phi);
		}
		catch (RemoteException e)
		{
			System.err.println(e); // BUG
			return 0;
		}
	}

	public void removeSlice(int n)
	{
		try
		{
			m_remote.removeSlice(n);
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
		}
	}   
}
class JASSliceAdaptor implements SliceParameters
{
	JASSliceAdaptor(RemoteSliceParameters remote)
	{
		m_remote = remote;
	}
	public double getX()
	{
		if (!cacheValid) setCache();
		return info.x;
	}

	public double getY()
	{
		if (!cacheValid) setCache();
		return info.y;
	}

	public double getWidth()
	{
		if (!cacheValid) setCache();
		return info.width;
	}

	public double getHeight()
	{
		if (!cacheValid) setCache();
		return info.height;
	}

	public double getPhi()
	{
		if (!cacheValid) setCache();
		return info.phi;
	}

	public void setX(double x)
	{
		try
		{
			m_remote.setX(x);
			cacheValid = false;
		}
		catch (RemoteException e)
		{
			System.err.println(e); // BUG
		}
	}

	public void setY(double y)
	{
		try
		{
			m_remote.setY(y);
			cacheValid = false;
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
		}
	}

	public void setWidth(double width)
	{
		try
		{
			m_remote.setWidth(width);
			cacheValid = false;
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
		}
	}

	public void setHeight(double height)
	{
		try
		{
			m_remote.setHeight(height);
			cacheValid = false;
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
		}
	}

	public void setPhi(double phi)
	{
		try
		{
			m_remote.setPhi(phi);
			cacheValid = false;
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
		}
	}
	private void setCache()
	{
		try
		{
			cacheValid = true;
			info = m_remote.getInfo();
		}
		catch (RemoteException x)
		{
			System.err.println(x); // BUG
		}
	}
	private boolean cacheValid = false;
	RemoteSliceInfo info;
	private RemoteSliceParameters m_remote;
}

 

Ads help maintain this website.