1 /* 2 * Copyright (C) 2019 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 android.util.ArrayMap; 20 21 import java.lang.reflect.Array; 22 import java.util.Iterator; 23 import java.util.Map; 24 25 /** 26 * Counts occurrences of keys of type K, where K is a custom key class. This is most commonly used 27 * for counting occurrences of composite keys. 28 * @param <K> Custom key type K must override {@link Object#hashCode()}, 29 * {@link Object#equals(Object)}, and {@link Object#toString()}. These methods can be auto-generated 30 * by IntelliJ by right clicking inside your class and clicking "Generate...". 31 */ 32 public class ObjectCounter<K> implements Iterable<Map.Entry<K, Integer>> { 33 private ArrayMap<K, Integer> mCounter; 34 ObjectCounter()35 public ObjectCounter() { 36 mCounter = new ArrayMap<>(); 37 } 38 39 /** 40 * Removes all keys and counts from this counter. 41 */ clear()42 public void clear() { 43 mCounter.clear(); 44 } 45 46 /** 47 * Returns the number of keys in this counter. 48 */ size()49 public int size() { 50 return mCounter.size(); 51 } 52 53 /** 54 * Gets the number of occurrences of a key 55 */ getCount(K key)56 public int getCount(K key) { 57 return mCounter.getOrDefault(key, 0); 58 } 59 60 /** 61 * Increments the count of a key by 1. 62 */ increment(K key)63 public void increment(K key) { 64 add(key, 1); 65 } 66 67 /** 68 * Increments the count of a key by <code>count</code>. 69 */ add(K key, int count)70 public void add(K key, int count) { 71 int curCount = getCount(key); 72 mCounter.put(key, curCount + count); 73 } 74 75 /** 76 * Returns a string representation of this counter object suitable for dump(). Note that the 77 * type K must have an overridden implementation of {@link Object#toString()}. 78 */ 79 @Override toString()80 public String toString() { 81 return mCounter.toString(); 82 } 83 84 /** 85 * Iterates over all (key, count) pairs. 86 */ 87 @Override iterator()88 public Iterator<Map.Entry<K, Integer>> iterator() { 89 return mCounter.entrySet().iterator(); 90 } 91 92 /** 93 * Converter function that converts a single (key, count) pair to a Protobuf object. 94 * @param <I> The type of the key. 95 * @param <O> The type of the Protobuf output. 96 */ 97 public interface ProtobufConverter<I, O> { 98 /** 99 * Converter function that converts a single (key, count) pair to a Protobuf object. 100 * @param key the key that we are counting occurrences for 101 * @param count the number of occurrences for this key 102 * @return the Protobuf output 103 */ convert(I key, int count)104 O convert(I key, int count); 105 } 106 107 /** 108 * Converts this object to a custom Protobuf representation. 109 * @param protoClass the class object for the Protobuf type. 110 * @param converter a conversion function. 111 * @param <T> the type of the Protobuf output. 112 * @return an array of Protobuf representation of buckets generated by the converter function. 113 */ toProto(Class<T> protoClass, ProtobufConverter<K, T> converter)114 public <T> T[] toProto(Class<T> protoClass, ProtobufConverter<K, T> converter) { 115 @SuppressWarnings("unchecked") 116 T[] output = (T[]) Array.newInstance(protoClass, size()); 117 int i = 0; 118 for (Map.Entry<K, Integer> entry : this) { 119 output[i] = converter.convert(entry.getKey(), entry.getValue()); 120 i++; 121 } 122 return output; 123 } 124 } 125