OSDN Git Service

915fe463b532d96dad288d7f959c868cf56f9552
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / builtin / REAL.java
1 /*
2  * Copyright 2011-2012 BitMeister Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package jp.bitmeister.asn1.type.builtin;
17
18 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
19 import jp.bitmeister.asn1.annotation.ASN1Tag;
20 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
21 import jp.bitmeister.asn1.processor.ASN1Visitor;
22 import jp.bitmeister.asn1.type.ASN1TagClass;
23 import jp.bitmeister.asn1.type.ASN1TagMode;
24 import jp.bitmeister.asn1.type.ASN1Type;
25 import jp.bitmeister.asn1.type.PrimitiveType;
26 import jp.bitmeister.asn1.type.ValueComparable;
27
28 /**
29  * Represents ASN.1 'REAL' type.
30  * 
31  * <p>
32  * An instance of this class represents a 'REAL' type data, and contains a
33  * {@link java.lang.Double} value. Encoding mode will be used when the data is
34  * encoded with BER or DER. Default value for encoding mode is decimal and it
35  * can be set to binary.
36  * </p>
37  * 
38  * @author WATANABE, Jun. <jwat at bitmeister.jp>
39  */
40 @ASN1BuiltIn
41 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 9, tagMode = ASN1TagMode.IMPLICIT)
42 public class REAL extends PrimitiveType<Double> implements ValueComparable {
43
44         private boolean isBinary = false;
45
46         /**
47          * Instantiates an empty {@code REAL}.
48          */
49         public REAL() {
50         }
51
52         /**
53          * Instantiates an empty {@code REAL} and sets encoding mode.
54          * 
55          * @param isBinary
56          *            The encoding mode. {@code true} means this REAL data will be
57          *            encoded in binary encoding.
58          */
59         public REAL(boolean isBinary) {
60                 this.isBinary = isBinary;
61         }
62
63         /**
64          * Instantiates a {@code REAL} and initialize it with the {@code double}
65          * value.
66          * 
67          * @param value
68          *            The value assigned to the instance.
69          */
70         public REAL(double value) {
71                 set(value);
72         }
73
74         /**
75          * Instantiates a {@code REAL}, initializes it with the {@code double} value
76          * and sets encoding mode.
77          * 
78          * @param value
79          *            The value assigned to the instance.
80          * @param isBinary
81          *            The encoding mode.
82          */
83         public REAL(double value, boolean isBinary) {
84                 this(isBinary);
85                 set(value);
86         }
87
88         /**
89          * Instantiates a {@code REAL} and initializes it with the value specified
90          * by the M*B^E formula.
91          * 
92          * @param mantissa
93          *            Mantissa.
94          * @param base
95          *            Base. It can take values 2 or 10.
96          * @param exponent
97          *            Exponent.
98          */
99         public REAL(long mantissa, int base, int exponent) {
100                 set(mantissa, base, exponent);
101         }
102
103         /**
104          * Sets the {@code Float} value to the instance.
105          * 
106          * @param value
107          *            The value assigned to the instance.
108          */
109         public void set(Float value) {
110                 set((double) value);
111         }
112
113         /**
114          * Sets the value specified by the M*B^E formula to the instance.
115          * 
116          * @param mantissa
117          *            Mantissa.
118          * @param base
119          *            Base. It can take values 2 or 10.
120          * @param exponent
121          *            Exponent.
122          */
123         public void set(long mantissa, int base, int exponent) {
124                 if (base == 2) {
125                         isBinary = true;
126                 } else if (base == 10) {
127                         isBinary = false;
128                 } else {
129                         ASN1IllegalArgument ex = new ASN1IllegalArgument();
130                         ex.setMessage("Invalid base value '" + base
131                                         + "'. Base must be '2' or '10'.", null, getClass(), null,
132                                         null);
133                         throw ex;
134                 }
135                 set(mantissa * Math.pow(base, exponent));
136         }
137
138         /**
139          * Sets binary encoding mode.
140          */
141         public void setBinaryEncoding() {
142                 isBinary = true;
143         }
144
145         /**
146          * Sets decimal encoding mode.
147          */
148         public void setDecimalEncoding() {
149                 isBinary = false;
150         }
151
152         /**
153          * Returns encoding mode for this data.
154          * 
155          * @return The encoding mode.
156          */
157         public boolean isBinary() {
158                 return isBinary;
159         }
160
161         /*
162          * (non-Javadoc)
163          * 
164          * @see
165          * jp.bitmeister.asn1.type.ValueComparable#compareTo(jp.bitmeister.asn1.
166          * type.ASN1Type)
167          */
168         public int compareTo(ASN1Type other) {
169                 return value().compareTo(((REAL) other).value());
170         }
171
172         /*
173          * (non-Javadoc)
174          * 
175          * @see
176          * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
177          * ASN1Visitor)
178          */
179         @Override
180         public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
181                 return visitor.visit(this);
182         }
183
184 }