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.lang3.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/RowIdImpl.html#RowIdImpl">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 RowIdImpll/RowIdImpl.html#RowIdImpl">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   @Override
102   public int compareTo(RowId other) {
103     return compareTo((RowIdImpl)other);
104   }
105   
106   public int compareTo(RowIdImpl other) {
107     return new CompareToBuilder()
108       .append(getType(), other.getType())
109       .append(getPageNumber(), other.getPageNumber())
110       .append(getRowNumber(), other.getRowNumber())
111       .toComparison();
112   }
113 
114   @Override
115   public int hashCode() {
116     return getPageNumber() ^ getRowNumber();
117   }
118 
119   @Override
120   public boolean equals(Object o) {
121     return ((this == o) ||
122             ((o != null) && (getClass() == o.getClass()) &&
123              (getPageNumber() == ((RowIdImpl)o).getPageNumber()) &&
124              (getRowNumber() == ((RowIdImpl)o).getRowNumber())));
125   }
126   
127   @Override
128   public String toString() {
129     return getPageNumber() + ":" + getRowNumber();
130   }
131   
132 }