|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.christopherschultz.util.zipcode.ZipCodeRegistry
public class ZipCodeRegistry
A registry of US postal codes.
The methods in this class are not thread safe, so intermixing
calls to add(net.christopherschultz.util.zipcode.ZipCode)
with the various forms of get
should be synchronized. Usually, the registry is initialized once
and then all access is read-only; hence, this class provides no
synchronization of its own.
Constructor Summary | |
---|---|
ZipCodeRegistry()
Creates a ZipCodeRegistry object with no ZipCodes. |
Method Summary | |
---|---|
void |
add(ZipCode zipCode)
Adds a ZipCode to the registry. |
ZipCode |
getClosestZipCode(ZipCode target)
Gets the ZipCode that is closest geographically from the specified ZipCode. |
static ZipCodeRegistry |
getInstance()
Gets the default instance of a ZipCodeRegistry. |
ZipCode |
getNearestMatch(int zipCode)
Gets the ZipCode which is the closest numerically to the specified zip code. |
ZipCode |
getZipCode(int zipCode)
Gets the ZipCode which corresponds to the specified zip code. |
List |
getZipCodesWithinKilometers(ZipCode target,
double km)
Gets a list of ZipCodes that are within the specified number of kilometers. |
List |
getZipCodesWithinMiles(ZipCode target,
double miles)
Gets a list of ZipCodes that are within the specified number of miles. |
static ZipCodeRegistry |
load(String resourceName)
Loads a ZipCodeRegistry from the specified file. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ZipCodeRegistry()
Creates a ZipCodeRegistry object with no ZipCodes.
Method Detail |
---|
public void add(ZipCode zipCode)
Adds a ZipCode to the registry.
zipCode
- The ZipCode to add to the registry.public ZipCode getZipCode(int zipCode)
Gets the ZipCode which corresponds to the specified zip code.
zipCode
- The 5-digit zip code to lookup.
null
if there is no corresponding ZipCode
object for the specified zip code.getNearestMatch(int)
public ZipCode getNearestMatch(int zipCode)
Gets the ZipCode which is the closest numerically to the specified zip code.
Numerically looking up zip codes is rather inaccurate, but if you have a zip code which is not in the registry, this may be the only way to find one.
zipCode
- The 5-digit zip code to lookup.
public ZipCode getClosestZipCode(ZipCode target)
Gets the ZipCode that is closest geographically from the specified ZipCode.
target
- The ZipCode to come closest to.
public List getZipCodesWithinMiles(ZipCode target, double miles)
Gets a list of ZipCodes that are within the specified number of miles.
target
- The ZipCode which should be the center of the search.miles
- The maximum distance (in miles) a ZipCode may be from the
target.
public List getZipCodesWithinKilometers(ZipCode target, double km)
Gets a list of ZipCodes that are within the specified number of kilometers.
target
- The ZipCode which should be the center of the search.km
- The maximum distance (in kilometers) a ZipCode may be from the
target.
public static ZipCodeRegistry getInstance() throws IOException
Gets the default instance of a ZipCodeRegistry.
This ZipCodeRegistry comes with a zip code location file that it will load for itself.
IOException
- If there is a problem loading the zip code registry.public static ZipCodeRegistry load(String resourceName) throws IOException
Loads a ZipCodeRegistry from the specified file.
The file should be text with each line like this:
byte 0123456789 123456789 12345 content [ZIP][LATITUDE][LONGITUDE]
...where [ZIP] is the 5-digit zip-code and [LATITUDE] and [LONGITUDE] are the latitude and longitude, respectively, of the post office serving that zip code.
The latitude and longitude may be of arbitrary precision, and may begin with a - or + sign. Note that there is no whitespace of any kind necessary. Whitespace must be used to pad any missing digits since the byte count is significant.
Any lines of text starting with a hash mark (#) will be ignored.
resourceName
- The name of the resource containing the
properly serialized form of the ZipCodeRegistry (see above).
IOException
- If there is a problem reading the resource.
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |