NodeID.java 2.69 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
Gaston Dombiak's avatar
Gaston Dombiak committed
11 12 13 14 15 16 17 18 19 20
 */

package org.jivesoftware.openfire.cluster;

import org.jivesoftware.util.cache.ExternalizableUtil;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
21
import java.util.ArrayList;
Gaston Dombiak's avatar
Gaston Dombiak committed
22
import java.util.Arrays;
23
import java.util.List;
Gaston Dombiak's avatar
Gaston Dombiak committed
24 25 26 27 28 29 30 31 32 33 34

/**
 * Class which wraps the byte[] we use to identify cluster members. The main reason
 * for this class is that byte[] cannot be directly compared so having a collection
 * of byte[] is not possible since you cannot remove to equivalent byte[] from the
 * collection.<p>
 *
 * @author Pete Matern
 * @author Gaston Dombiak
 */
public class NodeID implements Externalizable {
35 36
    private static List<NodeID> instances = new ArrayList<NodeID>();

Gaston Dombiak's avatar
Gaston Dombiak committed
37 38
    private byte[] nodeID;

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    public static synchronized NodeID getInstance(byte[] nodeIdBytes) {
        for (NodeID nodeID : instances) {
            if (nodeID.equals(nodeIdBytes)) {
                return nodeID;
            }
        }
        NodeID answer = new NodeID(nodeIdBytes);
        instances.add(answer);
        return answer;
    }

    public static synchronized void deleteInstance(byte[] nodeIdBytes) {
        NodeID toDelete = null;
        for (NodeID nodeID : instances) {
            if (nodeID.equals(nodeIdBytes)) {
                toDelete = nodeID;
                break;
            }
        }
        if (toDelete != null) {
            instances.remove(toDelete);
        }
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
63 64 65
    public NodeID() {
    }

66
    private NodeID(byte[] nodeIdBytes) {
Gaston Dombiak's avatar
Gaston Dombiak committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        this.nodeID = nodeIdBytes;
    }

    public boolean equals(byte[] anotherID) {
        return Arrays.equals(nodeID, anotherID);
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        NodeID that = (NodeID) o;

        return Arrays.equals(nodeID, that.nodeID);
    }

    public int hashCode() {
        return Arrays.hashCode(nodeID);
    }

    public byte[] toByteArray() {
        return nodeID;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        ExternalizableUtil.getInstance().writeByteArray(out, nodeID);
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        nodeID = ExternalizableUtil.getInstance().readByteArray(in);
    }
}