/* * [PrepareResources.java] * * Summary: Prepares activities and currency info for the jar resources. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-12-04 initial version. * 1.1 2010-12-06 add duty cycle. */ package com.mindprod.powcost; import com.mindprod.common18.EIO; import com.mindprod.csv.CSVReader; import com.mindprod.entities.DeEntifyStrings; import java.io.BufferedReader; import java.io.EOFException; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Collections; import static java.lang.System.*; /** * Prepares activities and currency info for the jar resources. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2010-12-06 add duty cycle. * @since 2010-12-02 */ public class PrepareResources { /** * true if want extra debug ouput */ private static final boolean DEBUGGING = false; // methods /** * export activities as a gzipped ser resource * * @throws IOException */ private static void prepareActivities() throws IOException { // must run in com\mindprod\powcost final CSVReader activitiesCSV = new CSVReader( EIO.getBufferedReader( new File( "activities.csv" ), 64 * 1024, EIO.UTF8 ) ); final ArrayList activities = new ArrayList<>( 200 ); try { while ( true ) { // 3000, 100, 1, hour, heat 32 gallons of water electrically to wash a load of clothes. final double watts = activitiesCSV.getDouble(); if ( watts == 0 ) { continue; // ignore blank lines } final double dutyCycle = activitiesCSV.getDouble(); final double duration = activitiesCSV.getDouble(); final String units = activitiesCSV.get(); final String activityDescription = DeEntifyStrings.deEntifyHTML( activitiesCSV.get(), ' ' ); activitiesCSV.skipToNextLine(); activities.add( new ActivityItem( watts, dutyCycle, duration, units, activityDescription ) ); } } catch ( EOFException e ) { activitiesCSV.close(); } if ( DEBUGGING ) { out.println( "pre sort" ); for ( ActivityItem i : activities ) { out.println( i ); } } final int size = activities.size(); Collections.sort( activities ); // sort by ascending energy consumption. if ( DEBUGGING ) { out.println( "post sort" ); for ( ActivityItem i : activities ) { out.println( i ); } } // save so can be included in jar // O P E N ObjectOutputStream oos = EIO.getObjectOutputStream( new File( "activities.ser" ), 64 * 1024, true ); // W R I T E oos.writeObject( activities.toArray( new ActivityItem[ size ] ) ); // C L O S E oos.close(); out.println( size + " activity records written" ); } // /method /** * export electricity costs as a gzipped *.ser resource * * @throws IOException */ private static void prepareCosts() throws IOException { // must run in com\mindprod\powcost final CSVReader costsCSV = new CSVReader( new BufferedReader( new FileReader( "costs.csv" ) ) ); final ArrayList costs = new ArrayList<>( 200 ); try { while ( true ) { // ca, AB, Alberta, 11.30, CAD. final String countryAbbr = costsCSV.get(); if ( countryAbbr == null ) { continue; } final String stateAbbr = costsCSV.get(); final String locationDescription = DeEntifyStrings.deEntifyHTML( costsCSV.get(), ' ' ); final double costPerKwh = costsCSV.getDouble(); /* manually preconverted from cents to dollars */ final String currencyAbbr = costsCSV.get(); costsCSV.skipToNextLine(); costs.add( new CostLocItem( countryAbbr, stateAbbr, locationDescription, costPerKwh, currencyAbbr ) ); } } catch ( EOFException e ) { costsCSV.close(); } final int size = costs.size(); // save so can be included in jar // O P E N final ObjectOutputStream oos = EIO.getObjectOutputStream( new File( "costs.ser" ), 4 * 1024, true ); // W R I T E oos.writeObject( costs.toArray( new CostLocItem[ size ] ) ); // C L O S E oos.close(); out.println( size + " cost records written" ); } // /method /** * prepare activities.ser and costs.ser resources */ public static void main( final String[] args ) { try { prepareActivities(); prepareCosts(); } catch ( IOException e ) { err.println(); e.printStackTrace( err ); err.println(); } } // /method // /methods }