View Javadoc
1   /*
2   Copyright (c) 2007 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl;
18  
19  import java.io.Serializable;
20  
21  import com.healthmarketscience.jackcess.RowId;
22  import org.apache.commons.lang.builder.CompareToBuilder;
23  
24  
25  /**
26   * Uniquely identifies a row of data within the access database.
27   *
28   * @author James Ahlborn
29   */
30  public class RowIdImpl implements RowId, Serializable
31  {
32    private static final long serialVersionUID = 20131014L;  
33  
34    /** special page number which will sort before any other valid page
35        number */
36    public static final int FIRST_PAGE_NUMBER = -1;
37    /** special page number which will sort after any other valid page
38        number */
39    public static final int LAST_PAGE_NUMBER = -2;
40  
41    /** special row number representing an invalid row number */
42    public static final int INVALID_ROW_NUMBER = -1;
43  
44    /** type attributes for RowIds which simplify comparisons */
45    public enum Type {
46      /** comparable type indicating this RowId should always compare less than
47          normal RowIds */
48      ALWAYS_FIRST,
49      /** comparable type indicating this RowId should always compare
50          normally */
51      NORMAL,
52      /** comparable type indicating this RowId should always compare greater
53          than normal RowIds */
54      ALWAYS_LAST;
55    }
56    
57    /** special rowId which will sort before any other valid rowId */
58    public static final RowIdImpl FIRST_ROW_ID = new RowIdImpl(
59        FIRST_PAGE_NUMBER, INVALID_ROW_NUMBER);
60  
61    /** special rowId which will sort after any other valid rowId */
62    public static final RowIdImpl LAST_ROW_ID = new RowIdImpl(
63        LAST_PAGE_NUMBER, INVALID_ROW_NUMBER);
64  
65    private final int _pageNumber;
66    private final int _rowNumber;
67    private final Type _type;
68    
69    /**
70     * Creates a new <code>RowId</code> instance.
71     *
72     */
73    public RowIdImpl(int pageNumber,int rowNumber) {
74      _pageNumber = pageNumber;
75      _rowNumber = rowNumber;
76      _type = ((_pageNumber == FIRST_PAGE_NUMBER) ? Type.ALWAYS_FIRST :
77               ((_pageNumber == LAST_PAGE_NUMBER) ? Type.ALWAYS_LAST :
78                Type.NORMAL));
79    }
80  
81    public int getPageNumber() {
82      return _pageNumber;
83    }
84  
85    public int getRowNumber() {
86      return _rowNumber;
87    }
88  
89    /**
90     * Returns {@code true} if this rowId potentially represents an actual row
91     * of data, {@code false} otherwise.
92     */
93    public boolean isValid() {
94      return((getRowNumber() >= 0) && (getPageNumber() >= 0));
95    }
96  
97    public Type getType() {
98      return _type;
99    }
100 
101   public int compareTo(RowId other) {
102     return compareTo((RowIdImpl)other);
103   }
104   
105   public int compareTo(RowIdImpl other) {
106     return new CompareToBuilder()
107       .append(getType(), other.getType())
108       .append(getPageNumber(), other.getPageNumber())
109       .append(getRowNumber(), other.getRowNumber())
110       .toComparison();
111   }
112 
113   @Override
114   public int hashCode() {
115     return getPageNumber() ^ getRowNumber();
116   }
117 
118   @Override
119   public boolean equals(Object o) {
120     return ((this == o) ||
121             ((o != null) && (getClass() == o.getClass()) &&
122              (getPageNumber() == ((RowIdImpl)o).getPageNumber()) &&
123              (getRowNumber() == ((RowIdImpl)o).getRowNumber())));
124   }
125   
126   @Override
127   public String toString() {
128     return getPageNumber() + ":" + getRowNumber();
129   }
130   
131 }