package com.ethon;

import com.ethon.mode.DataPoint;

/**
 * 公共的数据池
 * @author ethonchan	ethonchan@gmail.com
 *
 */
public class DataBase {
	private static DataBase instance=null;
	private static DataPoint[] points;
	
	/*
	 * 数据场的相关常量START
	 */
	//点的坐标的缩放程度=panel的长度/（2*点坐标的较大的边长）
	public static double scale=1.0;
	
	//点的实际坐标的范围
	public static double xMin=Double.MAX_VALUE;
	public static double yMin=Double.MAX_VALUE;
	public static double xMax=Double.MIN_VALUE;
	public static double yMax=Double.MIN_VALUE;

	//初始化坐标轴时坐标轴的边长
	public static int sLen;
	/*
	 * 数据场的相关常量END
	 */
	
	private DataBase(){
		points=null;
	}
	
	public static DataBase getInstance(){
		if(instance==null)	instance=new DataBase();
		
		return instance;
	}
	
	public DataPoint[] getPoints() {
		return points;
	}

	public void setPoints(DataPoint[] points) {
		DataBase.points = points;
	}
	
	public boolean pointsIsNull(){
		if(points==null||points.length==0)	return true;
		else return false;
	}
	
	public void setPoint(int index, DataPoint point){
		points[index]=point;
	}
	
	public DataPoint getPoint(int index){
		return points[index];
	}
	
	public void removePoint(int index){
		int len=points.length;
		if(index<0||index>len-1)	return ;
		DataPoint[] result=new DataPoint[len-1];
		for(int i=0,j=0;i<len;i++){
			if(i==index)	continue;
			result[j]=points[i];
			j++;
		}
		points=result;
	}
	
	public void clear(){
		points=new DataPoint[0];
	}
		
	public DataPoint[] insertPoint(DataPoint point){
		if(points==null){
			points=new DataPoint[1];
			points[0]=point;
		}else{
			int len=points.length;
			DataPoint[] newone=new DataPoint[len+1];
			for(int i=0;i<len;i++){
				newone[i]=points[i];
			}
			newone[len]=point;
			points=newone;
		}
		
		return points;
	}
}
