• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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.server.wifi.util;
18 
19 import java.util.BitSet;
20 
21 /**
22  * Class for general helper methods and objects for Wifi Framework code.
23  * @hide
24  */
25 public class GeneralUtil {
26 
27     /**
28      * Class which can be used to fetch an object out of a lambda. Fetching an object
29      * out of a local scope with HIDL is a common operation (although usually it can
30      * and should be avoided).
31      *
32      * @param <E> Inner object type.
33      */
34     public static final class Mutable<E> {
35         public E value;
36 
Mutable()37         public Mutable() {
38             value = null;
39         }
40 
Mutable(E value)41         public Mutable(E value) {
42             this.value = value;
43         }
44     }
45 
46     /**
47      * Convert a capability bitmask to its index in a BitSet.
48      *
49      * TODO: Remove this method once the WifiManager capabilities are
50      *       represented as indexes rather than bitmasks.
51      */
getCapabilityIndex(long capability)52     public static int getCapabilityIndex(long capability) {
53         // Index of the first enabled bit is the number of trailing zeroes
54         // in the binary representation
55         return Long.numberOfTrailingZeros(capability);
56     }
57 
58     /**
59      * Convert a long to a BitSet.
60      *
61      * See TODO in {@link #getCapabilityIndex(long)}.
62      */
longToBitset(long longValue)63     public static BitSet longToBitset(long longValue) {
64         return BitSet.valueOf(new long[]{longValue});
65     }
66 
67     /**
68      * Convert a BitSet to a long.
69      *
70      * See TODO in {@link #getCapabilityIndex(long)}.
71      */
bitsetToLong(BitSet bitset)72     public static long bitsetToLong(BitSet bitset) {
73         if (bitset == null || bitset.cardinality() == 0) {
74             return 0L;
75         }
76         return bitset.toLongArray()[0];
77     }
78 }
79