1/*
2 *  SimpleTester.java
3 *  Copyright (C) 2006 Amin Ahmad
4 *
5 *  This program is free software; you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation; either version 2 of the License, or
8 *  any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19package org.ahmadsoft.postal.test;
20
21import java.io.DataInputStream;
22import java.io.File;
23import java.util.Iterator;
24import java.util.List;
25
26import org.ahmadsoft.postal.DiskStrategy;
27import org.ahmadsoft.postal.InMemoryStrategy;
28import org.ahmadsoft.postal.PostalCodeConstants;
29import org.ahmadsoft.postal.PostalCodeEntry;
30import org.ahmadsoft.postal.PostalRetrievalStrategy;
31import org.ahmadsoft.postal.ReadOnceStrategy;
32import org.ahmadsoft.postal.USPostalCodeService;
33import org.ahmadsoft.postal.USPostalCodeService.MatchResult;
34
35public class SimpleTester {
36
37	/**
38	 * @param args
39	 */
40	public static void main(String[] args) throws Exception {
41		if (args.length != 2) {
42			System.err.println("Usage: SimpleTester indexFile dataFile");
43			System.exit(1);
44		}
45
46		System.out.println(
47				"Note: If you are running Java 5 or above, you can improve\n" +
48				"the method timing by replacing calls to currentTimeMillis\n" +
49				"with calls to nanoTime.\n\n"
50				);
51		System.out.println(
52				"*******************************************************\n" +
53				"* Initialization                                      *\n" +
54				"*******************************************************\n"
55		);
56
57		runGC();
58
59		long m1 = usedMemory();
60		long a= System.currentTimeMillis();
61
62		File indexFile = new File(args[0]); //"f:/temp/uspc.index");
63		File dataFile  = new File(args[1]); //"f:/temp/uspc.data");
64
65		PostalRetrievalStrategy retrievalStrategy = null;
66		retrievalStrategy = new DiskStrategy();
67		retrievalStrategy = new InMemoryStrategy();
68		retrievalStrategy = new ReadOnceStrategy();
69		retrievalStrategy.initialize(indexFile, dataFile);
70
71		USPostalCodeService uspcs = new USPostalCodeService();
72		uspcs.initialize(retrievalStrategy);
73
74		long b= System.currentTimeMillis();
75		long m2 = usedMemory();
76		System.out.println();
77		System.out.println("initialization (time): " + (b-a) + " ms");
78		System.out.println("initialization (memory): " + (m2 - m1) + " bytes");
79		System.out.println(
80				"\n" +
81				"*******************************************************\n" +
82				"* Examples                                            *\n" +
83				"*******************************************************\n"
84		);
85
86		boolean validState;
87		List results;
88
89		System.out.println("Match Rancho Park, OK against 90064 with default options.");
90		validState = uspcs.isPostalCodeIn(90064, "OK");
91		System.out.println("  USPostalCodeService.isPostalCodeIn(90064, \"OK\") = " + validState);
92		if (validState) {
93			results = uspcs.match("Rancho Park", 90064);
94			System.out.println("  Found " + results.size() + " results:");
95			for (Iterator i=results.iterator(); i.hasNext();) {
96				MatchResult result = (MatchResult) i.next();
97				System.out.println("    " + result);
98			}
99			System.out.println("    <end>");
100			System.out.println("  Preferred for 90064 is " + uspcs.getActualFor(90064));
101		}
102
103		System.out.println("Match Rancho Park, CA against 90064 with default options.");
104		validState = uspcs.isPostalCodeIn(90064, "CA");
105		System.out.println("  USPostalCodeService.isPostalCodeIn(90064, \"CA\") = " + validState);
106		if (validState) {
107			results = uspcs.match("Rancho Park", 90064);
108			System.out.println("  Found " + results.size() + " results:");
109			for (Iterator i=results.iterator(); i.hasNext();) {
110				MatchResult result = (MatchResult) i.next();
111				System.out.println("    " + result);
112			}
113			System.out.println("    <end>");
114			System.out.println("  Preferred for 90064 is " + uspcs.getActualFor(90064));
115		}
116
117		System.out.println("Match Ranchow Parc, CA against 90064 with default options.");
118		validState = uspcs.isPostalCodeIn(90064, "CA");
119		System.out.println("  USPostalCodeService.isPostalCodeIn(90064, \"CA\") = " + validState);
120		if (validState) {
121			results = uspcs.match("Ranchow Parc", 90064);
122			System.out.println("  Found " + results.size() + " results:");
123			for (Iterator i=results.iterator(); i.hasNext();) {
124				MatchResult result = (MatchResult) i.next();
125				System.out.println("    " + result);
126			}
127			System.out.println("    <end>");
128			System.out.println("  Preferred for 90064 is " + uspcs.getActualFor(90064));
129		}
130
131		System.out.println("Match Los Angeles, CA against 90064 with default options.");
132		validState = uspcs.isPostalCodeIn(90064, "CA");
133		System.out.println("  USPostalCodeService.isPostalCodeIn(90064, \"CA\") = " + validState);
134		if (validState) {
135			results = uspcs.match("Los Angeles", 90064);
136			System.out.println("  Found " + results.size() + " results:");
137			for (Iterator i=results.iterator(); i.hasNext();) {
138				MatchResult result = (MatchResult) i.next();
139				System.out.println("    " + result);
140			}
141			System.out.println("    <end>");
142			System.out.println("  Preferred for 90064 is " + uspcs.getActualFor(90064));
143		}
144
145		System.out.println("Match Urb Montesol, PR against 00623 with default options.");
146		validState = uspcs.isPostalCodeIn(623, "PR");
147		System.out.println("  USPostalCodeService.isPostalCodeIn(00623, \"PR\") = " + validState);
148		if (validState) {
149			results = uspcs.match("Urb Montesol", 623);
150			System.out.println("  Found " + results.size() + " results:");
151			for (Iterator i=results.iterator(); i.hasNext();) {
152				MatchResult result = (MatchResult) i.next();
153				System.out.println("    " + result);
154			}
155			System.out.println("    <end>");
156			System.out.println("  Preferred for 00623 is " + uspcs.getActualFor(623));
157		}
158
159		System.out.println("Match Urb Moraltesol, PR against 00623 with match level of unacceptable.");
160		validState = uspcs.isPostalCodeIn(623, "PR");
161		System.out.println("  USPostalCodeService.isPostalCodeIn(00623, \"PR\") = " + validState);
162		if (validState) {
163			results = uspcs.match("Urb Moraltesol", 623, new USPostalCodeService.MatchOptions(true, true, false, PostalCodeConstants.CITY_UNACCEPTABLE));
164			System.out.println("  Found " + results.size() + " results:");
165			for (Iterator i=results.iterator(); i.hasNext();) {
166				MatchResult result = (MatchResult) i.next();
167				System.out.println("    " + result);
168			}
169			System.out.println("    <end>");
170			System.out.println("  Preferred for 00623 is " + uspcs.getActualFor(623));
171		}
172
173
174		System.out.println(
175				"\n" +
176				"*******************************************************\n" +
177				"* Interactive (-1 Quits)                              *\n" +
178				"*******************************************************\n"
179		);
180
181		DataInputStream in2 = new DataInputStream(System.in);
182
183		int input=-1;
184		do {
185			System.out.print("Enter postal code: ");
186			input = Integer.parseInt(in2.readLine());
187
188			long t1 = System.currentTimeMillis();
189			List pcs = uspcs.getCandidates(input);
190			long t2 = System.currentTimeMillis();
191			for (Iterator i=pcs.iterator(); i.hasNext();) {
192				PostalCodeEntry z = (PostalCodeEntry) i.next();
193				System.out.println(z.getCity() + ", " + z.getState() + ", " + PostalCodeConstants.toString(z.getEntryType()));
194			}
195			System.out.println("lookup (time): " + (t2-t1) + " ms");
196		} while (input != -1);
197
198		uspcs.dispose();
199		retrievalStrategy.dispose();
200
201	}
202
203
204
205
206
207
208
209	private static void runGC () throws Exception
210    {
211        // It helps to call Runtime.gc()
212        // using several method calls:
213        for (int r = 0; r < 4; ++ r) _runGC ();
214    }
215
216    private static void _runGC () throws Exception
217    {
218        long usedMem1 = usedMemory (), usedMem2 = Long.MAX_VALUE;
219        for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++ i)
220        {
221            s_runtime.runFinalization ();
222            s_runtime.gc ();
223            Thread.currentThread ().yield ();
224
225            usedMem2 = usedMem1;
226            usedMem1 = usedMemory ();
227        }
228    }
229
230    private static long usedMemory ()
231    {
232        return s_runtime.totalMemory () - s_runtime.freeMemory ();
233    }
234
235    private static final Runtime s_runtime = Runtime.getRuntime ();
236}
237
238