1 16 package com.google.gwt.dev.cfg; 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 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 [][] 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 [permCount][]; 39 if (this.properties.length > 0) { 40 permute(null, 0); 41 assert (permCount == currPermIndex); 42 } else { 43 values[0] = new String [0]; 44 } 45 } 46 47 public Property[] getOrderedProperties() { 48 return properties; 49 } 50 51 56 public Iterator iterator() { 57 return new Iterator () { 58 59 private int iterPermIndex; 60 61 public boolean hasNext() { 62 return iterPermIndex < values.length; 63 } 64 65 public Object next() { 66 if (!hasNext()) { 67 throw new NoSuchElementException (); 68 } 69 return values[iterPermIndex++]; 70 } 71 72 public void remove() { 73 throw new UnsupportedOperationException ("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 [] options = getPossibilities(prop); 83 assert (options.length > 0); 84 count *= options.length; 85 } 86 return count; 87 } 88 89 private String [] getPossibilities(Property prop) { 90 String activeValue = prop.getActiveValue(); 91 if (activeValue != null) { 92 return new String [] {activeValue}; 95 } else { 96 return prop.getKnownValues(); 99 } 100 } 101 102 private void permute(String [] soFar, int whichProp) { 103 Property prop = properties[whichProp]; 104 String [] options = getPossibilities(prop); 105 106 for (int i = 0; i < options.length; i++) { 107 String knownValue = options[i]; 108 109 String [] nextStep = new String [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 values[currPermIndex] = nextStep; 121 ++currPermIndex; 122 } 123 } 124 } 125 } 126 | Popular Tags |