2 * Copyright 2011-2012 BitMeister Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package jp.bitmeister.asn1.type.builtin;
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;
29 * Represents ASN.1 'REAL' type.
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.
38 * @author WATANABE, Jun. <jwat at bitmeister.jp>
41 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 9, tagMode = ASN1TagMode.IMPLICIT)
42 public class REAL extends PrimitiveType<Double> implements ValueComparable {
44 private boolean isBinary = false;
47 * Instantiates an empty {@code REAL}.
53 * Instantiates an empty {@code REAL} and sets encoding mode.
56 * The encoding mode. {@code true} means this REAL data will be
57 * encoded in binary encoding.
59 public REAL(boolean isBinary) {
60 this.isBinary = isBinary;
64 * Instantiates a {@code REAL} and initialize it with the {@code double}
68 * The value assigned to the instance.
70 public REAL(double value) {
75 * Instantiates a {@code REAL}, initializes it with the {@code double} value
76 * and sets encoding mode.
79 * The value assigned to the instance.
83 public REAL(double value, boolean isBinary) {
89 * Instantiates a {@code REAL} and initializes it with the value specified
90 * by the M*B^E formula.
95 * Base. It can take values 2 or 10.
99 public REAL(long mantissa, int base, int exponent) {
100 set(mantissa, base, exponent);
104 * Sets the {@code Float} value to the instance.
107 * The value assigned to the instance.
109 public void set(Float value) {
114 * Sets the value specified by the M*B^E formula to the instance.
119 * Base. It can take values 2 or 10.
123 public void set(long mantissa, int base, int exponent) {
126 } else if (base == 10) {
129 ASN1IllegalArgument ex = new ASN1IllegalArgument();
130 ex.setMessage("Invalid base value '" + base
131 + "'. Base must be '2' or '10'.", null, getClass(), null,
135 set(mantissa * Math.pow(base, exponent));
139 * Sets binary encoding mode.
141 public void setBinaryEncoding() {
146 * Sets decimal encoding mode.
148 public void setDecimalEncoding() {
153 * Returns encoding mode for this data.
155 * @return The encoding mode.
157 public boolean isBinary() {
165 * jp.bitmeister.asn1.type.ValueComparable#compareTo(jp.bitmeister.asn1.
168 public int compareTo(ASN1Type other) {
169 return value().compareTo(((REAL) other).value());
176 * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
180 public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
181 return visitor.visit(this);