You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CaseInsensitiveColumnMatcher.java 2.1KB

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