| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.apache.qpid.proton.amqp; |
| 23 |
|
| 24 |
import java.math.BigDecimal; |
| 25 |
import java.nio.ByteBuffer; |
| 26 |
|
| 27 |
public final class Decimal128 extends Number |
| 28 |
{ |
| 29 |
private final BigDecimal _underlying; |
| 30 |
private final long _msb; |
| 31 |
private final long _lsb; |
| 32 |
|
| 33 |
public Decimal128(BigDecimal underlying) |
| 34 |
{ |
| 35 |
_underlying = underlying; |
| 36 |
|
| 37 |
_msb = calculateMostSignificantBits(underlying); |
| 38 |
_lsb = calculateLeastSignificantBits(underlying); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
public Decimal128(final long msb, final long lsb) |
| 43 |
{ |
| 44 |
_msb = msb; |
| 45 |
_lsb = lsb; |
| 46 |
|
| 47 |
_underlying = calculateBigDecimal(msb, lsb); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public Decimal128(byte[] data) |
| 52 |
{ |
| 53 |
this(ByteBuffer.wrap(data)); |
| 54 |
} |
| 55 |
|
| 56 |
private Decimal128(final ByteBuffer buffer) |
| 57 |
{ |
| 58 |
this(buffer.getLong(),buffer.getLong()); |
| 59 |
} |
| 60 |
|
| 61 |
private static long calculateMostSignificantBits(final BigDecimal underlying) |
| 62 |
{ |
| 63 |
return 0; //TODO. |
| 64 |
} |
| 65 |
|
| 66 |
private static long calculateLeastSignificantBits(final BigDecimal underlying) |
| 67 |
{ |
| 68 |
return 0; //TODO. |
| 69 |
} |
| 70 |
|
| 71 |
private static BigDecimal calculateBigDecimal(final long msb, final long lsb) |
| 72 |
{ |
| 73 |
return BigDecimal.ZERO; //TODO. |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public int intValue() |
| 78 |
{ |
| 79 |
return _underlying.intValue(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public long longValue() |
| 84 |
{ |
| 85 |
return _underlying.longValue(); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public float floatValue() |
| 90 |
{ |
| 91 |
return _underlying.floatValue(); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public double doubleValue() |
| 96 |
{ |
| 97 |
return _underlying.doubleValue(); |
| 98 |
} |
| 99 |
|
| 100 |
public long getMostSignificantBits() |
| 101 |
{ |
| 102 |
return _msb; |
| 103 |
} |
| 104 |
|
| 105 |
public long getLeastSignificantBits() |
| 106 |
{ |
| 107 |
return _lsb; |
| 108 |
} |
| 109 |
|
| 110 |
public byte[] asBytes() |
| 111 |
{ |
| 112 |
byte[] bytes = new byte[16]; |
| 113 |
ByteBuffer buf = ByteBuffer.wrap(bytes); |
| 114 |
buf.putLong(getMostSignificantBits()); |
| 115 |
buf.putLong(getLeastSignificantBits()); |
| 116 |
return bytes; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public boolean equals(final Object o) |
| 121 |
{ |
| 122 |
if (this == o) |
| 123 |
{ |
| 124 |
return true; |
| 125 |
} |
| 126 |
if (o == null || getClass() != o.getClass()) |
| 127 |
{ |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
final Decimal128 that = (Decimal128) o; |
| 132 |
|
| 133 |
if (_lsb != that._lsb) |
| 134 |
{ |
| 135 |
return false; |
| 136 |
} |
| 137 |
if (_msb != that._msb) |
| 138 |
{ |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public int hashCode() |
| 147 |
{ |
| 148 |
int result = (int) (_msb ^ (_msb >>> 32)); |
| 149 |
result = 31 * result + (int) (_lsb ^ (_lsb >>> 32)); |
| 150 |
return result; |
| 151 |
} |
| 152 |
} |