< prev index next >

src/com/sun/javatest/util/PropertyArray.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 210             cmp = key.compareTo(e);
 211             if (cmp < 0)
 212                 upper = mid - 2;
 213             else if (cmp > 0)
 214                 lower = mid + 2;
 215             else
 216                 return data[mid+1];
 217         }
 218 
 219         // did not find an exact match
 220         return null;
 221     }
 222 
 223     /**
 224      * Remove an entry from an array of properties.
 225      * @param data an array of sequential name value properties
 226      * @param key the name of the entry to be removed
 227      * @return an array that does not contain the named property
 228      */
 229     public static String[] remove(String[] data, String key) {
 230         Vector vec = copyOutOf(data);
 231         int lower = 0;
 232         int upper = vec.size() - 2;
 233         int mid = 0;
 234         String old = null;
 235 
 236         if (upper < 0) {
 237             // no data yet
 238             return data;
 239         }
 240 
 241         // goes at the end
 242         String last = (String)vec.elementAt(upper);
 243         int cmp = key.compareTo(last);
 244         if (cmp > 0) {
 245             return data;
 246         }
 247 
 248         while (lower <= upper) {
 249             // in next line, take care to ensure that mid is always even
 250             mid = lower + ((upper - lower) / 4) * 2;
 251             String e = (String)(vec.elementAt(mid));
 252             cmp = key.compareTo(e);
 253             if (cmp < 0) {
 254                 upper = mid - 2;
 255             }
 256             else if (cmp > 0) {
 257                 lower = mid + 2;
 258             }
 259             else {
 260                 // strings equal, zap key and value
 261                 vec.removeElementAt(mid);
 262                 old = (String)(vec.elementAt(mid));
 263                 vec.removeElementAt(mid);
 264                 break;
 265             }
 266         }
 267 
 268         String[] outData = new String[vec.size()];
 269         vec.copyInto(outData);
 270         return outData;
 271     }
 272 
 273     /**
 274      * Get a standard Map object from an array of properties.
 275      * @param data an array of sequential name value properties
 276      * @return a Map object containing data from the array
 277      */
 278     public static Map<String, String> getProperties(String[] data) {
 279         Map<String, String> props = new HashMap<>();
 280 
 281         if (data != null && data.length > 0) {
 282             for (int i = 0; i < data.length; i+=2) {


 356      * @see #save(String[], Writer)
 357      */
 358     public static String[] load(Reader in) throws IOException {
 359 
 360         Vector<String> v = Properties.load0(in, true);
 361         Vector<String> sorted = new Vector<>(v.size());
 362         for (int i = 0; i < v.size(); i+=2) {
 363             insert(sorted, v.elementAt(i), v.elementAt(i + 1));
 364         }
 365 
 366         String[] data = new String[sorted.size()];
 367         sorted.copyInto(data);
 368         return data;
 369     }
 370 
 371     /**
 372      * Enumerate the properties in an array.
 373      * @param props an array of sequential name value properties
 374      * @return an enumeration of the properties in the array
 375      */
 376     public static Enumeration enumerate(final String[] props) {
 377         return new Enumeration() {
 378             int pos = 0;
 379 
 380             public boolean hasMoreElements() {
 381                 return (props != null && pos < props.length);
 382             }
 383 
 384             public Object nextElement() {
 385                 if (props == null || pos >= props.length) {
 386                    return null;
 387                 }
 388                 else {
 389                    String current = props[pos];
 390                    pos += 2;
 391                    return current;
 392                 }
 393             }
 394         };
 395     }
 396 
 397     // --------------- INSTANCE METHODS ----------------
 398 
 399     /**
 400      * Get the data in this PropertyArray as a standard Properties object.
 401      * @return a Properties object containing the same data as this PropertyArray
 402      */
 403     public Map<String, String> getProperties() {
 404         return getProperties(dataA);




 210             cmp = key.compareTo(e);
 211             if (cmp < 0)
 212                 upper = mid - 2;
 213             else if (cmp > 0)
 214                 lower = mid + 2;
 215             else
 216                 return data[mid+1];
 217         }
 218 
 219         // did not find an exact match
 220         return null;
 221     }
 222 
 223     /**
 224      * Remove an entry from an array of properties.
 225      * @param data an array of sequential name value properties
 226      * @param key the name of the entry to be removed
 227      * @return an array that does not contain the named property
 228      */
 229     public static String[] remove(String[] data, String key) {
 230         Vector<String> vec = copyOutOf(data);
 231         int lower = 0;
 232         int upper = vec.size() - 2;
 233         int mid = 0;
 234         String old = null;
 235 
 236         if (upper < 0) {
 237             // no data yet
 238             return data;
 239         }
 240 
 241         // goes at the end
 242         String last = vec.elementAt(upper);
 243         int cmp = key.compareTo(last);
 244         if (cmp > 0) {
 245             return data;
 246         }
 247 
 248         while (lower <= upper) {
 249             // in next line, take care to ensure that mid is always even
 250             mid = lower + ((upper - lower) / 4) * 2;
 251             String e = (vec.elementAt(mid));
 252             cmp = key.compareTo(e);
 253             if (cmp < 0) {
 254                 upper = mid - 2;
 255             }
 256             else if (cmp > 0) {
 257                 lower = mid + 2;
 258             }
 259             else {
 260                 // strings equal, zap key and value
 261                 vec.removeElementAt(mid);
 262                 old = vec.elementAt(mid);
 263                 vec.removeElementAt(mid);
 264                 break;
 265             }
 266         }
 267 
 268         String[] outData = new String[vec.size()];
 269         vec.copyInto(outData);
 270         return outData;
 271     }
 272 
 273     /**
 274      * Get a standard Map object from an array of properties.
 275      * @param data an array of sequential name value properties
 276      * @return a Map object containing data from the array
 277      */
 278     public static Map<String, String> getProperties(String[] data) {
 279         Map<String, String> props = new HashMap<>();
 280 
 281         if (data != null && data.length > 0) {
 282             for (int i = 0; i < data.length; i+=2) {


 356      * @see #save(String[], Writer)
 357      */
 358     public static String[] load(Reader in) throws IOException {
 359 
 360         Vector<String> v = Properties.load0(in, true);
 361         Vector<String> sorted = new Vector<>(v.size());
 362         for (int i = 0; i < v.size(); i+=2) {
 363             insert(sorted, v.elementAt(i), v.elementAt(i + 1));
 364         }
 365 
 366         String[] data = new String[sorted.size()];
 367         sorted.copyInto(data);
 368         return data;
 369     }
 370 
 371     /**
 372      * Enumerate the properties in an array.
 373      * @param props an array of sequential name value properties
 374      * @return an enumeration of the properties in the array
 375      */
 376     public static Enumeration<String> enumerate(final String[] props) {
 377         return new Enumeration<String>() {
 378             int pos = 0;
 379 
 380             public boolean hasMoreElements() {
 381                 return (props != null && pos < props.length);
 382             }
 383 
 384             public String nextElement() {
 385                 if (props == null || pos >= props.length) {
 386                    return null;
 387                 }
 388                 else {
 389                    String current = props[pos];
 390                    pos += 2;
 391                    return current;
 392                 }
 393             }
 394         };
 395     }
 396 
 397     // --------------- INSTANCE METHODS ----------------
 398 
 399     /**
 400      * Get the data in this PropertyArray as a standard Properties object.
 401      * @return a Properties object containing the same data as this PropertyArray
 402      */
 403     public Map<String, String> getProperties() {
 404         return getProperties(dataA);


< prev index next >