package thisandthat;

import java.util.ArrayList;
import java.util.Arrays;

import math.Maths;

/**
 * @author Daniel Weschke
 */
public class WObject {
	private static boolean supressErrorMessage = false;
	private String name;
	private ArrayList<Integer> operations = new ArrayList<Integer>();
	private Integer[] convertedArray; 
	private boolean justConverted;

	public String getName() {
		return name;
	}

	public WObject setName(String name) {
		this.name = name;
		return this;
	}
	
	public Integer[] getOperations(){
		if(!justConverted){
			Integer[] itemArray = new Integer[operations.size()];
			convertedArray = operations.toArray(itemArray);
			justConverted = true;
		}
		return convertedArray;
	}
	
	public int getOperationsTotal(){
		return Maths.sum(getOperations());
	}
	
	public void newOperation(){
		operations.add(0);
		justConverted = false;
	}
	
	public void operate(){
		int index = operations.size()-1;
		operations.set(index,operations.get(index)+1);
		justConverted = false;
	}

	public static boolean isSupressErrorMessage() {
		return supressErrorMessage;
	}

	public static void setSupressErrorMessage(boolean supressErrorMessage) {
		WObject.supressErrorMessage = supressErrorMessage;
	}
    
    public static <T> T first(T[] array) {
        return array[0];
    }
    
    public static <T> T nth(T[] array, int n) {
        return array[n];
    }
    
    public static <T> T last(T[] array) {
        return array[array.length - 1];
    }
	
	/**
	 * The declaration of that method is:
	 * public T cast(Object o)
	 * @param o
	 * @param clazz
	 * @return casted object
	 */
	public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) {
	    try {
	        return clazz.cast(o);
	    } catch(ClassCastException e) {
	        return null;
	    }
	}
	
	@Override
	public String toString(){
		return name;
	}
    
    public void println(){
    	System.out.println(this);
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		WObject obj = new WObject();
    	obj.setName("one");
        obj.println();
        
        obj.newOperation();
        System.out.print(Arrays.toString(obj.getOperations()));
        System.out.println(" tot: "+obj.getOperationsTotal());
        obj.operate();
        System.out.print(Arrays.toString(obj.getOperations()));
        System.out.println(" tot: "+obj.getOperationsTotal());
        obj.operate();
        System.out.print(Arrays.toString(obj.getOperations()));
        System.out.println(" tot: "+obj.getOperationsTotal());
        
        obj.newOperation();
        System.out.print(Arrays.toString(obj.getOperations()));
        System.out.println(" tot: "+obj.getOperationsTotal());
        obj.operate();
        System.out.print(Arrays.toString(obj.getOperations()));
        System.out.println(" tot: "+obj.getOperationsTotal());
        obj.operate();
        System.out.print(Arrays.toString(obj.getOperations()));
        System.out.println(" tot: "+obj.getOperationsTotal());
	}

}
