/* * [ActivityItem.java] * * Summary: Describes an activity that uses electricity. * * 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-02 initial version. * 1.1 2010-12-06 add duty cycle. */ package com.mindprod.powcost; import com.mindprod.common18.Misc; import com.mindprod.common18.ST; import java.io.Serializable; import java.text.DecimalFormat; /** * Describes an activity that uses electricity. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2010-12-06 add duty cycle. * @since 2010-12-02 */ public class ActivityItem implements Serializable, Comparable { /** * Defining a layout version for a class. Watch the spelling and keywords! */ private static final long serialVersionUID = 2L; private static final String[] unitNames = { "second", "minute", "hour", "day", "week", "month", "year", "decade" }; /** * format for display and input of total cost */ private static final DecimalFormat TWO_PLACES = new DecimalFormat( "###,##0.00" ); /** * how many seconds in a second, minute... year */ private static final double[] secondsInUnit = { 1, 60, /* min */ 60 * 60, /* hour */ 60 * 60 * 24, /* day */ 60 * 60 * 24 * 7, /* week */ 60 * 60 * 24 * 30.4375, /* month */ 60 * 60 * 24 * 365.25, /* year */ 60 * 60 * 24 * 365.25 * 10 /* decade */ }; /** * description of the activity that consumes electricity */ private final String activityDescription; /** * percent of time the device runs */ private final double dutyCycle; /** * how many seconds this activity takes */ private final double seconds; /** * how many watts this activity consumes */ private final double watts; /** * @param watts how many watts the activity uses. * @param dutyCycle percent of time device runs. * @param duration duration of the activity in the given units. * @param units second, seconds, minutes, hours, days, weeks, months, years * @param activityDescription what the activity using electricity entails. */ public ActivityItem( final double watts, final double dutyCycle, final double duration, final String units, final String activityDescription ) { this.watts = watts; assert 1 <= dutyCycle && dutyCycle <= 100 : "duty cycle out of range"; this.dutyCycle = dutyCycle; this.activityDescription = activityDescription; // convert duration to seconds final String unit = ST.trimTrailing( units, 's' ); boolean found = false; int seconds = -1; for ( int i = 0; i < secondsInUnit.length; i++ ) { if ( unit.equals( unitNames[ i ] ) ) { seconds = ( int ) ( duration * secondsInUnit[ i ] ); found = true; break; } } if ( !found ) { throw new IllegalArgumentException( "unrecognised unit: " + units ); } this.seconds = seconds; } /** * sort activities by ascending energy cost. * Defines default the sort order for ActivityItem Objects. * Compare this ActivityItem with another ActivityItem * Compares energy numerically. * Informally, returns (this-other) or +ve if this sorts after other. *

* Summary: sort activities by ascending energy cost. *

* Requires: JDK 1.8+. *

* Java code generated with: Canadian Mind Products ComparatorCutter. *

* Version History: * 1.0 2013-01-19 - initial release * * @param other other ActivityItem to compare with this one * * @return +ve if this>other, 0 if this==other, -ve if this<other */ public final int compareTo( ActivityItem other ) { double joules = watts * dutyCycle / 100 * seconds; double otherJoules = other.watts * other.dutyCycle / 100 * other.seconds; return Misc.compare( joules, otherJoules ); } /** * get English description of activity that uses electrical energy. * * @return description */ public String getActivityDescription() { return activityDescription; } /** * display the number of seconds in integral units of seconds, minutes.. etc. * * @return string of form "nn minutes" or "1 day". */ public String getDuration() { for ( int i = 0; i < secondsInUnit.length; i++ ) { if ( secondsInUnit[ i ] <= seconds && seconds < secondsInUnit[ i + 1 ] ) { final double duration = seconds / secondsInUnit[ i ]; // two places, but trim .00 */ final String durationString = ST.chopTrailingString( TWO_PLACES.format( duration ), ".00" ); return durationString + " " + unitNames[ i ] + ( duration != 1 ? "s" : "" ); } } throw new IllegalArgumentException( "malformed duration: " + seconds + " seconds" ); } /** * duty cycle percent 0 to 100. Percent time device is on. * * @return 0 to 100. */ public double getDutyCycle() { return dutyCycle; } /** * how many seconds this activity takes. * * @return seconds as an int, not long. */ public double getSeconds() { return seconds; } /** * how many watts this activity consumes * * @return watts as an int. */ public double getWatts() { return watts; } /** * for use in JCheckBox , to display the description. */ public String toString() { return activityDescription; } }