<file_sep>ProGauge-in-Java
================

The original project in java<file_sep>import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
import com.google.gson.*;
//import com.google.gson.*;
import com.google.gson.JsonElement.*;
import com.google.gson.JsonArray.*;
import com.google.gson.Gson;



/*
 *  file: Tester.java
 *
 * This is a control file to test my code for the Junior Delvoper position at ProGauge.
 *
 * @author <NAME>
 * @see HashMap
 * @see Arrays
 * @see Vector
 * @see Calendar
 * @see Collection
 * @see Iterator
 * 
 ########## DO NOT EDIT ABOVE THIS LINE ########## */


public class Tester
{


/*
 * <p>Function RunProgram
 *  Run Program takes an incoming list of Devices and processes them
 *  to produce an indexed output List.
 *
 *  <p> Notes, This implementation takes in a list of devices. The devices are copied into an Array.
 *      The array is then sorted using Arrays.sort(); The logic for the sort can be found in the 
 *      Device subclasses(NumericalDevice and StateDevice) in the compareTo functions. The data is sorted by timeStamps
 *      simplifying the Accumulate process. The sorted array is then used to replace the data in Vector DeviceList.
 *
 *   <p> The devices are now sorted by their recorded TimeStamp and can now be processed.
 *       The data is processed by indexing the device names. On the first occurrence of a device
 *       The Object device is recorded into a hashmap. The hashmap allows easy tracking of first occurrence 
 *       of each device. In the event of additional occurrences of any given device, the data from the additional 
 *       occurrence is appended to the existing Device object and the additional occurrence is dropped.
 *       Any occurrence of a device that is not dated for the current date, is rejected from the list and ignored.
 *
 *       
 *
 *  <p> Finally the Devices stored in the hashmap (the orginal list has now been been condensed) are copied back into an array where they are
 *      sorted by their Tag_Name. Then the array is passed back as output.
 *
 *  @parm deviceList list of devices to be processed
 *  @return Device[] List of processed devices sorted by their Tag_Name
 */



 public Device[] RunProgram(Vector<Device> deviceList)
  {

       HashMap<String,Device> myMap = new HashMap<String,Device>();
       int inputDeviceCount = deviceList.size() -1;
       Device deviceProcessor = new Device();
       Calendar TimeStamp = new GregorianCalendar();
       String testDate = new String(TimeStamp.get(Calendar.YEAR) + "" + TimeStamp.get(Calendar.MONTH) + "" + TimeStamp.get(Calendar.DAY_OF_MONTH));
       Collection<Device> myViewer;
       Device[] outList;
       int counter = 0;

        outList = new Device[deviceList.size()];
        deviceList.toArray(outList);
        Arrays.sort(outList);
        Arrays.sort(outList);
        counter = 0;
        deviceList.clear();

        while(counter <= inputDeviceCount)
         { deviceList.add(outList[counter]);
           counter++;
         }

    counter = 0;


    while(counter <= inputDeviceCount)
     {
       
        deviceProcessor = deviceList.get(counter);//;inputDeviceCount);
        counter++;//inputDeviceCount--;
         if(deviceProcessor.isCurrent())   
          {
            if(!myMap.containsKey(deviceProcessor.getTagName()))
               {
               myMap.put(deviceProcessor.getTagName(),deviceProcessor);
               }
            else
             {
              Device temp = myMap.get(deviceProcessor.getTagName());
                if(deviceProcessor.equals(myMap.get(deviceProcessor.getTagName())))
               {  
                   temp.Accumulate(deviceProcessor);
                   myMap.remove(deviceProcessor.getTagName());
                   myMap.put(deviceProcessor.getTagName(),temp);
               }
              else
               {
                 System.out.println("type miss match error");//+ deviceProcessor.getType() +"  -->" + temp.getType());
               }
          }
       }

     }

     myViewer =  myMap.values();
     outList = new Device[myMap.size()];
     counter = 0;

     Iterator<Device> iter = myViewer.iterator();
       while (iter.hasNext())
         {
           Object reader = iter.next();
           outList[counter++] = (Device) reader;
         }

    Arrays.sort(outList);
    return outList;
 }



/*
 *  Function Main 
 *  <p> Notes, Generates and stores several Devices into a vector, the vector is then
 *       passed into the RUNPROGRAM function. The purpose of this is to set up conditons
 *       to test this programm.
 *
 *
 *
 */


public static void main (String[] args)
{
     Device test = new Device();
     Vector<Device> myList = new Vector<Device>();
     Device generator = new Device();
     Tester testme =new Tester();
     int counter;
     Device[] outList;
     String testToken;
     Gson mySon = new Gson();
     JsonObject temp = new JsonObject();
     int numOfTags;


     myList.add(generator = new NumericalDevice("Pump2","39.4362",6,9,5,0));
     myList.add(generator = new NumericalDevice("AWT3","510",6,9,5,0));
     myList.add(generator = new NumericalDevice("Exhaust2","1537.164",6,9,5,0));

     myList.add(generator = new StateDevice("Cooler2","ON"     ,6,9,5,0));
     myList.add(generator = new StateDevice("Cooler2","OFF" ,6,9,3,0));
     myList.add(generator = new StateDevice("Cooler2","OFF"   ,6,9,9,1));
     myList.add(generator = new StateDevice("Cooler2","ON",6,9,1,0));

     myList.add(generator = new StateDevice("Cooler1","on"     ,6,1,9,1));
     myList.add(generator = new StateDevice("Heater5","active",6,9,5,0));

     myList.add(generator = new StateDevice("PRS2","FAIL"          ,6,9,5,0));
     myList.add(generator = new StateDevice("PRS1","Active"       ,6,9,6,0));
     myList.add(generator = new StateDevice("PRS1","INACTIVE",6,9,7,0));
     myList.add(generator = new StateDevice("GPS","BakersField",6,9,5,1));


     outList = testme.RunProgram(myList);
     numOfTags = Array.getLength(outList);
     counter = 0;

     while(counter < numOfTags)
       {
        if(outList[counter].getType() == 0)
         temp.addProperty(outList[counter].getTagName(),outList[counter].getFloatData());
        else
         temp.addProperty(outList[counter].getTagName(),outList[counter].getStringData());
       counter++;
       }

      String outputString = mySon.toJson(temp);

      outputString = outputString.replaceAll(",",",\n" + " " );
      outputString = outputString.replaceAll("\\{","\\{\n" +" ");
      outputString = outputString.replaceAll(":",": ");
      outputString = outputString.replaceAll("}",",\n}");

      StringTokenizer output2 = new StringTokenizer(outputString,", ",true);
  
      while(output2.hasMoreTokens())
      {
        testToken = output2.nextToken();
        System.out.print(testToken);
      }


}

}
<file_sep>import java.*;
import java.lang.*;
import java.util.*;
import com.google.gson.*;
import com.google.*;

