PostalRetrievalStrategy retrievalStrategy = null; inMemoryStrategy = new ReadOnceStrategy(); inMemoryStrategy.initialize(indexFile, dataFile); USPostalCodeService uspcs = new USPostalCodeService(); uspcs.initialize(retrievalStrategy);
|
|||
What Is It?JPostal provides a Java API for validating city-state pairs against a U.S. postal code (also known as a Zip Code), as well as up-to-date postal code data in a special format designed to enable fast lookup and low memory consumption. The aim of this project is to cultivate more sophisticated address validation within the Java community, and especially the Java EE community. Visit releases to download the latest version. UsageUsage of the JPostal is straightforward, consisting of a configuration and initialization step followed by an arbitrary number of validations and data storage events, and, finally, a dispose step. Configuration and InitializationConfiguration and initialization consists of the following steps:
Creating and initializing JPostal
PostalRetrievalStrategy retrievalStrategy = null; inMemoryStrategy = new ReadOnceStrategy(); inMemoryStrategy.initialize(indexFile, dataFile); USPostalCodeService uspcs = new USPostalCodeService(); uspcs.initialize(retrievalStrategy); ValidationValidation is a three step process and consists of the following:
The block of code below illustrates proper validation. For many more examples, along with (light) commentary, look at SimpleTester.java, which is included in the distribution. Proper Validation with JPostal
System.out.println("Match Los Angeles, CA against 90064 with default options."); validState = uspcs.isPostalCodeIn(90064, "CA"); System.out.println(" USPostalCodeService.isPostalCodeIn(90064, \"CA\") = " + validState); if (validState) { results = uspcs.match("Los Angeles", 90064); System.out.println(" Found " + results.size() + " results:"); for (MatchResult result: results) { System.out.println(" " + result); } System.out.println(" Data StorageJPostal should also be used to normalize mailing addresses before storage by calling USPostalCodeService.getActualFor in order to return the city officially recognized as "acceptable" by the USPS for a given postal code.DisposalExplicit disposal of both the USPostalCodeService and PostalRetrievalStrategy are required, in that order, whenever your program has finished using them. Note that these methods are not thread-safe in the following sense: if one thread is trying to do a lookup using a service instance while a second is disposing the same service instance, the results of the first thread are not guaranteed to be valid because the dispose call will not block until the lookup completes. This is not an issue for single threaded environments and is unlikely to be an issue for Java EE based applications because the lifecycle contracts of Java EE services (Message Driven Beans, Session Beans, and Servlets) generally preclude such scenarios from occurring, assuming that disposal of the USPostalCodeService coincides with disposal of the containing Java EE service.The block of code below illustrates disposal: Disposing a JPostal Service Instance
uspcs.dispose() retrievalStrategy.dispose(); LicenseJPostal is available for free under the terms of the GNU General Public License. JPostal is also available under a commercial, single-server license for 100 USD. Please contact me for details. FeedbackAs always, I would like you feedback on this tool, so please feel free to send me an email with any comments for improvement or otherwise. User feedback is one metric that helps determine how much time I spend improving a product. |
|||