package clustering.kmeans;

import java.util.Vector;

public class Cluster implements Cloneable{
	private String mName;
	private Centroid mCentroid;
	private double mSumSqr;
	private Vector<DataPoint> mDataPoints;
	public Object clone() {
        try {
            return super.clone();
        }
        catch (CloneNotSupportedException e) {
            // This should never happen
            throw new InternalError(e.toString());
        }
    }
	public Cluster(String name) {
		this.mName = name;
		this.mCentroid = null; // will be set by calling setCentroid()
		mDataPoints = new Vector<DataPoint>();
	}

	public void setCentroid(Centroid c) {
		mCentroid = c;
	}

	public Centroid getCentroid() {
		return mCentroid;
	}

	public void addDataPoint(DataPoint dp) { // called from CAInstance
        dp.setCluster(this); // initiates a inner call to calcEuclideanDistance() in DP.
        this.mDataPoints.addElement(dp);
        calcSumOfSquares();
    }

	public void removeDataPoint(DataPoint dp) {
		this.mDataPoints.removeElement(dp);
		calcSumOfSquares();
	}

	public int getNumDataPoints() {
		return this.mDataPoints.size();
	}

	public DataPoint getDataPoint(int pos) {
		return (DataPoint) this.mDataPoints.elementAt(pos);
	}
	//Total EuClidean distances of all points inside this cluster to its centroid
	public void calcSumOfSquares() { // called from Centroid
		int size = this.mDataPoints.size();
		double temp = 0;
		for (int i = 0; i < size; i++) {
			temp = temp
					+ ((DataPoint) this.mDataPoints.elementAt(i))
							.getCurrentEuDt();
		}
		this.mSumSqr = temp;
	}

	public double getSumSqr() {
		return this.mSumSqr;
	}

	public String getName() {
		return this.mName;
	}

	public Vector<DataPoint> getDataPoints() {
		return this.mDataPoints;
	}
}
