KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > util > DjLikeComparator


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.genimen.djeneric.util;
32
33 public class DjLikeComparator
34 {
35
36   private char _likeChars[];
37   private int[] _typeOfChar;
38   private int _patternLength;
39   private boolean _ignorecase;
40
41   public DjLikeComparator(String JavaDoc pattern, char escapeChar, boolean ignorecase)
42   {
43     if (ignorecase)
44     {
45       pattern = pattern.toUpperCase();
46     }
47
48     normalize(pattern, true, escapeChar);
49     _ignorecase = ignorecase;
50   }
51
52   public boolean compare(String JavaDoc value)
53   {
54     if (value == null)
55     {
56       return _patternLength == 0;
57     }
58
59     if (_ignorecase)
60     {
61       value = value.toUpperCase();
62     }
63
64     return compareAt(value, 0, 0, value.length());
65   }
66
67   private boolean compareAt(String JavaDoc value, int patternOffset, int valueOffset, int uptoHere)
68   {
69
70     for (; patternOffset < _patternLength; patternOffset++)
71     {
72       switch (_typeOfChar[patternOffset])
73       {
74
75         case 0 : // general character
76
if ((valueOffset >= uptoHere) || (_likeChars[patternOffset] != value.charAt(valueOffset++)))
77           {
78             return false;
79           }
80           break;
81
82         case 1 : // underscore: do not test this character
83
if (valueOffset++ >= uptoHere)
84           {
85             return false;
86           }
87           break;
88
89         case 2 : // percent: none or any character(s)
90
if (++patternOffset >= _patternLength)
91           {
92             return true;
93           }
94
95           while (valueOffset < uptoHere)
96           {
97             if ((_likeChars[patternOffset] == value.charAt(valueOffset))
98                 && compareAt(value, patternOffset, valueOffset, uptoHere))
99             {
100               return true;
101             }
102
103             valueOffset++;
104           }
105
106           return false;
107       }
108     }
109
110     if (valueOffset != uptoHere)
111     {
112       return false;
113     }
114
115     return true;
116   }
117
118   private void normalize(String JavaDoc value, boolean supportEscaping, char escapeChar)
119   {
120
121     _patternLength = 0;
122
123     if (value == null)
124     {
125       return;
126     }
127
128     int length = value.length();
129
130     _likeChars = new char[length];
131     _typeOfChar = new int[length];
132
133     boolean isEscaping = false, isPercented = false;
134
135     for (int i = 0; i < length; i++)
136     {
137       char c = value.charAt(i);
138
139       if (isEscaping == false)
140       {
141         if (supportEscaping && (c == escapeChar))
142         {
143           isEscaping = true;
144
145           continue;
146         }
147         else if (c == '_')
148         {
149           _typeOfChar[_patternLength] = 1;
150         }
151         else if (c == '%')
152         {
153           if (isPercented)
154           {
155             continue;
156           }
157
158           isPercented = true;
159           _typeOfChar[_patternLength] = 2;
160         }
161         else
162         {
163           isPercented = false;
164         }
165       }
166       else
167       {
168         isPercented = false;
169         isEscaping = false;
170       }
171
172       _likeChars[_patternLength++] = c;
173     }
174
175     for (int i = 0; i < _patternLength - 1; i++)
176     {
177       if ((_typeOfChar[i] == 2) && (_typeOfChar[i + 1] == 1))
178       {
179         _typeOfChar[i] = 1;
180         _typeOfChar[i + 1] = 2;
181       }
182     }
183   }
184 }
Popular Tags