public class StateDevice extends Device implements Comparable   //implements DeviceInterface
{

private String tagName;
private String stringData;
private int deviceType = 1;

StateDevice(String tag,String data)
{
    tagName = tag;
    Boolean result = false;
    stringData = data;
}


StateDevice(String tag,String data,int month,int day,int hour, int am_pm)
{
     tagName = tag;
     Boolean result = false;
     stringData = data;
     timeStamp.set(Calendar.MONTH,month);
     timeStamp.set(Calendar.DATE,day);
     timeStamp.set(Calendar.HOUR,hour);
     timeStamp.set(Calendar.AM_PM,am_pm);

}

public String getData()
{
  return stringData;
}

public void Accumulate(Device merge)
{
   StateDevice temp = (StateDevice) merge; 
   stringData = (stringData + "\n" + temp.getStringData());

}


public String getStringData()
{
  return stringData.toString();
}

public int compareTo(Object otherDevice)
{
 /*
  If passed object is of type other than Device, throw ClassCastException.
 */
      if(!(otherDevice instanceof Device))
       {
        throw new ClassCastException("Invalid object");
       }

      String cTagName = ((Device) otherDevice).getTagName();

     if(this.getTagName().compareToIgnoreCase(cTagName) !=0)
       {
        return((this.getTagName()).compareToIgnoreCase(cTagName));
       }
     if( ((StateDevice) otherDevice).getAM_PM() - this.getAM_PM()  != 0)
       {
       return( (this.getAM_PM()) -  ((StateDevice) otherDevice).getAM_PM());
       }
  return(this.getHour() - ((StateDevice) otherDevice).getHour()  );
   
}

private int getAM_PM()

{ return this.timeStamp.get(Calendar.AM_PM);}

private int getHour()

{
return timeStamp.get(Calendar.HOUR);
}

public int getDay()
{
   return this.timeStamp.get(Calendar.DATE);
}

public int getMonth()
{
   return this.timeStamp.get(Calendar.MONTH); 
}


public String getTagName()
{
  return tagName.toString();
}

public Boolean equals(Device otherDevice)
{

  if (otherDevice.getType() == this.getType())
    return true;
    return false;
}

public int getType()
{return deviceType;}


public String toString()

{
 return(tagName.toString() +  "   " + stringData.toString());
}

public JsonObject toJson()
{

   Gson mySon = new Gson();

   JsonObject temp = new JsonObject();

  temp.addProperty(tagName,stringData);
  return temp;
   

}



public void SetData()
{}

}
<file_sep>public interface DeviceInterface
{

void GetData();
void SetData();
void Accumulate();


}
