/* * [StateItem.java] * * Summary: Tax information about one US state. * * Copyright: (c) 1999-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 2007-08-07 initial version * 1.1 2010-05-10 now serialised, read from resource. */ package com.mindprod.americantax; import com.mindprod.common18.EIO; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; import java.util.Vector; import static java.lang.System.*; /** * Tax information about one US state. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2010-05-10 now serialised, read from resource. * @since 2007-08-07 */ final class StateItem implements Serializable { /** * average districts per state. Used as initial capacity for Vector. Will be pruned to actual size later */ private static final int AVERAGE_DISTRICTS_PER_STATE = 1000; /** * Defining a layout version for a class. */ private static final long serialVersionUID = 3L; /** * look up by key "WA" to get taxRate, state name "Washington" etc. use a prime size for speed */ private static HashMap stateHash; /** * state item objects in order want to see them on screen. We use Vector because that is what JComboBox wants. */ private static Vector stateVector; /** * List of districts in this state in order want to display them. We use Vector because that is what JComboBox * wants. StateItem object has list of all corresponding districts. */ private final Vector districts = new Vector<>( AVERAGE_DISTRICTS_PER_STATE ); /** * long descriptive name for this state e.g. "Washington" */ private final String fullStateName; /** * 2-char abbreviation for this state, e.g. "WA" */ private final String stateAbbr; /** * percentage state sales tax */ private final int stateTaxRate; /** * constructor * * @param stateAbbr two letter state to define tax for * @param fullStateName human name for the state to define tax for * @param stateTaxPercent tax rate for state, e.g. 7.0 */ StateItem( String stateAbbr, String fullStateName, double stateTaxPercent ) { this.stateAbbr = stateAbbr.intern(); // want compact. this.fullStateName = fullStateName.intern(); this.stateTaxRate = ( int ) ( stateTaxPercent * 10000 + .5 ); } // end constructor /** * get array of state choice to use in dropdown. * * @return Array of possible states */ static Vector getStateChoices() { return stateVector; } /** * get 2-letter abbreviation for this this state. * * @return e.g. "AK" */ String getStateAbbr() { return stateAbbr; } /** * get state tax for this state.. * * @return state tax as a percentage e.g. 7% = 70,000 */ int getStateTaxRate() { return stateTaxRate; } /** * trim Vectors to minimal size */ void trimToSize() { districts.trimToSize(); } /** * look up state by state abbreviation * * @param stateAbbr 2-letter state abbreviation * * @return StateItem object of corresponding state */ public static StateItem findStateItem( String stateAbbr ) { return stateHash.get( stateAbbr ); } /** * Read serialised binary objects from a resource */ public static void load() { try { // O P E N final ObjectInputStream ois = EIO.getObjectInputStream( AmericanTaxTable.class.getResourceAsStream( "taxtables.ser" ), 8096, true ); // R E A D as array final StateItem[] stateItems = ( StateItem[] ) ois.readObject(); // convert to Vector stateVector = new Vector<>( Arrays.asList( stateItems ) ); // convert to lookup table. stateHash = new HashMap<>( stateItems.length + 21 ); for ( StateItem stateItem : stateItems ) { stateHash.put( stateItem.stateAbbr, stateItem ); } // C L O S E ois.close(); } catch ( IOException e ) { err.println( e.getMessage() ); err.println( "Unable to load taxtables.ser resource" ); } catch ( ClassNotFoundException e ) { err.println( e.getMessage() ); err.println( "Unable to load taxtables.ser resource because of missing class" ); } } /** * add District to this state * * @param districtItem District to add. */ public void add( DistrictItem districtItem ) { districts.add( districtItem ); } /** * get an array of Strings representing the districts for the given state 2-letter names, e.g. "AK", "AL", and an * entry for "out of country", suitable for populating a Choice box. * * @return array of tax district objects */ public Vector getDistrictItems() { return districts; } /** * how we display a state on screen */ public String toString() { return stateAbbr + " " + fullStateName; } }