KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > address > MatchCity


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33 package com.icesoft.applications.faces.address;
34
35 import java.util.Iterator JavaDoc;
36 import java.util.TreeMap JavaDoc;
37
38 /**
39  * City-specific matching.
40  */

41 public class MatchCity extends MatchBean {
42
43     public MatchCity(String JavaDoc city) {
44         this.city = city;
45         zipMap = new TreeMap JavaDoc();
46     }
47
48     public MatchCity(String JavaDoc city, String JavaDoc state) {
49         this.city = city;
50         this.state = state;
51         zipMap = new TreeMap JavaDoc();
52     }
53
54     /**
55      * Adds the supplied MatchZip to the zip map.
56      *
57      * @param zipDb the MatchZip to add
58      */

59     public MatchZip addZip(MatchZip zipDb) {
60         MatchZip existingZip = getZip(zipDb.getZip());
61
62         if (existingZip == null) {
63             zipMap.put(zipDb.getZip(), zipDb);
64             return zipDb;
65         } else {
66             return existingZip;
67         }
68     }
69
70     /**
71      * Takes the zip values from the map and puts them in an array.
72      *
73      * @return the array of zip strings
74      */

75     public String JavaDoc[] getZipsAsStrings() {
76
77         Iterator JavaDoc itor = zipMap.keySet().iterator();
78         String JavaDoc zips[] = new String JavaDoc[zipMap.size()];
79         int i = 0;
80
81         while (itor.hasNext()) {
82             zips[i++] = (String JavaDoc) itor.next();
83         }
84         return zips;
85     }
86
87     /**
88      * Determines whether the provided zip matches one in the database.
89      *
90      * @param checkZip the provided zip
91      * @return the status of the existance of the zip
92      */

93     public boolean hasZip(String JavaDoc checkZip) {
94         return getClosestZip(checkZip).isMatch();
95     }
96
97     /**
98      * Check whether the zip matches in the database and modifies the match
99      * flag.
100      *
101      * @param checkZip the zip to lookup
102      * @return the MatchZip object of the provided zip string
103      */

104     public MatchZip getZip(String JavaDoc checkZip) {
105
106         MatchZip matchingZip = (MatchZip) zipMap.get(checkZip);
107
108         if (matchingZip != null) {
109             //exact match
110
matchingZip.setMatch(true);
111         }
112         return matchingZip;
113     }
114
115     /**
116      * Finds the closest zip to the provided string.
117      *
118      * @param checkZip the zip to check
119      * @return the MatchZip object of the closest matching zip
120      */

121     public MatchZip getClosestZip(String JavaDoc checkZip) {
122         checkZip = checkZip.trim();
123         return (MatchZip) getClosestMatch(checkZip, zipMap);
124     }
125 }
Popular Tags