huangcm
2025-08-25 2f2fd745743ad500687c6985119d523146531958
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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.server.wifi.util;
 
import android.util.SparseIntArray;
 
import com.android.server.wifi.nano.WifiMetricsProto.Int32Count;
 
import java.lang.reflect.Array;
import java.util.Iterator;
 
/**
 * Utility class for counting occurrences of int keys using an int counter.
 * Note: this class can also be used for counting occurrences of enum values. Just define a new
 * Protobuf message, and call {@link #toProto(Class, ProtobufConverter)} with a
 * {@link ProtobufConverter} that populates your custom Protobuf message type.
 */
public class IntCounter extends SparseIntArray implements Iterable<IntCounter.KeyCount> {
 
    /**
     * A class to represent the number of occurrences for an int key.
     */
    public static class KeyCount {
        public int key;
        public int count;
 
        public KeyCount(int key, int count) {
            this.key = key;
            this.count = count;
        }
    }
 
    /**
     * Calls to {@link #add(int, int)}/{@link #increment(int)} for all keys < keyLowerBound are
     * instead attributed to keyLowerBound.
     */
    public final int keyLowerBound;
    /**
     * Calls to {@link #add(int, int)}/{@link #increment(int)} for all keys > keyUpperBound are
     * instead attributed to keyUpperBound.
     */
    public final int keyUpperBound;
 
    public IntCounter() {
        this(Integer.MIN_VALUE, Integer.MAX_VALUE);
    }
 
    /**
     * Clamps keys to the range between keyLowerBound and keyUpperBound. See {@link #keyLowerBound}
     * and {@link #keyUpperBound}.
     */
    public IntCounter(int keyLowerBound, int keyUpperBound) {
        this.keyLowerBound = keyLowerBound;
        this.keyUpperBound = keyUpperBound;
    }
 
    /**
     * Increments the count of a key by 1.
     */
    public void increment(int key) {
        add(key, 1);
    }
 
    /**
     * Increments the count of a key by <code>count</code>.
     */
    public void add(int key, int count) {
        key = Math.max(keyLowerBound, Math.min(key, keyUpperBound));
        int curCount = get(key); // returns 0 if key not found
        put(key, curCount + count);
    }
 
    /**
     * Iterates over all (key, count) pairs.
     */
    @Override
    public Iterator<KeyCount> iterator() {
        return new Iterator<KeyCount>() {
            private int mIndex = 0;
 
            @Override
            public boolean hasNext() {
                return mIndex < size();
            }
 
            @Override
            public KeyCount next() {
                KeyCount kc = new KeyCount(keyAt(mIndex), valueAt(mIndex));
                mIndex++;
                return kc;
            }
        };
    }
 
    /**
     * Converter function that converts a single (key, count) pair to a Protobuf object.
     * @param <T> the type of the Protobuf output.
     */
    public interface ProtobufConverter<T> {
        /**
         * Converter function that converts a single (key, count) pair to a Protobuf object.
         * @param key the key that we are counting occurrences for
         * @param count the number of occurrences for this key
         * @return the Protobuf output
         */
        T convert(int key, int count);
    }
 
    /**
     * Converts this object to a custom Protobuf representation.
     * @param protoClass the class object for the Protobuf type.
     * @param converter a conversion function.
     * @param <T> the type of the Protobuf output.
     * @return an array of Protobuf representation of buckets generated by the converter function.
     */
    public <T> T[] toProto(Class<T> protoClass, ProtobufConverter<T> converter) {
        @SuppressWarnings("unchecked")
        T[] output = (T[]) Array.newInstance(protoClass, size());
        int i = 0;
        for (KeyCount kc : this) {
            output[i] = converter.convert(kc.key, kc.count);
            i++;
        }
        return output;
    }
 
    /**
     * Converts this object to a standard Protobuf representation.
     */
    public Int32Count[] toProto() {
        return toProto(Int32Count.class, (key, count) -> {
            Int32Count entry = new Int32Count();
            entry.key = key;
            entry.count = count;
            return entry;
        });
    }
}