summaryrefslogtreecommitdiff
path: root/java/common/gentools/src/org/apache/qpid/gentools/AmqpConstant.java
blob: df5bc6c36257cefc1390c12bc648daa494770716 (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
/*
 *
 * 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.gentools;

import java.io.PrintStream;
import java.util.TreeMap;

/**
 * @author kpvdr
 *         Class to represent the <constant> declaration within the AMQP specification.
 *         Currently, only integer values exist within the specification, however looking forward
 *         to other possible types in the future, string and double types are also supported.
 *         <p/>
 *         The &lt;constant&gt; declaration in the specification contains only two attributes:
 *         name and value.
 *         <p/>
 *         The value of the constant is mapped against the version(s) for which the name is defined.
 *         This allows for a change in the value rather than the name only from one version to the next.
 */
@SuppressWarnings("serial")
public class AmqpConstant extends TreeMap<String, AmqpVersionSet>
        implements Printable, VersionConsistencyCheck, Comparable<AmqpConstant>
{
    /**
     * Constant name as defined by the name attribute of the &lt;constant&gt; declaration.
     */
    private final String _name;

    /**
     * Set of versions for which this constant name is defined.
     */
    private final AmqpVersionSet _versionSet;

    /**
     * Constructor
     *
     * @param name    Constant name as defined by the name attribute of the &lt;constant&gt; declaration.
     * @param value   Constant value as defined by the value attribute of the &lt;constant&gt; declaration.
     * @param version AMQP version for which this constant is defined
     */
    public AmqpConstant(String name, String value, AmqpVersion version)
    {
        _name = name;
        _versionSet = new AmqpVersionSet(version);
        AmqpVersionSet valueVersionSet = new AmqpVersionSet(version);
        put(value, valueVersionSet);
    }


    /**
     * Get the name of this constant.
     *
     * @return Name of this constant, being the name attribute of the &lt;constant&gt; declaration
     *         represented by this class.
     */
    public String getName()
    {
        return _name;
    }

    /**
     * Get the value of this constant as a String.
     *
     * @param version AMQP version for which this value is required.
     * @return Value of this constant, being the value attribute of the &lt;constant&gt; declaration
     *         represented by this class.
     * @throws AmqpTypeMappingException when a value is requested for a version for which it is not
     *                                  defined in the AMQP specifications.
     */
    public String getStringValue(AmqpVersion version)
            throws AmqpTypeMappingException
    {
        for (String thisValue : keySet())
        {
            AmqpVersionSet versionSet = get(thisValue);
            if (versionSet.contains(version))
            {
                return thisValue;
            }
        }
        throw new AmqpTypeMappingException("Unable to find value for constant \"" + getName() +
                                           "\" for version " + version.toString() + ".");
    }

    /**
     * Get the value of this constant as an integer.
     *
     * @param version AMQP version for which this value is required.
     * @return Value of this constant, being the value attribute of the &lt;constant&gt; declaration
     *         represented by this class.
     * @throws AmqpTypeMappingException when a value is requested for a version for which it is not
     *                                  defined in the AMQP specifications.
     */
    public int getIntegerValue(AmqpVersion version)
            throws AmqpTypeMappingException
    {
        return Integer.parseInt(getStringValue(version));
    }

    /**
     * Get the value of this constant as a double.
     *
     * @param version AMQP version for which this value is required.
     * @return Value of this constant, being the value attribute of the &lt;constant&gt; declaration
     *         represented by this class.
     * @throws AmqpTypeMappingException when a value is requested for a version for which it is not
     *                                  defined in the AMQP specifications.
     */
    public double getDoubleValue(AmqpVersion version)
            throws AmqpTypeMappingException
    {
        return Double.parseDouble(getStringValue(version));
    }

    /**
     * Get the version set for this constant. It contains the all the versions for which this
     * constant name exists.
     *
     * @return Set of versions for which this constant exists.
     */
    public AmqpVersionSet getVersionSet()
    {
        return _versionSet;
    }

    /* (non-Javadoc)
    * @see java.lang.Comparable#compareTo(java.lang.Object)
    */

    public int compareTo(AmqpConstant other)
    {
        int res = getName().compareTo(other.getName());
        if (res != 0)
        {
            return res;
        }
        return getVersionSet().compareTo(other.getVersionSet());
    }

    /* (non-Javadoc)
     * @see org.apache.qpid.gentools.VersionConsistencyCheck#isVersionConsistent(org.apache.qpid.gentools.AmqpVersionSet)
     */
    public boolean isVersionConsistent(AmqpVersionSet globalVersionSet)
    {
        if (size() != 1)
        {
            return false;
        }
        return get(firstKey()).equals(globalVersionSet);
    }

    /* (non-Javadoc)
     * @see org.apache.qpid.gentools.Printable#print(java.io.PrintStream, int, int)
     */
    public void print(PrintStream out, int marginSize, int tabSize)
    {
        String margin = Utils.createSpaces(marginSize);
        String tab = Utils.createSpaces(tabSize);
        if (size() == 1)
        {
            out.println(margin + tab + "[C] " + getName() + " = \"" + firstKey() + "\" " + getVersionSet());
        }
        else
        {
            out.println(margin + tab + "[C] " + getName() + ": " + getVersionSet());
            for (String thisValue : keySet())
            {
                out.println(margin + tab + tab + "= \"" + thisValue + "\" " + get(thisValue));
            }
        }
    }

}