1 /*
2 * Copyright (C) 2015 Samsung System LSI
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 
17 package com.android.bluetooth;
18 
19 import com.android.bluetooth.map.BluetoothMapUtils;
20 
21 import java.io.UnsupportedEncodingException;
22 
23 /**
24  * Class to represent a 128bit value using two long member variables.
25  * Has functionality to convert to/from hex-strings.
26  * Mind that since signed variables are used to store the value internally
27  * is used, the MSB/LSB long values can be negative.
28  */
29 public class SignedLongLong implements Comparable<SignedLongLong> {
30 
31     private long mMostSigBits;
32     private long mLeastSigBits;
33 
SignedLongLong(long leastSigBits, long mostSigBits)34     public SignedLongLong(long leastSigBits, long mostSigBits) {
35         this.mMostSigBits = mostSigBits;
36         this.mLeastSigBits = leastSigBits;
37     }
38 
39     /**
40      * Create a SignedLongLong from a Hex-String without "0x" prefix
41      * @param value the hex-string
42      * @return the created object
43      * @throws UnsupportedEncodingException
44      */
fromString(String value)45     public static SignedLongLong fromString(String value) throws UnsupportedEncodingException {
46         String lsbStr, msbStr;
47         long lsb = 0, msb = 0;
48 
49         lsbStr = msbStr = null;
50         if (value == null) {
51             throw new NullPointerException();
52         }
53         value = value.trim();
54         int valueLength = value.length();
55         if (valueLength == 0 || valueLength > 32) {
56             throw new NumberFormatException("invalid string length: " + valueLength);
57         }
58         if (valueLength <= 16) {
59             lsbStr = value;
60         } else {
61             lsbStr = value.substring(valueLength - 16, valueLength);
62             msbStr = value.substring(0, valueLength - 16);
63             msb = BluetoothMapUtils.getLongFromString(msbStr);
64         }
65         lsb = BluetoothMapUtils.getLongFromString(lsbStr);
66         return new SignedLongLong(lsb, msb);
67     }
68 
69     @Override
compareTo(SignedLongLong another)70     public int compareTo(SignedLongLong another) {
71         if (mMostSigBits == another.mMostSigBits) {
72             if (mLeastSigBits == another.mLeastSigBits) {
73                 return 0;
74             }
75             if (mLeastSigBits < another.mLeastSigBits) {
76                 return -1;
77             }
78             return 1;
79         }
80         if (mMostSigBits < another.mMostSigBits) {
81             return -1;
82         }
83         return 1;
84     }
85 
86     @Override
toString()87     public String toString() {
88         return toHexString();
89     }
90 
91     /**
92      *
93      * @return a hex-string representation of the object values
94      */
toHexString()95     public String toHexString() {
96         return BluetoothMapUtils.getLongLongAsString(mLeastSigBits, mMostSigBits);
97     }
98 
99     @Override
equals(Object obj)100     public boolean equals(Object obj) {
101         if (this == obj) {
102             return true;
103         }
104         if (obj == null) {
105             return false;
106         }
107         if (getClass() != obj.getClass()) {
108             return false;
109         }
110         SignedLongLong other = (SignedLongLong) obj;
111         if (mLeastSigBits != other.mLeastSigBits) {
112             return false;
113         }
114         if (mMostSigBits != other.mMostSigBits) {
115             return false;
116         }
117         return true;
118     }
119 
getMostSignificantBits()120     public long getMostSignificantBits() {
121         return mMostSigBits;
122     }
123 
getLeastSignificantBits()124     public long getLeastSignificantBits() {
125         return mLeastSigBits;
126     }
127 
128 }
129