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.

RowId.java 3.8KB

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