• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.util.Objects;
23 
24 /**
25  * Class to represent a 128bit value using two long member variables.
26  * Has functionality to convert to/from hex-strings.
27  * Mind that since signed variables are used to store the value internally
28  * is used, the MSB/LSB long values can be negative.
29  */
30 public class SignedLongLong implements Comparable<SignedLongLong> {
31 
32     private long mMostSigBits;
33     private long mLeastSigBits;
34 
SignedLongLong(long leastSigBits, long mostSigBits)35     public SignedLongLong(long leastSigBits, long mostSigBits) {
36         this.mMostSigBits = mostSigBits;
37         this.mLeastSigBits = leastSigBits;
38     }
39 
40     /**
41      * Create a SignedLongLong from a Hex-String without "0x" prefix
42      * @param value the hex-string
43      * @return the created object
44      * @throws UnsupportedEncodingException if "US-ASCII" charset is not supported,
45      * @throws NullPointerException if the string {@code value} is null,
46      * @throws NumberFormatException if the string {@code value} contains invalid characters.
47      */
fromString(String value)48     public static SignedLongLong fromString(String value) throws UnsupportedEncodingException {
49         String lsbStr, msbStr;
50         long msb = 0;
51 
52         lsbStr = msbStr = null;
53         if (value == null) {
54             throw new NullPointerException();
55         }
56         value = value.trim();
57         int valueLength = value.length();
58         if (valueLength == 0 || valueLength > 32) {
59             throw new NumberFormatException("invalid string length: " + valueLength);
60         }
61         if (valueLength <= 16) {
62             lsbStr = value;
63         } else {
64             lsbStr = value.substring(valueLength - 16, valueLength);
65             msbStr = value.substring(0, valueLength - 16);
66             msb = BluetoothMapUtils.getLongFromString(msbStr);
67         }
68         long lsb = BluetoothMapUtils.getLongFromString(lsbStr);
69         return new SignedLongLong(lsb, msb);
70     }
71 
72     @Override
compareTo(SignedLongLong another)73     public int compareTo(SignedLongLong another) {
74         if (mMostSigBits == another.mMostSigBits) {
75             if (mLeastSigBits == another.mLeastSigBits) {
76                 return 0;
77             }
78             if (mLeastSigBits < another.mLeastSigBits) {
79                 return -1;
80             }
81             return 1;
82         }
83         if (mMostSigBits < another.mMostSigBits) {
84             return -1;
85         }
86         return 1;
87     }
88 
89     @Override
toString()90     public String toString() {
91         return toHexString();
92     }
93 
94     /**
95      *
96      * @return a hex-string representation of the object values
97      */
toHexString()98     public String toHexString() {
99         return BluetoothMapUtils.getLongLongAsString(mLeastSigBits, mMostSigBits);
100     }
101 
102     @Override
equals(Object obj)103     public boolean equals(Object obj) {
104         if (this == obj) {
105             return true;
106         }
107         if (obj == null) {
108             return false;
109         }
110         if (!(obj instanceof SignedLongLong)) {
111             return false;
112         }
113         SignedLongLong other = (SignedLongLong) obj;
114         if (mLeastSigBits != other.mLeastSigBits) {
115             return false;
116         }
117         if (mMostSigBits != other.mMostSigBits) {
118             return false;
119         }
120         return true;
121     }
122 
123     @Override
hashCode()124     public int hashCode() {
125         return Objects.hash(mLeastSigBits, mMostSigBits);
126     }
127 
getMostSignificantBits()128     public long getMostSignificantBits() {
129         return mMostSigBits;
130     }
131 
getLeastSignificantBits()132     public long getLeastSignificantBits() {
133         return mLeastSigBits;
134     }
135 
136 }
137