KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > db > Variations


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance.db;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.test.internal.performance.PerformanceTestPlugin;
19
20 /**
21  * The <code>Variations</code> class represents a set of key/value pairs
22  * and is used to tag data stored in the performance database and when
23  * querying for data from the database.
24  */

25 public class Variations extends Properties JavaDoc {
26     
27     private static final long serialVersionUID= 1L;
28
29     /**
30      * Creates an empty set of key/value pairs.
31      */

32     public Variations() {
33         //
34
}
35
36     /**
37      * Creates a Variations object that is populated with a "config" and a "build" key/value pair.
38      * @param configValue a value to store under the config key
39      * @param buildValue a value to store under the build key
40      * @deprecated Use the default constructor instead and fill in key/value pairs explicitely.
41      */

42     public Variations(String JavaDoc configValue, String JavaDoc buildValue) {
43         if (configValue != null)
44             put(PerformanceTestPlugin.CONFIG, configValue);
45         if (buildValue != null)
46             put(PerformanceTestPlugin.BUILD, buildValue);
47     }
48
49     /**
50      * Creates a set of key/value pairs by parsing the given string.
51      * The format of the string must be:
52      * <pre>
53      * key1=value1;key2=value2; .... ; keyn=valuen
54      * </pre>
55      * @param keyValuePairs
56      */

57     public Variations(String JavaDoc keyValuePairs) {
58         parsePairs(keyValuePairs);
59     }
60
61     public String JavaDoc toExactMatchString() {
62         return toDB(this, false);
63     }
64     
65     public String JavaDoc toQueryPattern() {
66         return toDB(this, true);
67     }
68
69     public void parsePairs(String JavaDoc keyvaluepairs) {
70         parse(keyvaluepairs, ";"); //$NON-NLS-1$
71
}
72
73     public void parseDB(String JavaDoc keyvaluepairs) {
74         parse(keyvaluepairs, "|"); //$NON-NLS-1$
75
}
76
77     /**
78      * parsing the given string as key/value pairs and stores them in Variations.
79      * The string's format is an implementation detail of the database.
80      * @param keyvaluepairs
81      * @param separator
82      */

83     private void parse(String JavaDoc keyvaluepairs, String JavaDoc separator) {
84         StringTokenizer JavaDoc st= new StringTokenizer JavaDoc(keyvaluepairs, separator); //$NON-NLS-1$
85
while (st.hasMoreTokens()) {
86             String JavaDoc token= st.nextToken();
87             int i= token.indexOf('=');
88             if (i < 1)
89                 throw new IllegalArgumentException JavaDoc("kev/value pair '" + token + "' is illformed"); //$NON-NLS-1$ //$NON-NLS-2$
90
String JavaDoc value= token.substring(i+1);
91             token= token.substring(0, i);
92             //System.out.println(token + ": <" + value + ">");
93
put(token, value);
94         }
95     }
96     
97     /*
98      * TODO: we need to escape '=' and ';' characters in key/values.
99      */

100     private static String JavaDoc toDB(Properties JavaDoc keyValues, boolean asQuery) {
101         Set JavaDoc set= keyValues.keySet();
102         String JavaDoc[] keys= (String JavaDoc[]) set.toArray(new String JavaDoc[set.size()]);
103         Arrays.sort(keys);
104         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
105         
106         for (int i= 0; i < keys.length; i++) {
107             if (asQuery)
108                 sb.append('%');
109             String JavaDoc key= keys[i];
110             String JavaDoc value= keyValues.getProperty(key);
111             sb.append('|');
112             sb.append(key);
113             sb.append('=');
114             if (value != null)
115                 sb.append(value);
116             sb.append('|');
117         }
118         if (asQuery)
119             sb.append('%');
120         return sb.toString();
121     }
122 }
123
Popular Tags