1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2007, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package ohos.global.icu.dev.test.util; 11 12 import ohos.global.icu.impl.Utility; 13 14 15 /** 16 * @author srl 17 * 18 * analog of FieldsSet in C++ 19 */ 20 21 public class FieldsSet { 22 public static final int NO_ENUM = -1; 23 FieldsSet(int whichEnum, int fieldsCount)24 protected FieldsSet(int whichEnum, int fieldsCount) { 25 if (fieldsCount <= 0 && whichEnum != NO_ENUM) { 26 fieldsCount = DebugUtilities.enumCount(whichEnum); 27 } 28 fEnum = whichEnum; 29 fFieldsCount = fieldsCount; 30 if(fieldsCount<0) { 31 throw new InternalError("Preposterous field count " + fieldsCount); 32 } 33 fValues = new int[fFieldsCount]; 34 fIsSet = new boolean[fFieldsCount]; 35 clear(); 36 } 37 38 protected int fEnum = NO_ENUM; 39 40 protected int fFieldsCount = 0; 41 42 protected int fValues[] = null; 43 44 protected boolean fIsSet[] = null; 45 clear()46 public void clear() { 47 for (int i = 0; i < fFieldsCount; i++) { 48 clear(i); 49 } 50 } 51 clear(int field)52 public void clear(int field) { 53 fValues[field] = -1; 54 fIsSet[field] = false; 55 } 56 set(int field, int amount)57 public void set(int field, int amount) { 58 fValues[field] = amount; 59 fIsSet[field] = true; 60 } 61 isSet(int field)62 public boolean isSet(int field) { 63 return fIsSet[field]; 64 } 65 get(int field)66 public int get(int field) { 67 if (fIsSet[field]) { 68 return fValues[field]; 69 } else { 70 return -1; 71 } 72 } 73 isSameType(FieldsSet other)74 public boolean isSameType(FieldsSet other) { 75 return ((other.fEnum == fEnum) && (other.fFieldsCount == fFieldsCount)); 76 } 77 fieldCount()78 public int fieldCount() { 79 return fFieldsCount; 80 } 81 82 /** 83 * @param other "expected" set to match against 84 * @return a formatted string listing which fields are set in this, with the 85 * comparison made agaainst those fields in other, or, 'null' if there is no difference. 86 */ diffFrom(FieldsSet other)87 public String diffFrom(FieldsSet other) { 88 StringBuffer str = new StringBuffer(); 89 if(!isSameType(other)) { 90 throw new IllegalArgumentException("U_ILLEGAL_ARGUMENT_ERROR: FieldsSet of a different type!"); 91 } 92 for (int i=0; i<fieldCount(); i++) { 93 if (isSet(i)) { 94 int myVal = get(i); 95 int theirVal = other.get(i); 96 97 if(fEnum != NO_ENUM) { 98 String fieldName = DebugUtilities.enumString(fEnum, i); 99 100 String aval = Integer.toString(myVal); 101 String bval = Integer.toString(theirVal); 102 103 str.append(fieldName +"="+aval+" not "+bval+", "); 104 } else { 105 str.append(Integer.toString(i) + "=" + myVal+" not " + theirVal+", "); 106 } 107 } 108 } 109 if(str.length()==0) { 110 return null; 111 } 112 return str.toString(); 113 } 114 115 /** 116 * @param str string to parse 117 * @param status formatted string for status 118 */ parseFrom(String str)119 public int parseFrom(String str) { 120 return parseFrom(str, null); 121 } 122 parseFrom(String str, FieldsSet inheritFrom)123 public int parseFrom(String str, FieldsSet inheritFrom) { 124 int goodFields = 0; 125 126 String[] fields = Utility.split(str, ','); 127 for(int i=0;i<fields.length;i++) { 128 String fieldStr = fields[i]; 129 String kv[] = Utility.split(fieldStr, '='); 130 if(kv.length < 1 || kv.length > 2) { 131 throw new InternalError("split around '=' failed: " + fieldStr); 132 } 133 String key = kv[0]; 134 String value = ""; 135 if(kv.length>1) { 136 value = kv[1]; 137 } 138 139 int field = handleParseName(inheritFrom, key, value); 140 if(field != -1) { 141 handleParseValue(inheritFrom, field, value); 142 goodFields++; 143 } 144 } 145 146 return goodFields; 147 } 148 149 /** 150 * Callback interface for subclass. This function is called when parsing a 151 * field name, such as "MONTH" in "MONTH=4". Base implementation is to 152 * lookup the enum value using udbg_* utilities, or else as an integer if 153 * enum is not available. 154 * 155 * If there is a special directive, the implementer can catch it here and 156 * return -1 after special processing completes. 157 * 158 * @param inheritFrom the set inheriting from - may be null. 159 * @param name the field name (key side) 160 * @param substr the string in question (value side) 161 * @param status error status - set to error for failure. 162 * @return field number, or negative if field should be skipped. 163 */ handleParseName(FieldsSet inheritFrom, String name, String substr)164 protected int handleParseName(FieldsSet inheritFrom, String name, 165 String substr) { 166 int field = -1; 167 if(fEnum != NO_ENUM) { 168 field = DebugUtilities.enumByString(fEnum, name); 169 } 170 if(field < 0) { 171 field = Integer.parseInt(name); 172 } 173 return field; 174 } 175 176 /** 177 * Callback interface for subclass. Base implementation is to call 178 * parseValueDefault(...) 179 * 180 * @param inheritFrom the set inheriting from - may be null. 181 * @param field which field is being parsed 182 * @param substr the string in question (value side) 183 * @param status error status - set to error for failure. 184 * @see parseValueDefault 185 */ handleParseValue(FieldsSet inheritFrom, int field, String substr)186 protected void handleParseValue(FieldsSet inheritFrom, int field, 187 String substr) { 188 parseValueDefault(inheritFrom, field, substr); 189 } 190 191 /** 192 * the default implementation for handleParseValue. Base implementation is 193 * to parse a decimal integer value, or inherit from inheritFrom if the 194 * string is 0-length. Implementations of this function should call 195 * set(field,...) on successful parse. 196 * 197 * @see handleParseValue 198 */ parseValueDefault(FieldsSet inheritFrom, int field, String substr)199 protected void parseValueDefault(FieldsSet inheritFrom, int field, 200 String substr) { 201 if(substr.length()==0) { 202 if(inheritFrom == null) { 203 throw new InternalError("Trying to inherit from field " + field + " but inheritFrom is null"); 204 } 205 if(!inheritFrom.isSet(field)) { 206 throw new InternalError("Trying to inherit from field " + field + " but inheritFrom["+field+"] is not set"); 207 } 208 set(field,inheritFrom.get(field)); 209 } else { 210 int value = Integer.parseInt(substr); 211 set(field, value); 212 } 213 } 214 215 /** 216 * convenience implementation for handleParseValue attempt to load a value 217 * from an enum value using udbg_enumByString() if fails, will call 218 * parseValueDefault() 219 * 220 * @see handleParseValue 221 */ parseValueEnum(int type, FieldsSet inheritFrom, int field, String substr)222 protected void parseValueEnum(int type, FieldsSet inheritFrom, int field, 223 String substr) { 224 int value = DebugUtilities.enumByString(type, substr); 225 if(value>=0) { 226 set(field,value); 227 return; 228 } 229 parseValueDefault(inheritFrom, field, substr); 230 } 231 fieldName(int field)232 public String fieldName(int field) { 233 return (fEnum!=NO_ENUM)?DebugUtilities.enumString(fEnum, field):Integer.toString(field); 234 } 235 toString()236 public String toString() { 237 String str = getClass().getName()+" ["+fFieldsCount+"," 238 +(fEnum!=NO_ENUM?DebugUtilities.typeString(fEnum):Integer.toString(fEnum))+"]: "; 239 for(int i=0;i<fFieldsCount;i++) { 240 if(isSet(i)) { 241 str = str + fieldName(i)+"="+get(i)+","; 242 } 243 } 244 return str; 245 } 246 } 247