1 11 12 13 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints; 14 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import org.eclipse.jdt.core.dom.ITypeBinding; 19 20 public class TypeConstraintFactory implements ITypeConstraintFactory { 21 22 private Map fSimpleConstraints= new HashMap (); 23 private Map fOrConstraints= new HashMap (); 24 25 protected static final boolean PRINT_STATS= false; 26 protected int fNrCreated= 0; 27 protected int fNrFiltered= 0; 28 protected int fNrRetrieved= 0; 29 30 private SimpleTypeConstraint createSimpleTypeConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator) { 32 if (fSimpleConstraints.containsKey(v1)){ 33 Map m2= (Map )fSimpleConstraints.get(v1); 34 if (m2.containsKey(v2)){ 35 Map m3= (Map )m2.get(v2); 36 if (m3.containsKey(operator)){ 37 if (PRINT_STATS) fNrRetrieved++; 38 if (PRINT_STATS) dumpStats(); 39 return (SimpleTypeConstraint)m3.get(operator); 40 } else { 41 return storeConstraint(v1, v2, operator, m3); 42 } 43 } else { 44 Map m3= new HashMap (); 45 m2.put(v2, m3); 46 return storeConstraint(v1, v2, operator, m3); 47 } 48 } else { 49 Map m2= new HashMap (); 50 fSimpleConstraints.put(v1, m2); 51 Map m3= new HashMap (); 52 m2.put(v2, m3); 53 return storeConstraint(v1, v2, operator, m3); 54 } 55 } 56 57 private SimpleTypeConstraint storeConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator, Map m3) { 58 SimpleTypeConstraint constraint= new SimpleTypeConstraint(v1, v2, operator); 59 m3.put(operator, constraint); 60 if (PRINT_STATS) fNrCreated++; 61 if (PRINT_STATS) dumpStats(); 62 return constraint; 63 } 64 65 public ITypeConstraint[] createConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator){ 66 if (filter(v1, v2, operator)){ 67 return new ITypeConstraint[0]; 68 } else { 69 return new ITypeConstraint[]{ createSimpleTypeConstraint(v1, v2, operator) }; 70 } 71 } 72 73 public ITypeConstraint[] createSubtypeConstraint(ConstraintVariable v1, ConstraintVariable v2){ 74 return createConstraint(v1, v2, ConstraintOperator.createSubTypeOperator()); 75 } 76 77 public ITypeConstraint[] createStrictSubtypeConstraint(ConstraintVariable v1, ConstraintVariable v2){ 78 return createConstraint(v1, v2, ConstraintOperator.createStrictSubtypeOperator()); 79 } 80 81 public ITypeConstraint[] createEqualsConstraint(ConstraintVariable v1, ConstraintVariable v2){ 82 return createConstraint(v1, v2, ConstraintOperator.createEqualsOperator()); 83 } 84 85 public ITypeConstraint[] createDefinesConstraint(ConstraintVariable v1, ConstraintVariable v2){ 86 return createConstraint(v1, v2, ConstraintOperator.createDefinesOperator()); 87 } 88 89 93 public boolean filter(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator) { 94 if ((v1.getBinding() != null && v1.getBinding().isPrimitive() && 95 v2.getBinding() != null && v2.getBinding().isPrimitive()) || 96 v1 == v2) { 97 if (PRINT_STATS) fNrFiltered++; 98 if (PRINT_STATS) dumpStats(); 99 return true; 100 } 101 return false; 102 } 103 104 109 public CompositeOrTypeConstraint createCompositeOrTypeConstraint(ITypeConstraint[] constraints){ 110 ConstraintVariable left= ((SimpleTypeConstraint)constraints[0]).getLeft(); 111 String bounds= ""; for (int i= 0; i < constraints.length; i++){ 113 ConstraintVariable right= ((SimpleTypeConstraint)constraints[i]).getRight(); 114 ITypeBinding binding= right.getBinding(); 115 String typeName= binding.getQualifiedName(); 116 bounds= bounds + typeName +","; } 118 119 if (fOrConstraints.containsKey(left)){ 120 Map m2= (Map )fOrConstraints.get(left); 121 if (m2.containsKey(bounds)){ 122 if (PRINT_STATS) fNrRetrieved++; 123 if (PRINT_STATS) dumpStats(); 124 return (CompositeOrTypeConstraint)m2.get(bounds); 125 } else { 126 CompositeOrTypeConstraint constraint= new CompositeOrTypeConstraint(constraints); 127 m2.put(bounds, constraint); 128 if (PRINT_STATS) dumpStats(); 129 if (PRINT_STATS) fNrCreated++; 130 return constraint; 131 } 132 } else { 133 Map m2= new HashMap (); 134 fOrConstraints.put(left, m2); 135 CompositeOrTypeConstraint constraint= new CompositeOrTypeConstraint(constraints); 136 m2.put(bounds, constraint); 137 if (PRINT_STATS) dumpStats(); 138 if (PRINT_STATS) fNrCreated++; 139 return constraint; 140 } 141 } 142 143 protected void dumpStats() { 144 System.out.println("Constraints: " + fNrCreated + " created, " + fNrRetrieved + " retrieved, " + fNrFiltered + " filtered"); } 146 147 } 148 | Popular Tags |