2016-09-15 11:08:27 +00:00
package com.verictas.pos.simulator.dataWriter ;
2016-09-15 14:32:14 +00:00
import com.verictas.pos.simulator.Object ;
2016-09-15 12:01:03 +00:00
import com.verictas.pos.simulator.SimulatorConfig ;
2016-09-15 11:08:27 +00:00
import com.verictas.pos.simulator.mathUtils.AU ;
import javax.vecmath.Vector3d ;
import java.io.File ;
import java.io.FileWriter ;
import java.io.IOException ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
public class DataWriter {
private FileWriter writer = null ;
2016-09-15 12:10:06 +00:00
/ * *
* Set global variables , such as the delimiter and the new line character
* /
2016-09-15 11:08:27 +00:00
private static final String DELIMITER = " \ t " ;
private static final String NEW_LINE = " \ n " ;
2016-09-15 12:01:03 +00:00
private int counter = 0 ;
2016-09-15 12:10:06 +00:00
/ * *
* Constructor
* @throws WritingException
* /
2016-09-15 11:08:27 +00:00
public DataWriter ( ) throws WritingException {
try {
2016-09-15 12:10:06 +00:00
/ * *
* Define the save path
* /
2016-09-15 11:08:27 +00:00
String directory = System . getProperty ( " user.home " ) + File . separator + " simulatorExports " ;
File directoryPath = new File ( directory ) ;
String path = directory + File . separator + getCurrentTimeStamp ( ) + " .txt " ;
System . out . println ( " WRITING DATA TO: " + path ) ;
2016-09-15 12:10:06 +00:00
/ * *
* Check if the saving directory exists to prevent IOException
* /
2016-09-15 11:08:27 +00:00
if ( ! directoryPath . exists ( ) ) {
directoryPath . mkdirs ( ) ;
}
2016-09-15 12:10:06 +00:00
/ * *
* Open a file to write to and write the header
* /
2016-09-15 11:08:27 +00:00
this . writer = new FileWriter ( path ) ;
2016-09-15 14:32:14 +00:00
this . writer . write ( " Object " + DELIMITER + " Position (m) " + DELIMITER + " Position (AU) " + DELIMITER + " Distance from the sun (m) " + DELIMITER + " Speed (m/s) " + DELIMITER + " Speed (AU/day) " + DELIMITER + " Old Acceleration " + DELIMITER + " Acceleration " + DELIMITER + " Mass " + NEW_LINE ) ;
2016-09-15 12:01:03 +00:00
this . counter + + ;
2016-09-15 11:08:27 +00:00
} catch ( IOException e ) {
2016-09-15 12:01:03 +00:00
throw new WritingException ( " The destination file couldn't be created. " ) ;
2016-09-15 11:08:27 +00:00
} catch ( Exception e ) {
2016-09-15 12:01:03 +00:00
throw new WritingException ( " Some unknown error occurred while writing to the file! " ) ;
2016-09-15 11:08:27 +00:00
}
}
2016-09-15 12:10:06 +00:00
/ * *
* Writes a string to the file
* @param string
* @throws WritingException
* /
2016-09-15 11:08:27 +00:00
public void write ( String string ) throws WritingException {
if ( this . writer = = null ) {
throw new WritingException ( " The writer isn't defined yet " ) ;
} else {
try {
2016-09-15 12:01:03 +00:00
if ( this . counter % SimulatorConfig . skipLines = = 0 ) {
this . writer . append ( string ) ;
}
this . counter + + ;
2016-09-15 11:08:27 +00:00
} catch ( Exception e ) {
2016-09-15 12:01:03 +00:00
e . printStackTrace ( ) ;
throw new WritingException ( " An error occurred while writing to the file! " ) ;
2016-09-15 11:08:27 +00:00
}
}
}
2016-09-15 12:10:06 +00:00
/ * *
2016-09-15 14:32:14 +00:00
*
* @param object The object you want to write data about
* @param reference The system ' s star
2016-09-15 12:10:06 +00:00
* @throws WritingException
* /
2016-09-15 14:32:14 +00:00
public void write ( Object object , Object reference ) throws WritingException {
String id = object . name ;
Vector3d position = object . position ;
Vector3d speed = object . speed ;
Vector3d oldAcceleration = object . oldAcceleration ;
Vector3d acceleration = object . acceleration ;
double mass = object . mass ;
2016-09-15 11:08:27 +00:00
if ( this . writer = = null ) {
throw new WritingException ( " The writer isn't defined yet " ) ;
} else {
try {
2016-09-15 12:01:03 +00:00
if ( this . counter % SimulatorConfig . skipLines = = 0 ) {
2016-09-15 14:32:14 +00:00
// Calculate the distance to the sun
double sunDistance = object . getDistance ( reference ) . length ( ) ;
this . writer . append ( id + DELIMITER + position . toString ( ) + DELIMITER + AU . convertFromMeter ( position ) . toString ( ) + DELIMITER + String . valueOf ( sunDistance ) + DELIMITER + speed . toString ( ) + DELIMITER + AU . convertFromMetersPerSecond ( speed ) . toString ( ) + DELIMITER + oldAcceleration . toString ( ) + DELIMITER + acceleration . toString ( ) + DELIMITER + String . valueOf ( mass ) + NEW_LINE ) ;
2016-09-15 12:01:03 +00:00
}
this . counter + + ;
2016-09-15 11:08:27 +00:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2016-09-15 12:01:03 +00:00
throw new WritingException ( " An error occurred while writing to the file! " ) ;
2016-09-15 11:08:27 +00:00
}
}
}
2016-09-15 12:10:06 +00:00
/ * *
* Saves the file to disk
* @throws WritingException
* /
2016-09-15 11:08:27 +00:00
public void save ( ) throws WritingException {
if ( this . writer = = null ) {
throw new WritingException ( " The writer isn't defined yet " ) ;
} else {
try {
this . writer . flush ( ) ;
this . writer . close ( ) ;
} catch ( IOException e ) {
throw new WritingException ( " Whoop! Save error! " ) ;
}
}
}
2016-09-15 12:10:06 +00:00
/ * *
* Gets the current line count
* @return int
* /
2016-09-15 12:01:03 +00:00
public int getLines ( ) {
return this . counter ;
}
2016-09-15 12:10:06 +00:00
/ * *
* Gets the current filestamp for file naming
* @return String
* /
private String getCurrentTimeStamp ( ) {
2016-09-15 11:08:27 +00:00
return new SimpleDateFormat ( " yyyy-MM-dd_HH-mm-ss " ) . format ( new Date ( ) ) ;
}
}