summaryrefslogtreecommitdiffstats
path: root/src/com/android/bluetooth/SignedLongLong.java
blob: 42fd4a8523ce293182d176388aebd2029f0c7d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (C) 2015 Samsung System LSI
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.android.bluetooth;

import java.io.UnsupportedEncodingException;

import com.android.bluetooth.map.BluetoothMapUtils;

/**
 * Class to represent a 128bit value using two long member variables.
 * Has functionality to convert to/from hex-strings.
 * Mind that since signed variables are used to store the value internally
 * is used, the MSB/LSB long values can be negative.
 */
public class SignedLongLong implements Comparable<SignedLongLong> {

    private long mMostSigBits;
    private long mLeastSigBits;

    public SignedLongLong(long leastSigBits, long mostSigBits) {
        this.mMostSigBits = mostSigBits;
        this.mLeastSigBits = leastSigBits;
    }

    /**
     * Create a SignedLongLong from a Hex-String without "0x" prefix
     * @param value the hex-string
     * @return the created object
     * @throws UnsupportedEncodingException
     */
    public static SignedLongLong fromString(String value) throws UnsupportedEncodingException {
        String lsbStr, msbStr;
        long lsb = 0, msb = 0;

        lsbStr = msbStr = null;
        if(value == null) throw new NullPointerException();
        value=value.trim();
        int valueLength = value.length();
        if(valueLength == 0 || valueLength > 32) {
            throw new NumberFormatException("invalid string length: " + valueLength);
        }
        if(valueLength <= 16){
            lsbStr = value;
        } else {
            lsbStr = value.substring(valueLength-16, valueLength);
            msbStr = value.substring(0, valueLength-16);
            msb = BluetoothMapUtils.getLongFromString(msbStr);
        }
        lsb = BluetoothMapUtils.getLongFromString(lsbStr);
        return new SignedLongLong(lsb, msb);
    }

    @Override
    public int compareTo(SignedLongLong another) {
        if(mMostSigBits == another.mMostSigBits) {
            if(mLeastSigBits == another.mLeastSigBits) {
                return 0;
            }
            if(mLeastSigBits < another.mLeastSigBits) {
                return -1;
            }
            return 1;
        }
        if(mMostSigBits < another.mMostSigBits) {
            return -1;
        }
        return 1;
    }

    @Override
    public String toString() {
        return toHexString();
    }

    /**
     *
     * @return a hex-string representation of the object values
     */
    public String toHexString(){
        return BluetoothMapUtils.getLongLongAsString(mLeastSigBits, mMostSigBits);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        SignedLongLong other = (SignedLongLong) obj;
        if (mLeastSigBits != other.mLeastSigBits) {
            return false;
        }
        if (mMostSigBits != other.mMostSigBits) {
            return false;
        }
        return true;
    }

    public long getMostSignificantBits() {
        return mMostSigBits;
    }

    public long getLeastSignificantBits() {
        return mLeastSigBits;
    }

}