/* * [Province.java] * * Summary: enum for Canadian prices. Also fact about tax changes in each province. * * Copyright: (c) 2011-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 2011-03-24 initial version */ package com.mindprod.canadiantax; import com.mindprod.common18.BigDate; import com.mindprod.common18.Build; import com.mindprod.common18.EIO; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; /** * enum for Canadian prices. Also fact about tax changes in each province. *

* enum for Canadian provinces. Used to build an array of TaxFactoids for each province sorted with most recent date * first. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-03-24 initial version. * @since 2011-03-24 */ enum Province { // PQ is QC, YK is YT, NF is NL BC( "BC : British Columbia" ), AB( "AB : Alberta" ), SK( "SK : Saskatchewan" ), MB( "MB : Manitoba" ), ON( "ON : Ontario" ), QC( "QC : Qu\u00e9bec" ), PE( "PE : Prince Edward Island" ), NB( "NB : New Brunswick" ), NS( "NS : Nova Scotia" ), NL( "NL : Newfoundland/Labrador" ), YT( "YT : Yukon" ), NT( "NT : NWT" ), NU( "NU : Nunavut" ), OC( "out of country" ); /** * name to use for province in JComboBox */ private final String longName; /** * array of tax changes of this province ordered by date, most recent first. */ private ProvincialTaxFact[] taxFacts = new ProvincialTaxFact[ 0 ]; /** * constructor * * @param longName long name to use in JComboBox */ Province( String longName ) { this.longName = longName; } /** * export compressed binary historical tax info for each province. * Only used in PrepareCanadianTax construction. * * @throws java.io.IOException if can't write cantaxhistory.ser file. */ static void exportTaxResource() throws IOException { // O P E N final FileOutputStream fos = new FileOutputStream( Build.MINDPROD_SOURCE + "/canadiantax/cantaxhistory.ser", false /* append */ ); // no need to compress, since jar is compressed anyway. Will get down to about 500 bytes. final ObjectOutputStream oos = new ObjectOutputStream( fos ); // W R I T E oos.writeObject( ProvincialTaxFact.serialVersionUID ); for ( Province province : Province.values() ) { oos.writeObject( province.taxFacts ); } // C L O S E oos.close(); } /** * import compressed binary historical tax info for each province. */ static void importTaxResource() { try { // O P E N final ObjectInputStream ois = EIO.getObjectInputStream( Province.class.getResourceAsStream( "cantaxhistory.ser" ), 4 * 1024, false ); // R E A D if ( ( Long ) ( ois.readObject() ) != ProvincialTaxFact.serialVersionUID ) { throw new IllegalArgumentException( "Program bug: mismatched versions in Tax history resource" ); } for ( Province province : Province.values() ) { province.taxFacts = ( ProvincialTaxFact[] ) ois.readObject(); } // C L O S E ois.close(); } catch ( Exception e ) { throw new IllegalArgumentException( "Program bug: Unable to load cantaxhistory.ser tax fact resource " ); } } /** * Add a dated tax fact to this province. * Only used in PrepareCanadianTax construction * * @param taxFact info about tax rates in a given province starting on a given date */ void add( ProvincialTaxFact taxFact ) { int length = taxFacts.length; ProvincialTaxFact[] grownTaxFacts = new ProvincialTaxFact[ length + 1 ]; System.arraycopy( taxFacts, 0, grownTaxFacts, 0, length ); grownTaxFacts[ length ] = taxFact; // tack it on the end. Arrays.sort( grownTaxFacts ); // there is no concern for speed here. taxFacts = grownTaxFacts; } /** * find the taxes in that province that were in effect on that date. * * @param date yyyy-mm-dd you want the taxes as of, usually today. * * @return TaxFact for that date. */ ProvincialTaxFact findTaxFactForDate( BigDate date ) { // we want to find the tax rate for this province as it was on that date, // e.g. find a ProvincialTaxFact for this date or earlier. for ( ProvincialTaxFact candidate : taxFacts ) { if ( candidate.getDate().compareTo( date ) <= 0 ) { return candidate; } } throw new IllegalArgumentException( "Missing tax data for date " + date.toString() + " in province " + this .toString() ); } public String toString() { return longName; // name used for JComboBox } }