KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Determines the closest match in the database. Superclass for all of the
40  * Matching classes.
41  */

42 public class Matchable {
43
44     private boolean match = true;
45
46     /**
47      * Determine whether or not there is a match.
48      *
49      * @return the status of match
50      */

51     public boolean isMatch() {
52         return match;
53     }
54
55     /**
56      * Inidcate whether or not there is a match.
57      *
58      * @param match
59      */

60     public void setMatch(boolean match) {
61         this.match = match;
62     }
63
64     /**
65      * Matchable compares a string against a TreeMap of strings to determine the
66      * closest match.
67      *
68      * @param checkMatch The string to compare against
69      * @param matchSet The TreeMap of strings.
70      * @return Matchable
71      */

72     public Matchable getClosestMatch(String JavaDoc checkMatch, TreeMap JavaDoc matchSet) {
73
74         Matchable matchable = (Matchable) matchSet.get(checkMatch);
75         if (matchable != null) {
76             //exact match
77
matchable.setMatch(true);
78             return matchable;
79         } else {
80             //search keys for closest match
81
Iterator JavaDoc itor = matchSet.keySet().iterator();
82             String JavaDoc prevVal = null;
83             String JavaDoc nextVal = null;
84             int thisCompare;
85             int lastCompare;
86
87             while (itor.hasNext()) {
88                 nextVal = (String JavaDoc) (itor.next());
89                 thisCompare = checkMatch.compareTo(nextVal);
90                 if (thisCompare < 0) {
91                     if (prevVal == null) {
92                         //first element is closest
93
matchable = (Matchable) matchSet.get(nextVal);
94                     } else {
95                         //decide if prev or next is closest
96
//need to compare characters in order to decide
97
byte nextByte[] = nextVal.getBytes();
98                         byte prevByte[] = prevVal.getBytes();
99                         byte matchByte[] = checkMatch.getBytes();
100                         lastCompare = 0;
101                         int nextDiff, prevDiff;
102                         try {
103                             //inside try block in case we blow off the end of the string
104
nextDiff = nextByte[lastCompare]
105                                        - matchByte[lastCompare];
106                             prevDiff = matchByte[lastCompare]
107                                        - prevByte[lastCompare];
108                             while (nextDiff == prevDiff) {
109                                 lastCompare++;
110                                 nextDiff = nextByte[lastCompare]
111                                            - matchByte[lastCompare];
112                                 prevDiff = matchByte[lastCompare]
113                                            - prevByte[lastCompare];
114                             }
115                             if (nextDiff > prevDiff) {
116                                 //next is farther, so previous is closer
117
matchable = (Matchable) matchSet
118                                         .get(prevVal);
119                             } else {
120                                 //next must be closer
121
matchable = (Matchable) matchSet
122                                         .get(nextVal);
123                             }
124                             matchable.setMatch(false);
125                             return matchable;
126
127                         } catch (Exception JavaDoc e) {
128                             //blew off the end of one of the strings
129
if (lastCompare >= matchByte.length) {
130                                 //blew off matching string so pick shortest
131
if (prevByte.length <= nextByte.length) {
132                                     matchable = (Matchable) matchSet
133                                             .get(prevVal);
134                                 } else {
135                                     matchable = (Matchable) matchSet
136                                             .get(nextVal);
137                                 }
138                             } else if (lastCompare >= nextByte.length) {
139                                 //blew off nextDiff, so pick previous
140
matchable = (Matchable) matchSet
141                                         .get(prevVal);
142                             } else {
143                                 matchable = (Matchable) matchSet
144                                         .get(nextVal);
145                             }
146                         }
147                     }
148                     matchable.setMatch(false);
149                     return matchable;
150                 }
151                 prevVal = nextVal;
152             }
153             //last result is closest
154
matchable = (Matchable) matchSet.get(nextVal);
155             matchable.setMatch(false);
156             return matchable;
157         }
158     }
159 }
Popular Tags