1 /* 2 * Copyright (C) 2014 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 dexfuzz; 18 19 import java.util.ArrayList; 20 import java.util.HashMap; 21 import java.util.List; 22 import java.util.Map; 23 24 /** 25 * A wrapper for a dictionary tracking what mutations have been performed. 26 */ 27 public class MutationStats { 28 29 public static class StatNotFoundException extends RuntimeException { 30 private static final long serialVersionUID = -7038515184655168470L; 31 } 32 33 private Map<String,Long> stats; 34 private List<String> statsOrder; 35 MutationStats()36 public MutationStats() { 37 stats = new HashMap<String,Long>(); 38 statsOrder = new ArrayList<String>(); 39 } 40 incrementStat(String statName)41 public void incrementStat(String statName) { 42 increaseStat(statName, 1); 43 } 44 45 /** 46 * Increase the named stat by the specified amount. 47 */ increaseStat(String statName, long amt)48 public void increaseStat(String statName, long amt) { 49 if (!stats.containsKey(statName)) { 50 stats.put(statName, 0L); 51 statsOrder.add(statName); 52 } 53 stats.put(statName, stats.get(statName) + amt); 54 } 55 56 /** 57 * Get a string representing the collected stats - looks like a JSON dictionary. 58 */ getStatsString()59 public String getStatsString() { 60 StringBuilder builder = new StringBuilder(); 61 builder.append("{"); 62 boolean first = true; 63 for (String statName : statsOrder) { 64 if (!first) { 65 builder.append(", "); 66 } else { 67 first = false; 68 } 69 builder.append("\"").append(statName).append("\": ").append(stats.get(statName)); 70 } 71 builder.append("}"); 72 return builder.toString(); 73 } 74 } 75