1 /* 2 * Copyright (C) 2022 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 android.host.systemui; 18 19 import java.util.Objects; 20 21 /** 22 * A simple hash function for use in privacy-sensitive logging. Few bits = lots of collisions. 23 * See NotificationRecordLogger. 24 */ 25 public class SmallHash { 26 // Hashes will be in the range [0, MAX_HASH). 27 public static final int MAX_HASH = (1 << 13); 28 29 /** 30 * @return Small hash of the string, if non-null, or 0 otherwise. 31 */ hash(String in)32 public static int hash(String in) { 33 return hash(Objects.hashCode(in)); 34 } 35 36 /** 37 * Maps in to the range [0, MAX_HASH), keeping similar values distinct. 38 * @param in An arbitrary integer. 39 * @return in mod MAX_HASH, signs chosen to stay in the range [0, MAX_HASH). 40 */ hash(int in)41 public static int hash(int in) { 42 return Math.floorMod(in, MAX_HASH); 43 } 44 } 45