package normalize;


/**
*
* @author gordon
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import java.io.*;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.io.*;
import org.apache.hadoop.util.*;

/**
*
* @author Gordon S. Linoff (gordon@data-miners.com)
* December 2009
*/
public class GenericRecord implements Writable {
	private GenericRecordMetadata metadata;
	private String [] values;
	
	public GenericRecord (GenericRecordMetadata grm) {
		metadata = grm;
		values = new String[metadata.numColumns];
		for (int i = 0; i < metadata.numColumns; i++) {
			values[i] = new String("");
		}
	}  // GenericRecord()
	
	public GenericRecord () {
	}  // GenericRecord()
	
	
	public void _setMetadata (GenericRecordMetadata metadata) {
		this.metadata = metadata;
	}  // _setMetadata()
	
	
	public void init () {
		for (int i = 0; i < values.length; i++) {
			values[i] = "";
		}
	}  // init()
	
	public  GenericRecordMetadata getMetadata () {
		return metadata;
	}  // getMetadata()

   public String get (int colnum) throws ArrayIndexOutOfBoundsException  {
	   return this.values[colnum];
   }  // get()
   
   public String get (String colname) throws ArrayIndexOutOfBoundsException  {
	   return this.get(metadata.getOffset(colname));
   }  // get()
   
   public void set (String [] colvals) throws ArrayIndexOutOfBoundsException {
	   for (int i = 0; i < values.length; i++) {
		   this.values[i] = colvals[i];
	   }
   }  // set()
   
   public void set (int colnum, String val) throws ArrayIndexOutOfBoundsException {
	   this.values[colnum] = val;
   }
   
   public void set (String colname, String val) throws ArrayIndexOutOfBoundsException {
	   this.values[metadata.getOffset(colname)] = val;
   }  // set()
   
   public void write(DataOutput out) throws IOException {
	   out.writeInt(values.length);
	   for (int i = 0; i < values.length; i++) {
		   out.writeUTF(this.values[i]);
	   }
   }  // write()

   public void readFields(DataInput in) throws IOException {
	   int numvals = in.readInt();
	   this.values = new String[numvals];
	   for (int i = 0; i < numvals; i++) {
		   this.values[i] = in.readUTF();
	   }
   }  // readFields()
   
   public String toString () {
	   StringBuffer sb = new StringBuffer();
	   boolean first = true;
	   for (int i = 0; i < metadata.numColumns; i++) {
		   sb.append((! first ? "\t" : "") + values[i]);
		   first = false;
		   
	   }
	   /*
	   for (Map.Entry<String, Integer> kv : this.metadata.entrySet()) {
		   sb.append((! first ? "\t" : "") + values[kv.getValue()]);
		   first = false;
	   } */
	   return(sb.toString());
	   
   }  // toString()

   public String toString(boolean verbose) {
	   if (verbose) {
		   StringBuffer sb = new StringBuffer();
		   sb.append("Metadata="+metadata.metadataName+"; ");
		   for (Map.Entry<String, Integer> kv : this.metadata.entrySet()) {
			   sb.append(kv.getKey() + "=["+values[kv.getValue()]+"]; ");
		   }
		   return(sb.toString());
	   }
	   else return toString();
   }  // toString()
}  // class ZipCensus


