1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.android.internal.telephony;
18 
19 import java.util.Objects;
20 
21 /**
22  * Represent the version of underlying vendor HAL service.
23  * @see <a href="https://source.android.com/devices/architecture/hidl/versioning">
24  * HIDL versioning</a>.
25  */
26 public class HalVersion implements Comparable<HalVersion> {
27 
28     /** The HAL Version indicating that the version is unknown or invalid */
29     public static final HalVersion UNKNOWN = new HalVersion(-1, -1);
30 
31     public final int major;
32 
33     public final int minor;
34 
HalVersion(int major, int minor)35     public HalVersion(int major, int minor) {
36         this.major = major;
37         this.minor = minor;
38     }
39 
40     @Override
compareTo(HalVersion ver)41     public int compareTo(HalVersion ver) {
42         if (ver == null) {
43             return 1;
44         }
45         if (this.major > ver.major) {
46             return 1;
47         } else if (this.major < ver.major) {
48             return -1;
49         } else if (this.minor > ver.minor) {
50             return 1;
51         } else if (this.minor < ver.minor) {
52             return -1;
53         }
54         return 0;
55     }
56 
57     @Override
hashCode()58     public int hashCode() {
59         return Objects.hash(major, minor);
60     }
61 
62     @Override
equals(Object o)63     public boolean equals(Object o) {
64         return ((o instanceof HalVersion) && (o == this || compareTo((HalVersion) o) == 0));
65     }
66 
67     /**
68      * @return True if the version is greater than the compared version.
69      */
greater(HalVersion ver)70     public boolean greater(HalVersion ver) {
71         return compareTo(ver) > 0;
72     }
73 
74     /**
75      * @return True if the version is less than the compared version.
76      */
less(HalVersion ver)77     public boolean less(HalVersion ver) {
78         return compareTo(ver) < 0;
79     }
80 
81     /**
82      * @return True if the version is greater than or equal to the compared version.
83      */
greaterOrEqual(HalVersion ver)84     public boolean greaterOrEqual(HalVersion ver) {
85         return greater(ver) || equals(ver);
86     }
87 
88     /**
89      * @return True if the version is less than or equal to the compared version.
90      */
lessOrEqual(HalVersion ver)91     public boolean lessOrEqual(HalVersion ver) {
92         return less(ver) || equals(ver);
93     }
94 
95     @Override
toString()96     public String toString() {
97         return major + "." + minor;
98     }
99 }
100