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.

SimpleColumnMatcher.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 java.util.Arrays;
  16. import java.util.Objects;
  17. import com.healthmarketscience.jackcess.DataType;
  18. import com.healthmarketscience.jackcess.Table;
  19. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  20. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  21. /**
  22. * Simple concrete implementation of ColumnMatcher which tests for equality.
  23. * If initial comparison fails, attempts to coerce the values to a common type
  24. * for comparison.
  25. *
  26. * @author James Ahlborn
  27. * @usage _general_class_
  28. */
  29. public class SimpleColumnMatcher implements ColumnMatcher {
  30. public static final SimpleColumnMatcher INSTANCE = new SimpleColumnMatcher();
  31. @Override
  32. public boolean matches(Table table, String columnName, Object value1,
  33. Object value2)
  34. {
  35. if(equals(value1, value2)) {
  36. return true;
  37. }
  38. if((value1 != null) && (value2 != null) &&
  39. (value1.getClass() != value2.getClass())) {
  40. // the values aren't the same type, try coercing them to "internal"
  41. // values and try again
  42. DataType dataType = table.getColumn(columnName).getType();
  43. try {
  44. DatabaseImpl db = (DatabaseImpl)table.getDatabase();
  45. Object internalV1 = ColumnImpl.toInternalValue(dataType, value1, db);
  46. Object internalV2 = ColumnImpl.toInternalValue(dataType, value2, db);
  47. return equals(internalV1, internalV2);
  48. } catch(IOException e) {
  49. // ignored, just go with the original result
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * Returns {@code true} if the two objects are equal, handling {@code null}
  56. * and objects of type {@code byte[]}.
  57. */
  58. private static boolean equals(Object o1, Object o2)
  59. {
  60. return (Objects.equals(o1, o2) ||
  61. ((o1 instanceof byte[]) && (o2 instanceof byte[]) &&
  62. Arrays.equals((byte[])o1, (byte[])o2)));
  63. }
  64. }