001 package ps4; 002 003 /** 004 * A StreetClassification describes a street category. It is an 005 * <i>enumeration</i> type, which you can tell by the class declaration 006 * <tt>public enum StreetClassification</tt>. There are a handful of 007 * values the type may hold, and the set of options is fixed at compile 008 * time. 009 * 010 * <p> 011 * Example uses: 012 * 013 * <pre> 014 * StreetClassification myType = StreetClassification.LOCAL_ROAD; 015 * // ... 016 * if (myType == StreetClassification.UNKNOWN) { 017 * // ... 018 * } 019 * </pre> 020 * 021 * <p> 022 * Notice that you may reference the constant values as you would with any other 023 * static variable, e.g. ClassName.STATIC_FIELD_NAME. Also, you may use the 024 * <code>==</code> operator to check for equality, since there is only one copy 025 * of each object. 026 * 027 * <p> 028 * The ordering given by the <code>compareTo</code> method of this class is 029 * consistent with equals, and gives the following ordering: PRIM_HWY, SEC_HWY, 030 * LOCAL_ROAD, UNKNOWN. 031 */ 032 033 public enum StreetClassification { 034 035 /** 036 * Classification indicating a primary highway. Primary highways include 037 * interstate highways and some toll highways; these highways are accesed by 038 * way of ramps and have multiple lanes of traffic. 039 */ 040 PRIM_HWY("PRIM_HWY", "Primary Highway"), 041 042 /** 043 * Classification indicating a secondary highway. Secondary highways include 044 * state highways and some county highways. 045 */ 046 SEC_HWY("SEC_HWY", "Secondary Highway"), 047 048 /** 049 * Classification indicating a local road. Local roads are for local 050 * traffic. Scenic park roads and unpaved roads are also included in this 051 * category. 052 */ 053 LOCAL_ROAD("LOCAL_ROAD", "Local Road"), 054 055 /** 056 * Classification indicating an unknown type of street. This classificiation 057 * is given to streets that do not fall within one of the other three 058 * categories or to streets for which not enough information is known to 059 * classify them. 060 */ 061 UNKNOWN("UNKNOWN", "Unknown"); 062 063 064 //FIELDS 065 066 /** Name of enum constant */ 067 private final String repr; 068 /** Human-friendly name */ 069 private final String name; 070 071 //CONSTRUCTOR 072 073 StreetClassification(String repr, String name) { 074 this.repr = repr; 075 this.name = name; 076 } 077 078 //OBSERVERS 079 080 /** 081 * @returns a String representation of this 082 */ 083 public String toString() { 084 return "StreetClassification[" + name + "]"; 085 } 086 087 //FACTORY METHOD 088 089 /** 090 * @param in the enum constant name or human-friendly name 091 * @returns a StreetClassification that matches the string passed in 092 */ 093 public static StreetClassification parse(String in) { 094 for (StreetClassification typ : StreetClassification.values()) { 095 if (typ.name.equals(in) || typ.repr.equals(in)) { 096 return typ; 097 } 098 } 099 return UNKNOWN; 100 } 101 102 /** Returns a concise, parseable string representation. */ 103 public String unparse() { 104 return repr; 105 } 106 107 } //StreetClassification