1 /* 2 * Copyright (C) 2016 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 package com.android.compatibility.common.util; 17 18 import java.io.IOException; 19 import java.lang.AutoCloseable; 20 import java.util.Arrays; 21 import java.util.List; 22 23 public abstract class InfoStore implements AutoCloseable { 24 25 protected static final int MAX_STRING_LENGTH = 1000; 26 protected static final int MAX_ARRAY_LENGTH = 1000; 27 protected static final int MAX_LIST_LENGTH = 1000; 28 29 /** 30 * Opens the file for storage and creates the writer. 31 */ 32 abstract void open() throws IOException; 33 34 /** 35 * Closes the writer. 36 */ 37 @Override 38 public abstract void close() throws Exception; 39 40 /** 41 * Start a new group of result. 42 */ 43 abstract void startGroup() throws IOException; 44 45 /** 46 * Start a new group of result with specified name. 47 */ 48 abstract void startGroup(String name) throws IOException; 49 50 /** 51 * Complete adding result to the last started group. 52 */ 53 abstract void endGroup() throws IOException; 54 55 /** 56 * Start a new array of result. 57 */ 58 abstract void startArray() throws IOException; 59 60 /** 61 * Start a new array of result with specified name. 62 */ 63 abstract void startArray(String name) throws IOException; 64 65 /** 66 * Complete adding result to the last started array. 67 */ 68 abstract void endArray() throws IOException; 69 70 /** 71 * Adds a int value to the InfoStore 72 */ 73 abstract void addResult(String name, int value) throws IOException; 74 75 /** 76 * Adds a long value to the InfoStore 77 */ 78 abstract void addResult(String name, long value) throws IOException; 79 80 /** 81 * Adds a float value to the InfoStore 82 */ 83 abstract void addResult(String name, float value) throws IOException; 84 85 /** 86 * Adds a double value to the InfoStore 87 */ 88 abstract void addResult(String name, double value) throws IOException; 89 90 /** 91 * Adds a boolean value to the InfoStore 92 */ 93 abstract void addResult(String name, boolean value) throws IOException; 94 95 /** 96 * Adds a String value to the InfoStore 97 */ 98 abstract void addResult(String name, String value) throws IOException; 99 100 /** 101 * Adds a int array to the InfoStore 102 */ 103 abstract void addArrayResult(String name, int[] array) throws IOException; 104 105 /** 106 * Adds a long array to the InfoStore 107 */ 108 abstract void addArrayResult(String name, long[] array) throws IOException; 109 110 /** 111 * Adds a float array to the InfoStore 112 */ 113 abstract void addArrayResult(String name, float[] array) throws IOException; 114 115 /** 116 * Adds a double array to the InfoStore 117 */ 118 abstract void addArrayResult(String name, double[] array) throws IOException; 119 120 /** 121 * Adds a boolean array to the InfoStore 122 */ 123 abstract void addArrayResult(String name, boolean[] array) throws IOException; 124 125 /** 126 * Adds a List of String to the InfoStore 127 */ 128 abstract void addListResult(String name, List<String> list) throws IOException; 129 130 protected static int[] checkArray(int[] values) { 131 if (values.length > MAX_ARRAY_LENGTH) { 132 return Arrays.copyOf(values, MAX_ARRAY_LENGTH); 133 } else { 134 return values; 135 } 136 } 137 138 protected static long[] checkArray(long[] values) { 139 if (values.length > MAX_ARRAY_LENGTH) { 140 return Arrays.copyOf(values, MAX_ARRAY_LENGTH); 141 } else { 142 return values; 143 } 144 } 145 146 protected static float[] checkArray(float[] values) { 147 if (values.length > MAX_ARRAY_LENGTH) { 148 return Arrays.copyOf(values, MAX_ARRAY_LENGTH); 149 } else { 150 return values; 151 } 152 } 153 154 protected static double[] checkArray(double[] values) { 155 if (values.length > MAX_ARRAY_LENGTH) { 156 return Arrays.copyOf(values, MAX_ARRAY_LENGTH); 157 } else { 158 return values; 159 } 160 } 161 162 protected static boolean[] checkArray(boolean[] values) { 163 if (values.length > MAX_ARRAY_LENGTH) { 164 return Arrays.copyOf(values, MAX_ARRAY_LENGTH); 165 } else { 166 return values; 167 } 168 } 169 170 protected static List<String> checkStringList(List<String> list) { 171 if (list.size() > MAX_LIST_LENGTH) { 172 return list.subList(0, MAX_LIST_LENGTH); 173 } 174 return list; 175 } 176 177 protected static String checkString(String value) { 178 if (value == null || value.isEmpty()) { 179 return ""; 180 } 181 if (value.length() > MAX_STRING_LENGTH) { 182 return value.substring(0, MAX_STRING_LENGTH); 183 } 184 return value; 185 } 186 187 protected static String checkName(String value) { 188 if (value == null || value.isEmpty()) { 189 throw new NullPointerException(); 190 } 191 return value; 192 } 193 194 protected static boolean isDoubleNaNOrInfinite(Double value) { 195 return Double.isNaN(value) || Double.isInfinite(value); 196 } 197 } 198