summaryrefslogtreecommitdiff
path: root/qpid/tools/src/java/qpid-qmf2/src/main/java/org/apache/qpid/qmf2/common/ObjectId.java
blob: b84822def74454ce3ca2b9861ae24bb12b6a2ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.qmf2.common;

// Misc Imports
import java.util.Map;

/**
 * This class provides a wrapper for QMF Object IDs to enable easier comparisons.
 * <p>
 * QMF Object IDs are Maps and <i>in theory</i> equals should work, but strings in QMF are actually often returned
 * as byte[] due to inconsistent binary and UTF-8 encodings being used and byte[].equals() compares the address not a
 * bytewise comparison.
 * <p>
 * This class creates a String from the internal ObjectId state information to enable easier comparison and rendering.
 *
 * @author Fraser Adams
 */
public final class ObjectId extends QmfData
{
    private final String _agentName;
    private final String _objectName;
    private final long   _agentEpoch;

    /**
     * Create an ObjectId given the ID created via ObjectId.toString().
     * @param oid a string created via ObjectId.toString().
     */
    public ObjectId(String oid)
    {
        String[] split = oid.split("@");

        _agentName  = split.length == 3 ? split[0] : "";
        _agentEpoch = split.length == 3 ? Long.parseLong(split[1]) : 0;
        _objectName = split.length == 3 ? split[2] : "";

        setValue("_agent_name", _agentName);
        setValue("_agent_epoch", _agentEpoch);
        setValue("_object_name", _objectName);
    }

    /**
     * Create an ObjectId given an agentName, objectName and agentEpoch.
     * @param agentName the name of the Agent managing the object.
     * @param objectName the name of the managed object.
     * @param agentEpoch a count used to identify if an Agent has been restarted.
     */
    public ObjectId(String agentName, String objectName, long agentEpoch)
    {
        _agentName = agentName;
        _objectName = objectName;
        _agentEpoch = agentEpoch;
        setValue("_agent_name", _agentName);
        setValue("_object_name", _objectName);
        setValue("_agent_epoch", _agentEpoch);
    }

    /**
     * Create an ObjectId from a Map. In essence it "deserialises" its state from the Map.
     * @param m the Map the Object is retrieving its state from.
     */
    public ObjectId(Map m)
    {
        super(m);
        _agentName = getStringValue("_agent_name");
        _objectName = getStringValue("_object_name");
        _agentEpoch = getLongValue("_agent_epoch");
    }

    /**
     * Create an ObjectId from a QmfData object. In essence it "deserialises" its state from the QmfData object.
     * @param qmfd the QmfData the Object is retrieving its state from.
     */
    public ObjectId(QmfData qmfd)
    {
        this(qmfd.mapEncode());
    }

    /**
     * Returns the name of the Agent managing the object.
     * @return the name of the Agent managing the object.
     */
    public String getAgentName()
    {
        return _agentName;
    }

    /**
     * Returns the name of the managed object.
     * @return the name of the managed object.
     */
    public String getObjectName()
    {
        return _objectName;
    }

    /**
     * Returns the Epoch count.
     * @return the Epoch count.
     */
    public long getAgentEpoch()
    {
        return _agentEpoch;
    }

    /**
     * Compares two ObjectId objects for equality.
     * @param rhs the right hands side ObjectId in the comparison.
     * @return true if the two ObjectId objects are equal otherwise returns false.
     */
    @Override
    public boolean equals(Object rhs)
    {
        if (rhs instanceof ObjectId)
        {
            return toString().equals(rhs.toString());
        }
        return false;
    }

    /**
     * Returns the ObjectId hashCode.
     * @return the ObjectId hashCode.
     */
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }

    /**
     * Returns a String representation of the ObjectId.
     * @return a String representation of the ObjectId.
     */
    @Override
    public String toString()
    {
        return _agentName + "@" +  _agentEpoch + "@" + _objectName;
    }
}