1 /*
2  * Copyright (C) 2016 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 package android.car;
17 
18 import android.annotation.SystemApi;
19 
20 /**
21  * Collection of utilities for handling zones
22  * @hide
23  */
24 @SystemApi
25 public final class VehicleZoneUtil {
26 
27     /**
28      * Change zone flag into index with available zones.
29      * For example with zoned of 0x80000001, 0x1 will be index 0 and 0x80000000 will be index 2.
30      * @param available zones, should not be zero
31      * @param zone flag for the zone to get index, should not be zero and should be one of the flags
32      *             defined in zones.
33      * @return index of desired zone.
34      * @throws IllegalArgumentException if zones or zone is invalid.
35      */
zoneToIndex(int zones, int zone)36     public static int zoneToIndex(int zones, int zone) throws IllegalArgumentException {
37         if ((zone == 0) || // check that zone is non-zero
38                 ((zone & zones) != zone) || // check that zone is inside of zones
39                 ((zone & (zone - 1)) != 0)) { // check that zone only has one bit set
40             throw new IllegalArgumentException("Invalid zones 0x" + Integer.toHexString(zones) +
41                     " or zone 0x" + Integer.toHexString(zone));
42         }
43         int index = -1;
44         while((zone & zones) != 0) {
45             index++;
46             zones &= zones - 1;
47         }
48         return index;
49     }
50 
51     /**
52      * Return number of zones (non-zero flag) from given zones.
53      */
getNumberOfZones(int zones)54     public static int getNumberOfZones(int zones) {
55         int numZones = 0;
56         while (zones != 0) {
57             zones &= zones - 1;
58             numZones++;
59         }
60         return numZones;
61     }
62 
63     /**
64      * Return bit flag of first zone. If zones is 0, it will just return 0.
65      * @param zones can be 0 if there is no zone
66      * @return
67      */
getFirstZone(int zones)68     public static int getFirstZone(int zones) {
69         if (zones == 0) {
70             return 0;
71         }
72         int xorFlag = zones & (zones - 1);
73 
74         return zones ^ xorFlag;
75     }
76 
77     /**
78      * Return bit flag of zone available after startingZone. For zones of 0x7 with startingZone of
79      * 0x2, it will return 0x4. If no zone exist after startingZone, it will return 0.
80      * @param zones
81      * @param startingZone A bit flag representing a zone. This does not necessarily be one of flags
82      *                     available in zones.
83      * @return
84      * @throws IllegalArgumentException If startingZone is invalid.
85      */
getNextZone(int zones, int startingZone)86     public static int getNextZone(int zones, int startingZone) throws IllegalArgumentException {
87         if ((startingZone & (startingZone - 1)) != 0 || startingZone == 0) {
88             throw new IllegalArgumentException(
89                     "Starting zone should represent only one bit flag: 0x" +
90                             Integer.toHexString(startingZone));
91         }
92 
93         // Create a mask that sets all bits above the current one
94         int mask = startingZone << 1;
95         mask -= 1;
96         mask = ~mask;
97         return getFirstZone(zones & mask);
98     }
99 
100     /**
101      * Return array of zone with each active zone in one index. This can be useful for iterating
102      * all available zones.
103      */
listAllZones(int zones)104     public static int[] listAllZones(int zones) {
105         int numberOfZones = getNumberOfZones(zones);
106         int[] list = new int[numberOfZones];
107         if (numberOfZones == 0) {
108             return list;
109         }
110 
111         int arrayIndex = 0;
112         while (zones != 0) {
113             int xorFlag = zones & (zones - 1);
114             int zone = zones ^ xorFlag;
115             list[arrayIndex++] = zone;
116             zones = xorFlag;
117         }
118         return list;
119     }
120 
VehicleZoneUtil()121     private VehicleZoneUtil() {}
122 }
123