Location xref

View Javadoc

    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.activeio.journal.active;
    19
    20  import java.io.DataInput;
    21  import java.io.DataOutput;
    22  import java.io.IOException;
    23
    24  import org.activeio.Packet;
    25  import org.activeio.PacketData;
    26  import org.activeio.journal.RecordLocation;
    27
    28  /***
    29   * Defines a where a record can be located in the Journal.
    30   *
    31   * @version $Revision: 1.1 $
    32   */
    33  final public class Location implements RecordLocation {
    34
    35      static final public int SERIALIZED_SIZE=8;
    36
    37      final private int logFileId;
    38      final private int logFileOffset;
    39
    40      public Location(int logFileId, int fileOffset) {
    41          this.logFileId = logFileId;
    42          this.logFileOffset = fileOffset;
    43      }
    44
    45      public int compareTo(Object o) {
    46          int rc = logFileId - ((Location) o).logFileId;
    47          if (rc != 0)
    48              return rc;
    49
    50          return logFileOffset - ((Location) o).logFileOffset;
    51      }
    52
    53      public int hashCode() {
    54          return logFileOffset ^ logFileId;
    55      }
    56
    57      public boolean equals(Object o) {
    58          if (o == null || o.getClass() != Location.class)
    59              return false;
    60          Location rl = (Location) o;
    61          return rl.logFileId == this.logFileId && rl.logFileOffset == this.logFileOffset;
    62      }
    63
    64      public String toString() {
    65          return "" + logFileId + ":" + logFileOffset;
    66      }
    67
    68      public int getLogFileId() {
    69          return logFileId;
    70      }
    71
    72      public int getLogFileOffset() {
    73          return logFileOffset;
    74      }
    75
    76      public void writeToPacket(Packet packet) throws IOException {
    77          PacketData data = new PacketData(packet);
    78          data.writeInt(logFileId);
    79          data.writeInt(logFileOffset);
    80      }
    81
    82      public void writeToDataOutput(DataOutput data) throws IOException {
    83          data.writeInt(logFileId);
    84          data.writeInt(logFileOffset);
    85      }
    86
    87      static public Location readFromPacket(Packet packet) throws IOException {
    88          PacketData data = new PacketData(packet);
    89          return new Location(data.readInt(), data.readInt());
    90      }
    91
    92      public static Location readFromDataInput(DataInput data) throws IOException {
    93          return new Location(data.readInt(), data.readInt());
    94      }
    95
    96
    97  }