View Javadoc
1   /*
2   Copyright (c) 2010 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  
16  */
17  
18  package com.healthmarketscience.jackcess.util;
19  
20  import java.io.IOException;
21  
22  import com.healthmarketscience.jackcess.DataType;
23  import com.healthmarketscience.jackcess.RuntimeIOException;
24  import com.healthmarketscience.jackcess.Table;
25  import com.healthmarketscience.jackcess.impl.ColumnImpl;
26  
27  /**
28   * Concrete implementation of ColumnMatcher which tests textual columns
29   * case-insensitively ({@link DataType#TEXT} and {@link DataType#MEMO}), and
30   * all other columns using simple equality.
31   * 
32   * @author James Ahlborn
33   * @usage _general_class_
34   */
35  public class CaseInsensitiveColumnMatcher implements ColumnMatcher {
36  
37    public static final CaseInsensitiveColumnMatcher INSTANCE = 
38      new CaseInsensitiveColumnMatcher();
39    
40  
41    public CaseInsensitiveColumnMatcher() {
42    }
43  
44    public boolean matches(Table table, String columnName, Object value1,
45                           Object value2)
46    {
47      if(!table.getColumn(columnName).getType().isTextual()) {
48        // use simple equality
49        return SimpleColumnMatcher.INSTANCE.matches(table, columnName, 
50                                                    value1, value2);
51      }
52  
53      // convert both values to Strings and compare case-insensitively
54      try {
55        CharSequence cs1 = ColumnImpl.toCharSequence(value1);
56        CharSequence cs2 = ColumnImpl.toCharSequence(value2);
57  
58        return((cs1 == cs2) ||
59               ((cs1 != null) && (cs2 != null) &&
60                cs1.toString().equalsIgnoreCase(cs2.toString())));
61      } catch(IOException e) {
62        throw new RuntimeIOException("Could not read column " + columnName 
63                                     + " value", e);
64      }
65    }
66  
67  }