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  import java.util.Comparator;
21  
22  import com.healthmarketscience.jackcess.RowId;
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 static final Comparator<RowIdImpl> COMPARATOR =
66      Comparator.comparing(RowIdImpl::getType)
67        .thenComparing(RowIdImpl::getPageNumber)
68        .thenComparing(RowIdImpl::getRowNumber);
69  
70    private final int _pageNumber;
71    private final int _rowNumber;
72    private final Type _type;
73  
74    /**
75     * Creates a new <code>RowId</code> instance.
76     *
77     */
78    public RowIdImpl(int pageNumber,int rowNumber) {
79      _pageNumber = pageNumber;
80      _rowNumber = rowNumber;
81      _type = ((_pageNumber == FIRST_PAGE_NUMBER) ? Type.ALWAYS_FIRST :
82               ((_pageNumber == LAST_PAGE_NUMBER) ? Type.ALWAYS_LAST :
83                Type.NORMAL));
84    }
85  
86    public int getPageNumber() {
87      return _pageNumber;
88    }
89  
90    public int getRowNumber() {
91      return _rowNumber;
92    }
93  
94    /**
95     * Returns {@code true} if this rowId potentially represents an actual row
96     * of data, {@code false} otherwise.
97     */
98    public boolean isValid() {
99      return((getRowNumber() >= 0) && (getPageNumber() >= 0));
100   }
101 
102   public Type getType() {
103     return _type;
104   }
105 
106   @Override
107   public int compareTo(RowId other) {
108     return compareTo((RowIdImpl)other);
109   }
110 
111   public int compareTo(RowIdImpl other) {
112     return COMPARATOR.compare(this, other);
113   }
114 
115   @Override
116   public int hashCode() {
117     return getPageNumber() ^ getRowNumber();
118   }
119 
120   @Override
121   public boolean equals(Object o) {
122     return ((this == o) ||
123             ((o != null) && (getClass() == o.getClass()) &&
124              (getPageNumber() == ((RowIdImpl)o).getPageNumber()) &&
125              (getRowNumber() == ((RowIdImpl)o).getRowNumber())));
126   }
127 
128   @Override
129   public String toString() {
130     return getPageNumber() + ":" + getRowNumber();
131   }
132 
133 }