KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > cfg > PropertyPermutations


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.cfg;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 /**
22  * Generates all possible permutations of properties in a module.
23  */

24 public class PropertyPermutations {
25
26   private int currPermIndex;
27
28   private final int lastProp;
29
30   private final Property[] properties;
31
32   private final String JavaDoc[][] values;
33
34   public PropertyPermutations(Properties properties) {
35     this.properties = properties.toArray();
36     lastProp = this.properties.length - 1;
37     int permCount = countPermutations();
38     values = new String JavaDoc[permCount][];
39     if (this.properties.length > 0) {
40       permute(null, 0);
41       assert (permCount == currPermIndex);
42     } else {
43       values[0] = new String JavaDoc[0];
44     }
45   }
46
47   public Property[] getOrderedProperties() {
48     return properties;
49   }
50
51   /**
52    * Enumerates each permutation as an array of strings such that the index of
53    * each string in the array corresponds to the property at the same index in
54    * the array returned from {@link #getOrderedProperties()}.
55    */

56   public Iterator JavaDoc iterator() {
57     return new Iterator JavaDoc() {
58
59       private int iterPermIndex;
60
61       public boolean hasNext() {
62         return iterPermIndex < values.length;
63       }
64
65       public Object JavaDoc next() {
66         if (!hasNext()) {
67           throw new NoSuchElementException JavaDoc();
68         }
69         return values[iterPermIndex++];
70       }
71
72       public void remove() {
73         throw new UnsupportedOperationException JavaDoc("remove");
74       }
75     };
76   }
77
78   private int countPermutations() {
79     int count = 1;
80     for (int i = 0; i < properties.length; i++) {
81       Property prop = properties[i];
82       String JavaDoc[] options = getPossibilities(prop);
83       assert (options.length > 0);
84       count *= options.length;
85     }
86     return count;
87   }
88
89   private String JavaDoc[] getPossibilities(Property prop) {
90     String JavaDoc activeValue = prop.getActiveValue();
91     if (activeValue != null) {
92       // This property is fixed.
93
//
94
return new String JavaDoc[] {activeValue};
95     } else {
96       // This property is determined on the client.
97
//
98
return prop.getKnownValues();
99     }
100   }
101
102   private void permute(String JavaDoc[] soFar, int whichProp) {
103     Property prop = properties[whichProp];
104     String JavaDoc[] options = getPossibilities(prop);
105
106     for (int i = 0; i < options.length; i++) {
107       String JavaDoc knownValue = options[i];
108
109       String JavaDoc[] nextStep = new String JavaDoc[whichProp + 1];
110       if (whichProp > 0) {
111         System.arraycopy(soFar, 0, nextStep, 0, soFar.length);
112       }
113       nextStep[whichProp] = knownValue;
114
115       if (whichProp < lastProp) {
116         permute(nextStep, whichProp + 1);
117       } else {
118         // Finished this permutation.
119
//
120
values[currPermIndex] = nextStep;
121         ++currPermIndex;
122       }
123     }
124   }
125 }
126
Popular Tags