1 /***
2 *
3 * Copyright 2004 Hiram Chirino
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.journal.impl;
19
20 import org.codehaus.activemq.journal.RecordLocation;
21
22 /***
23 * Defines a where a record can be located in the Journal.
24 *
25 * @version $Revision: 1.2 $
26 */
27 public class RecordLocationImpl implements RecordLocation {
28
29 final private byte fileMangerId;
30 final private byte segmentIndex;
31 final private int segmentOffset;
32 final private long sequenceId;
33
34 public RecordLocationImpl(final byte fileMangerId, final byte segmentIndex,
35 final int fileOffset, long sequenceId) {
36 super();
37 this.fileMangerId = fileMangerId;
38 this.segmentIndex = segmentIndex;
39 this.segmentOffset = fileOffset;
40 this.sequenceId = sequenceId;
41 }
42
43 /***
44 * @param b
45 * @param segmentIndex2
46 * @param offset
47 */
48 public RecordLocationImpl(final byte fileMangerId, final byte segmentIndex, final int fileOffset) {
49 super();
50 this.fileMangerId = fileMangerId;
51 this.segmentIndex = segmentIndex;
52 this.segmentOffset = fileOffset;
53 this.sequenceId = -1;
54 }
55
56 /***
57 * @see java.lang.Comparable#compareTo(java.lang.Object)
58 */
59 public int compareTo(Object o) {
60 return (int)(sequenceId - ((RecordLocationImpl)o).sequenceId);
61 }
62
63 /***
64 * @see java.lang.Object#hashCode()
65 */
66 public int hashCode() {
67 return segmentOffset^segmentIndex^(fileMangerId<<8)^((int)sequenceId);
68 }
69
70 /***
71 * @see java.lang.Object#equals(java.lang.Object)
72 */
73 public boolean equals(Object o) {
74 if( o==null || o.getClass()!=RecordLocationImpl.class )
75 return false;
76 RecordLocationImpl rl = (RecordLocationImpl)o;
77 return rl.sequenceId == this.sequenceId && rl.segmentOffset == this.segmentOffset &&
78 rl.segmentIndex==this.segmentIndex && rl.fileMangerId == this.fileMangerId;
79 }
80
81 /***
82 * @see java.lang.Object#toString()
83 */
84 public String toString() {
85 return ""+fileMangerId+":"+segmentIndex+":"+segmentOffset+":"+sequenceId;
86 }
87
88 /***
89 * @return Returns the fileMangerId.
90 */
91 public byte getFileMangerId() {
92 return fileMangerId;
93 }
94 /***
95 * @return Returns the fileId.
96 */
97 public byte getSegmentIndex() {
98 return segmentIndex;
99 }
100 /***
101 * @return Returns the fileOffset.
102 */
103 public int getSegmentOffset() {
104 return segmentOffset;
105 }
106
107 /***
108 * @return Returns the sequenceId.
109 */
110 public long getSequenceId() {
111 return sequenceId;
112 }
113
114 public RecordLocationImpl setSegmentIndexAndOffset(byte segmentIndex, int offset) {
115 return new RecordLocationImpl(this.fileMangerId, segmentIndex, offset);
116 }
117
118 public RecordLocationImpl setSequence(long seq) {
119 return new RecordLocationImpl(this.fileMangerId, this.segmentIndex, this.segmentOffset, seq);
120 }
121 }