KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > PartialSearcher


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.util;
27
28 import org.snipsnap.snip.Snip;
29
30 import java.util.*;
31
32 /**
33  * Implements a partial search wrapper around a hashmap
34  *
35  * This file is based on code from Jack Shirazi which
36  * was public domain. Thanks. http://www.javaperformancetuning.com
37  *
38  * credit Jack Shirazi
39  * @author Stephan J. Schmidt
40  * @version $Id: PartialSearcher.java 1256 2003-12-11 13:24:57Z leo $
41  */

42
43 public class PartialSearcher implements Map {
44   private Map hash;
45   private String JavaDoc[] sortedArray;
46
47   public PartialSearcher() {
48     this(HashMap.class);
49   }
50
51    public PartialSearcher(Class JavaDoc klass) {
52      try {
53        hash = (Map) klass.newInstance();
54      } catch (Exception JavaDoc e) {
55        hash = new HashMap();
56      }
57      createSortedArray();
58   }
59
60   public Snip[] match(String JavaDoc s) {
61      return match(s, s + '\uFFFF');
62   }
63
64   public Snip[] match(String JavaDoc start, String JavaDoc end) {
65     int startIdx = binarySearch(sortedArray, start, 0, sortedArray.length - 1);
66     int endIdx = binarySearch(sortedArray, end, 0, sortedArray.length - 1);
67
68     Snip[] objs = new Snip[endIdx - startIdx];
69     for (int i = startIdx; i < endIdx; i++) {
70       objs[i - startIdx] = (Snip) hash.get(sortedArray[i]);
71     }
72     return objs;
73   }
74
75   public void createSortedArray() {
76     sortedArray = new String JavaDoc[hash.size()];
77     // @replace with toArray?
78
Iterator iterator = hash.keySet().iterator();
79     for (int i = 0; iterator.hasNext(); i++) {
80       sortedArray[i] = (String JavaDoc) iterator.next();
81     }
82     quicksort(sortedArray, 0, sortedArray.length - 1);
83   }
84
85   public static int binarySearch(String JavaDoc[] arr, String JavaDoc elem, int fromIndex, int toIndex) {
86     int mid,cmp;
87     while (fromIndex <= toIndex) {
88      mid = (fromIndex + toIndex) / 2;
89       if ((cmp = arr[mid].compareTo(elem)) < 0) {
90         fromIndex = mid + 1;
91       } else if (cmp > 0) {
92         toIndex = mid - 1;
93       } else {
94         return mid;
95       }
96     }
97     return fromIndex;
98   }
99
100   public void quicksort(String JavaDoc[] arr, int lo, int hi) {
101     if (lo >= hi) {
102       return;
103     }
104
105     int mid = (lo + hi) / 2;
106     String JavaDoc tmp;
107     String JavaDoc middle = arr[mid];
108
109     if (arr[lo].compareTo(middle) > 0) {
110       arr[mid] = arr[lo];
111       arr[lo] = middle;
112       middle = arr[mid];
113     }
114
115     if (middle.compareTo(arr[hi]) > 0) {
116       arr[mid] = arr[hi];
117       arr[hi] = middle;
118       middle = arr[mid];
119
120       if (arr[lo].compareTo(middle) > 0) {
121         arr[mid] = arr[lo];
122         arr[lo] = middle;
123         middle = arr[mid];
124       }
125     }
126
127     int left = lo + 1;
128     int right = hi - 1;
129
130     if (left >= right) {
131       return;
132     }
133
134     for (; ;) {
135       while (arr[right].compareTo(middle) > 0) {
136         right--;
137       }
138
139       while (left < right && arr[left].compareTo(middle) <= 0) {
140         left++;
141       }
142
143       if (left < right) {
144         tmp = arr[left];
145         arr[left] = arr[right];
146         arr[right] = tmp;
147         right--;
148       } else {
149         break;
150       }
151     }
152
153     quicksort(arr, lo, left);
154     quicksort(arr, left + 1, hi);
155   }
156
157   public int size() {
158     return hash.size();
159   }
160
161   public boolean isEmpty() {
162     return hash.isEmpty();
163   }
164
165   public boolean containsKey(Object JavaDoc o) {
166     return hash.containsKey(o);
167   }
168
169   public boolean containsValue(Object JavaDoc o) {
170     return hash.containsValue(o);
171   }
172
173   public Object JavaDoc get(Object JavaDoc o) {
174     return hash.get(o);
175   }
176
177   public Object JavaDoc put(Object JavaDoc o, Object JavaDoc o1) {
178     Object JavaDoc object = hash.put(o,o1);
179     createSortedArray();
180     return object;
181   }
182
183   public Object JavaDoc remove(Object JavaDoc o) {
184     Object JavaDoc object = hash.remove(o);
185     createSortedArray();
186     return object;
187   }
188
189   public void putAll(Map map) {
190     hash.putAll(map);
191     createSortedArray();
192   }
193
194   public void clear() {
195     hash.clear();
196     createSortedArray();
197   }
198
199   public Set keySet() {
200     return hash.keySet();
201   }
202
203   public Collection values() {
204     return hash.values();
205   }
206
207   public Set entrySet() {
208     return hash.entrySet();
209   }
210 }
211
Popular Tags