summaryrefslogtreecommitdiff
path: root/qpid/tools/src/java/qpid-qmf2/src/main/java/org/apache/qpid/qmf2/common/SchemaClassId.java
blob: 5ad4ba8249c79c3999f59d5742c6cec68e2c1f0d (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 *
 * 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;
import java.util.UUID;

/**
 * Schema are identified by a combination of their package name and class name. A hash value over the body of
 * the schema provides a revision identifier. The class SchemaClassId represents this Schema identifier.
 * <p>
 * If the hash value is not supplied, then the value of the hash string will be set to None. This will be the
 * case when a SchemaClass is being dynamically constructed, and a proper hash is not yet available.
 *
 * @author Fraser Adams
 */
public final class SchemaClassId extends QmfData
{
    private String _packageName = "";
    private String _className = "";
    private String _type = "";
    private UUID _hash = null;

    /**
     * The main constructor, taking a java.util.Map as a parameter.
     *
     * @param m the map used to construct the SchemaClassId.
     */
    public SchemaClassId(final Map m)
    {
        super(m);
        _packageName =  getStringValue("_package_name");
        _className = getStringValue("_class_name");
        _type = getStringValue("_type");
        _hash = hasValue("_hash") ? (UUID)getValue("_hash") : null;
    }

    /**
     * Construct a SchemaClassId from a given class name.
     *
     * @param className the class name.
     */
    public SchemaClassId(final String className)
    {
        this(null, className, null, null);
    }

    /**
     * Construct a SchemaClassId from a given package name and class name.
     *
     * @param packageName the package name.
     * @param className the class name.
     */
    public SchemaClassId(final String packageName, final String className)
    {
        this(packageName, className, null, null);
    }

    /**
     * Construct a SchemaClassId from a given package name and class name and type (_data or _event).
     *
     * @param packageName the package name.
     * @param className the class name.
     * @param type the schema type (_data or _event).
     */
    public SchemaClassId(final String packageName, final String className, final String type)
    {
        this(packageName, className, type, null);
    }

    /**
     * Construct a SchemaClassId from a given package name and class name, type (_data or _event) and hash.
     *
     * @param packageName the package name.
     * @param className the class name.
     * @param type the schema type (_data or _event).
     * @param hash a UUID representation of the md5 hash of the Schema,
     */
    public SchemaClassId(final String packageName, final String className, final String type, final UUID hash)
    {
        if (packageName != null)
        {
            setValue("_package_name", packageName);
            _packageName = packageName;
        }

        if (className != null)
        {
            setValue("_class_name", className);
            _className = className;
        }

        if (type != null)
        {
            setValue("_type", type);
            _type = type;
        }

        if (hash != null)
        {
            setValue("_hash", hash);
            _hash = hash;
        }
    }

    /**
     * Return The name of the associated package.
     * @return The name of the associated package. Returns empty String if there's no package name.
     */
    public String getPackageName()
    {
        return _packageName;
    }

    /**
     * Return The name of the class within the package.
     * @return The name of the class within the package. Returns empty String if there's no class name.
     */
    public String getClassName()
    {
        return _className;
    }

    /**
     * Return The type of schema, either "_data" or "_event".
     * @return The type of schema, either "_data" or "_event". Returns empty String if type is unknown.
     */
    public String getType()
    {
        return _type;
    }

    /**
     * Return The MD5 hash of the schema.
     * @return The MD5 hash of the schema, in the format "%08x-%08x-%08x-%08x"
     */
    public UUID getHashString()
    {
        return _hash;
    }

    /**
     * Compares two SchemaClassId objects for equality.
     * @param rhs the right hands side SchemaClassId in the comparison.
     * @return true if the two SchemaClassId objects are equal otherwise returns false.
     */
    @Override
    public boolean equals(final Object rhs)
    {
        if (rhs instanceof SchemaClassId)
        {
            SchemaClassId that = (SchemaClassId)rhs;
            String lvalue = _packageName + _className + _hash;
            String rvalue = that._packageName + that._className + that._hash;
            return lvalue.equals(rvalue);
        }
        return false;
    }

    /**
     * Returns the SchemaClassId hashCode.
     * @return the SchemaClassId hashCode.
     */
    @Override
    public int hashCode()
    {
        String lvalue = _packageName + _className + _hash;
        return lvalue.hashCode();
    }

    /**
     * Helper/debug method to list the QMF Object properties and their type.
     */
    @Override
    public void listValues()
    {
        System.out.println("_package_name: " + getPackageName());
        System.out.println("_class_name: " + getClassName());
        System.out.println("_type: " + getType());
        System.out.println("_hash: " + getHashString());
    }
}