package com.chinamobile.bcbsp.examples.ml.kmeans;




import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.Writable;

import com.chinamobile.bcbsp.ml.VectorWritable;
import com.chinamobile.bcbsp.ml.math.DoubleVector;

public class CenterData implements Writable {

  private static final Log LOG = LogFactory.getLog(CenterData.class);

  private int centerIndex;
  private DoubleVector newCenter;
  private int incrementCounter;

  public CenterData() {
  }

  public CenterData(int key, DoubleVector value) {
    this.centerIndex = key;
    this.newCenter = value;
  }

  public CenterData(int key, int increment, DoubleVector value) {
    this.centerIndex = key;
    this.incrementCounter = increment;
    this.newCenter = value;
    LOG.info("ljn test : new a Kmeans message." );
  }

  @Override
  public void readFields(DataInput in) throws IOException {
    centerIndex = in.readInt();
    incrementCounter = in.readInt();
    newCenter = VectorWritable.readVector(in);
  }

  @Override
  public void write(DataOutput out) throws IOException {
    out.writeInt(centerIndex);
    out.writeInt(incrementCounter);
    if(newCenter == null){
      LOG.info("ljn test : CenterData write newCenter is null. ");
    }
    VectorWritable.writeVector(newCenter, out);
  }

  public int getCenterIndex() {
    return centerIndex;
  }

  public int getIncrementCounter() {
    return incrementCounter;
  }

  public final int getTag() {
    return centerIndex;
  }

  public final DoubleVector getData() {
    return newCenter;
  }

}
