1 /* 2 * Copyright 2017 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.privacy.internal.rappor; 18 19 import android.privacy.DifferentialPrivacyConfig; 20 import android.text.TextUtils; 21 22 import com.android.internal.util.Preconditions; 23 24 /** 25 * A class to store {@link RapporEncoder} config. 26 * 27 * @hide 28 */ 29 public class RapporConfig implements DifferentialPrivacyConfig { 30 31 private static final String ALGORITHM_NAME = "Rappor"; 32 33 final String mEncoderId; 34 final int mNumBits; 35 final double mProbabilityF; 36 final double mProbabilityP; 37 final double mProbabilityQ; 38 final int mNumCohorts; 39 final int mNumBloomHashes; 40 41 /** 42 * Constructor for {@link RapporConfig}. 43 * 44 * @param encoderId Unique id for encoder. 45 * @param numBits Number of bits to be encoded in Rappor algorithm. 46 * @param probabilityF Probability F that used in Rappor algorithm. This will be 47 * quantized to the nearest 1/128. 48 * @param probabilityP Probability P that used in Rappor algorithm. 49 * @param probabilityQ Probability Q that used in Rappor algorithm. 50 * @param numCohorts Number of cohorts that used in Rappor algorithm. 51 * @param numBloomHashes Number of bloom hashes that used in Rappor algorithm. 52 */ RapporConfig(String encoderId, int numBits, double probabilityF, double probabilityP, double probabilityQ, int numCohorts, int numBloomHashes)53 public RapporConfig(String encoderId, int numBits, double probabilityF, 54 double probabilityP, double probabilityQ, int numCohorts, int numBloomHashes) { 55 Preconditions.checkArgument(!TextUtils.isEmpty(encoderId), "encoderId cannot be empty"); 56 this.mEncoderId = encoderId; 57 Preconditions.checkArgument(numBits > 0, "numBits needs to be > 0"); 58 this.mNumBits = numBits; 59 Preconditions.checkArgument(probabilityF >= 0 && probabilityF <= 1, 60 "probabilityF must be in range [0.0, 1.0]"); 61 this.mProbabilityF = probabilityF; 62 Preconditions.checkArgument(probabilityP >= 0 && probabilityP <= 1, 63 "probabilityP must be in range [0.0, 1.0]"); 64 this.mProbabilityP = probabilityP; 65 Preconditions.checkArgument(probabilityQ >= 0 && probabilityQ <= 1, 66 "probabilityQ must be in range [0.0, 1.0]"); 67 this.mProbabilityQ = probabilityQ; 68 Preconditions.checkArgument(numCohorts > 0, "numCohorts needs to be > 0"); 69 this.mNumCohorts = numCohorts; 70 Preconditions.checkArgument(numBloomHashes > 0, "numBloomHashes needs to be > 0"); 71 this.mNumBloomHashes = numBloomHashes; 72 } 73 74 @Override getAlgorithm()75 public String getAlgorithm() { 76 return ALGORITHM_NAME; 77 } 78 79 @Override toString()80 public String toString() { 81 return String.format( 82 "EncoderId: %s, NumBits: %d, ProbabilityF: %.3f, ProbabilityP: %.3f" 83 + ", ProbabilityQ: %.3f, NumCohorts: %d, NumBloomHashes: %d", 84 mEncoderId, mNumBits, mProbabilityF, mProbabilityP, mProbabilityQ, 85 mNumCohorts, mNumBloomHashes); 86 } 87 } 88