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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. Copyright (c) 2010 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess;
  17. import java.io.IOException;
  18. /**
  19. * Concrete implementation of ColumnMatcher which tests textual columns
  20. * case-insensitively ({@link DataType#TEXT} and {@link DataType#MEMO}), and
  21. * all other columns using simple equality.
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class CaseInsensitiveColumnMatcher implements ColumnMatcher {
  26. public static final CaseInsensitiveColumnMatcher INSTANCE =
  27. new CaseInsensitiveColumnMatcher();
  28. public CaseInsensitiveColumnMatcher() {
  29. }
  30. public boolean matches(Table table, String columnName, Object value1,
  31. Object value2)
  32. {
  33. if(!table.getColumn(columnName).getType().isTextual()) {
  34. // use simple equality
  35. return SimpleColumnMatcher.INSTANCE.matches(table, columnName,
  36. value1, value2);
  37. }
  38. // convert both values to Strings and compare case-insensitively
  39. try {
  40. CharSequence cs1 = Column.toCharSequence(value1);
  41. CharSequence cs2 = Column.toCharSequence(value2);
  42. return((cs1 == cs2) ||
  43. ((cs1 != null) && (cs2 != null) &&
  44. cs1.toString().equalsIgnoreCase(cs2.toString())));
  45. } catch(IOException e) {
  46. throw new IllegalStateException("Could not read column " + columnName
  47. + " value", e);
  48. }
  49. }
  50. }