001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.tm;
023:
024: /**
025: * LocalId is a wrapper for a long value that identifies a transaction within
026: * a JBoss server. This implementation is immutable and serializable at
027: * runtime.
028: *
029: * @author <a href="reverbel@ime.usp.br">Francisco Reverbel</a>
030: * @version $Revision: 57208 $
031: */
032: public class LocalId implements java.io.Externalizable {
033: static final long serialVersionUID = 2076780468014328911L;
034: /**
035: * Long value that identifies a transaction within a JBoss server.
036: * This is really a sequence number generated by the XidFactory.
037: */
038: private long value;
039:
040: // Static --------------------------------------------------------
041:
042: public static void toByteArray(long localIdValue, byte[] dst,
043: int dstBegin) {
044: dst[dstBegin + 0] = (byte) (0xff & (localIdValue >>> 56));
045: dst[dstBegin + 1] = (byte) (0xff & (localIdValue >>> 48));
046: dst[dstBegin + 2] = (byte) (0xff & (localIdValue >>> 40));
047: dst[dstBegin + 3] = (byte) (0xff & (localIdValue >>> 32));
048: dst[dstBegin + 4] = (byte) (0xff & (localIdValue >>> 24));
049: dst[dstBegin + 5] = (byte) (0xff & (localIdValue >>> 16));
050: dst[dstBegin + 6] = (byte) (0xff & (localIdValue >>> 8));
051: dst[dstBegin + 7] = (byte) (0xff & (localIdValue >>> 0));
052: }
053:
054: public static long fromByteArray(byte[] src, int srcBegin) {
055: return ((long) (src[srcBegin + 0] & 0xff) << 56)
056: | ((long) (src[srcBegin + 1] & 0xff) << 48)
057: | ((long) (src[srcBegin + 2] & 0xff) << 40)
058: | ((long) (src[srcBegin + 3] & 0xff) << 32)
059: | ((long) (src[srcBegin + 4] & 0xff) << 24)
060: | ((long) (src[srcBegin + 5] & 0xff) << 16)
061: | ((long) (src[srcBegin + 6] & 0xff) << 8)
062: | ((long) (src[srcBegin + 7] & 0xff));
063: }
064:
065: // Constructors --------------------------------------------------
066:
067: /**
068: * No-arg constructor for Externalizable support.
069: */
070: public LocalId() {
071: }
072:
073: /**
074: * Create a new instance. This constructor is public <em>only</em>
075: * to get around a class loader problem; it should be package-private.
076: */
077: public LocalId(long value) {
078: this .value = value;
079: }
080:
081: public LocalId(XidImpl xid) {
082: this (xid.getLocalIdValue());
083: }
084:
085: // Public --------------------------------------------------------
086:
087: public long getValue() {
088: return value;
089: }
090:
091: /**
092: * Compare for equality.
093: */
094: public boolean equals(Object obj) {
095: return (obj instanceof LocalId) ? (value == ((LocalId) obj).value)
096: : false;
097: }
098:
099: public int hashCode() {
100: return (int) value;
101: }
102:
103: // Externalizable implementation ---------------------------------
104: public void writeExternal(java.io.ObjectOutput out)
105: throws java.io.IOException {
106: out.writeLong(value);
107: }
108:
109: public void readExternal(java.io.ObjectInput in)
110: throws java.io.IOException, ClassNotFoundException {
111: value = in.readLong();
112: }
113:
114: }
|