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.

RowIdImpl.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  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.impl;
  14. import java.io.Serializable;
  15. import com.healthmarketscience.jackcess.RowId;
  16. import org.apache.commons.lang.builder.CompareToBuilder;
  17. /**
  18. * Uniquely identifies a row of data within the access database.
  19. *
  20. * @author James Ahlborn
  21. */
  22. public class RowIdImpl implements RowId, Serializable
  23. {
  24. private static final long serialVersionUID = 20131014L;
  25. /** special page number which will sort before any other valid page
  26. number */
  27. public static final int FIRST_PAGE_NUMBER = -1;
  28. /** special page number which will sort after any other valid page
  29. number */
  30. public static final int LAST_PAGE_NUMBER = -2;
  31. /** special row number representing an invalid row number */
  32. public static final int INVALID_ROW_NUMBER = -1;
  33. /** type attributes for RowIds which simplify comparisons */
  34. public enum Type {
  35. /** comparable type indicating this RowId should always compare less than
  36. normal RowIds */
  37. ALWAYS_FIRST,
  38. /** comparable type indicating this RowId should always compare
  39. normally */
  40. NORMAL,
  41. /** comparable type indicating this RowId should always compare greater
  42. than normal RowIds */
  43. ALWAYS_LAST;
  44. }
  45. /** special rowId which will sort before any other valid rowId */
  46. public static final RowIdImpl FIRST_ROW_ID = new RowIdImpl(
  47. FIRST_PAGE_NUMBER, INVALID_ROW_NUMBER);
  48. /** special rowId which will sort after any other valid rowId */
  49. public static final RowIdImpl LAST_ROW_ID = new RowIdImpl(
  50. LAST_PAGE_NUMBER, INVALID_ROW_NUMBER);
  51. private final int _pageNumber;
  52. private final int _rowNumber;
  53. private final Type _type;
  54. /**
  55. * Creates a new <code>RowId</code> instance.
  56. *
  57. */
  58. public RowIdImpl(int pageNumber,int rowNumber) {
  59. _pageNumber = pageNumber;
  60. _rowNumber = rowNumber;
  61. _type = ((_pageNumber == FIRST_PAGE_NUMBER) ? Type.ALWAYS_FIRST :
  62. ((_pageNumber == LAST_PAGE_NUMBER) ? Type.ALWAYS_LAST :
  63. Type.NORMAL));
  64. }
  65. public int getPageNumber() {
  66. return _pageNumber;
  67. }
  68. public int getRowNumber() {
  69. return _rowNumber;
  70. }
  71. /**
  72. * Returns {@code true} if this rowId potentially represents an actual row
  73. * of data, {@code false} otherwise.
  74. */
  75. public boolean isValid() {
  76. return((getRowNumber() >= 0) && (getPageNumber() >= 0));
  77. }
  78. public Type getType() {
  79. return _type;
  80. }
  81. public int compareTo(RowId other) {
  82. return compareTo((RowIdImpl)other);
  83. }
  84. public int compareTo(RowIdImpl other) {
  85. return new CompareToBuilder()
  86. .append(getType(), other.getType())
  87. .append(getPageNumber(), other.getPageNumber())
  88. .append(getRowNumber(), other.getRowNumber())
  89. .toComparison();
  90. }
  91. @Override
  92. public int hashCode() {
  93. return getPageNumber() ^ getRowNumber();
  94. }
  95. @Override
  96. public boolean equals(Object o) {
  97. return ((this == o) ||
  98. ((o != null) && (getClass() == o.getClass()) &&
  99. (getPageNumber() == ((RowIdImpl)o).getPageNumber()) &&
  100. (getRowNumber() == ((RowIdImpl)o).getRowNumber())));
  101. }
  102. @Override
  103. public String toString() {
  104. return getPageNumber() + ":" + getRowNumber();
  105. }
  106. }