KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > booleanvalueexpression > predicates > LikeComparator


1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.*;
4 import com.daffodilwoods.daffodildb.utils.*;
5 import com.daffodilwoods.daffodildb.utils.comparator.*;
6 import com.daffodilwoods.daffodildb.utils.field.*;
7 import com.daffodilwoods.database.resource.*;
8
9 /**
10  * <p>Title: </p>
11  * <p>Description: </p>
12  * <p>Copyright: Copyright (c) 2003</p>
13  * <p>Company: </p>
14  * @author unascribed
15  * @version 1.0
16  */

17
18 public class LikeComparator extends SuperComparator {
19
20    private boolean firstClob, secondClob;
21
22    public LikeComparator(boolean firstClob0, boolean secondClob0) {
23       firstClob = firstClob0;
24       secondClob = secondClob0;
25    }
26
27    public int compare(_DComparator leftMatchValue0, _DComparator rightPattern0) throws DException {
28       byte[] matchValue = firstClob ? ( (DClobUpdatable) leftMatchValue0).getBytes()
29           : ( (FieldBase) leftMatchValue0).getBufferRange().getBytes();
30       byte[] matchPattern = secondClob ? ( (DClobUpdatable) rightPattern0).getBytes()
31           : ( (FieldBase) rightPattern0).getBufferRange().getBytes();
32     int cmp = compareLike(ConvertToLowerCase(matchValue) , ConvertToLowerCase(matchPattern),0,0 );
33       return cmp;
34    }
35
36
37    private byte[] ConvertToLowerCase(byte[] values){
38        for(int i=0;i<values.length;i++){
39          if(values[i]>=65 && values[i]<=91){
40            values[i] = (byte)(values[i]+32);
41            }
42        }
43        return values;
44      }
45
46
47   private void printBytes(byte[] b){
48     for (int i = 0; i < b.length; i++) {
49       System.out.print((char)b[i]);
50     }
51   }
52
53 /* protected int compareLike(byte[] matchValue, byte[] matchPattern) throws DException {
54       int length = matchValue.length > matchPattern.length ? matchValue.length : matchPattern.length;
55       boolean percent = false;
56       int i, j;
57       for (i = 0, j = 0; j < length; i++, j++) {
58          if (j >= matchPattern.length) {
59             break;
60          }
61          switch (matchPattern[j]) {
62             case 95: // _ underscore
63                if (percent) {
64                   if (j + 1 >= matchPattern.length) {
65                      return 0;
66                   }
67                }
68                break;
69             case 37: // % percent
70                if (j + 1 >= matchPattern.length || (j + 1 == matchPattern.length && matchPattern[j + 1] == '_')) {
71                   return 0;
72                } else {
73                   percent = true;
74                   i--;
75                }
76                break;
77             default:
78                if (percent) {
79                   if (i < matchValue.length) {
80                      int[] indexes = getPossibleIndexes(matchValue, i, matchPattern[j]);
81                      for (int a = indexes.length - 1; a >= 0; a--) {
82                         if (compareLike(extractRestOfBytes(indexes[a], matchValue), extractRestOfBytes(j, matchPattern)) == 0) {
83                            return 0;
84                         }
85                      }
86                      return 1;
87                   } else {
88                      return 1;
89                   }
90                }
91                if (i >= matchValue.length || getAppropriateByte(matchValue[i]) != getAppropriateByte(matchPattern[j])) {
92                   return 1;
93                }
94          }
95       }
96       if (i == matchValue.length && j == matchPattern.length) {
97          return 0;
98       } else if (i != matchValue.length) {
99          for (; i < matchValue.length; i++) {
100             if (matchValue[i] != 32) {
101                break;
102             }
103          }
104          if (i == matchValue.length) {
105             return 0;
106          }
107       }
108       return 1;
109    }
110 */

111
112 protected int compareLike(byte[] matchValue, byte[] matchPattern,int i1, int j1) throws DException {
113    int length = matchValue.length > matchPattern.length ? matchValue.length : matchPattern.length;
114    boolean percent = false;
115    int i,j;
116    for (i = i1, j = j1; j < length; i++, j++) {
117       if (j >= matchPattern.length) {
118          break;
119       }
120       switch (matchPattern[j]) {
121          case 95: // _ underscore
122
if (percent) {
123                if (j + 1 >= matchPattern.length) {
124                   return 0;
125                }
126             }
127             break;
128          case 37: // % percent
129
if (j + 1 >= matchPattern.length || (j + 1 == matchPattern.length && matchPattern[j + 1] == '_')) {
130                return 0;
131             } else {
132                percent = true;
133                i--;
134             }
135             break;
136          default:
137             if (percent) {
138                if (i < matchValue.length) {
139                   int[] indexes = getPossibleIndexes(matchValue, i, matchPattern[j]);
140                   for (int a = indexes.length - 1; a >= 0; a--) {
141                      if (compareLike(matchValue, matchPattern,indexes[a],j) == 0) {
142                         return 0;
143                      }
144                   }
145                   return 1;
146                } else {
147                   return 1;
148                }
149             }
150             if (i >= matchValue.length || getAppropriateByte(matchValue[i]) != getAppropriateByte(matchPattern[j])) {
151                return 1;
152             }
153       }
154    }
155    if (i == matchValue.length && j == matchPattern.length) {
156       return 0;
157    } else if (i != matchValue.length) {
158       for (; i < matchValue.length; i++) {
159          if (matchValue[i] != 32) {
160             break;
161          }
162       }
163       if (i == matchValue.length) {
164          return 0;
165       }
166    }
167    return 1;
168 }
169
170
171    private byte getAppropriateByte(byte b) {
172       return b >= 97 && b <= 122 ? (byte) (b - 32) : b;
173    }
174
175    private byte[] extractRestOfBytes(int fromIndex, byte[] sourceArray) throws DException {
176       int length = sourceArray.length - fromIndex;
177       byte[] newArray = new byte[length];
178       for (int i = fromIndex, j = 0; i < sourceArray.length; i++, j++) {
179          newArray[j] = sourceArray[i];
180       }
181       return newArray;
182    }
183
184    private int[] getPossibleIndexes(byte[] source, int index1, byte b) throws DException {
185       int length = 0;
186       int[] array = new int[length];
187       for (; index1 < source.length; index1++) {
188          if (source[index1] == b) {
189             length++;
190             int[] tempArray = new int[length];
191             System.arraycopy(array, 0, tempArray, 0, array.length);
192             tempArray[length - 1] = index1;
193             array = tempArray;
194          }
195       }
196       return array;
197    }
198
199    public String JavaDoc toString() {
200       return "LikeComparator ";
201    }
202
203 }
204
Popular Tags