1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
13 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints;
15 import java.util.HashMap;
18 import org.eclipse.jdt.core.dom.ITypeBinding;
20 public class TypeConstraintFactory implements ITypeConstraintFactory {
22 private Map<ConstraintVariable, Map<ConstraintVariable, Map<ConstraintOperator, SimpleTypeConstraint>>> fSimpleConstraints= new HashMap<ConstraintVariable, Map<ConstraintVariable, Map<ConstraintOperator, SimpleTypeConstraint>>>();
23 private Map<ConstraintVariable, Map<String, CompositeOrTypeConstraint>> fOrConstraints= new HashMap<ConstraintVariable, Map<String, CompositeOrTypeConstraint>>();
25 protected static final boolean PRINT_STATS= false;
26 protected int fNrCreated= 0;
27 protected int fNrFiltered= 0;
28 protected int fNrRetrieved= 0;
30 // Only to be called by the createXXXConstraint() methods
31 SimpleTypeConstraint createSimpleTypeConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator) {
32 if (fSimpleConstraints.containsKey(v1)){
33 Map<ConstraintVariable, Map<ConstraintOperator, SimpleTypeConstraint>> m2= fSimpleConstraints.get(v1);
34 if (m2.containsKey(v2)){
35 Map<ConstraintOperator, SimpleTypeConstraint> m3= m2.get(v2);
36 if (m3.containsKey(operator)){
37 if (PRINT_STATS) fNrRetrieved++;
38 if (PRINT_STATS) dumpStats();
39 return m3.get(operator);
41 return storeConstraint(v1, v2, operator, m3);
44 Map<ConstraintOperator, SimpleTypeConstraint> m3= new HashMap<ConstraintOperator, SimpleTypeConstraint>();
45 return v2.generated_7946975818548066197(v1, this, operator, m2, m3);
48 Map<ConstraintVariable, Map<ConstraintOperator, SimpleTypeConstraint>> m2= new HashMap<ConstraintVariable, Map<ConstraintOperator, SimpleTypeConstraint>>();
49 fSimpleConstraints.put(v1, m2);
50 Map<ConstraintOperator, SimpleTypeConstraint> m3= new HashMap<ConstraintOperator, SimpleTypeConstraint>();
52 return storeConstraint(v1, v2, operator, m3);
56 SimpleTypeConstraint storeConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator, Map<ConstraintOperator, SimpleTypeConstraint> m3) {
57 SimpleTypeConstraint constraint= operator.generated_6455140397054150679(v1, v2, m3);
58 if (PRINT_STATS) fNrCreated++;
59 if (PRINT_STATS) dumpStats();
63 public ITypeConstraint[] createConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator){
64 return operator.generated_4909450032677160968(v1, v2, this);
67 public ITypeConstraint[] createSubtypeConstraint(ConstraintVariable v1, ConstraintVariable v2){
68 return createConstraint(v1, v2, ConstraintOperator.createSubTypeOperator());
71 public ITypeConstraint[] createStrictSubtypeConstraint(ConstraintVariable v1, ConstraintVariable v2){
72 return createConstraint(v1, v2, ConstraintOperator.createStrictSubtypeOperator());
75 public ITypeConstraint[] createEqualsConstraint(ConstraintVariable v1, ConstraintVariable v2){
76 return createConstraint(v1, v2, ConstraintOperator.createEqualsOperator());
79 public ITypeConstraint[] createDefinesConstraint(ConstraintVariable v1, ConstraintVariable v2){
80 return createConstraint(v1, v2, ConstraintOperator.createDefinesOperator());
85 * Avoid creating constraints involving primitive types and self-constraints.
87 public boolean filter(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator) {
88 if ((v1.getBinding() != null && v1.getBinding().isPrimitive() &&
89 v2.getBinding() != null && v2.getBinding().isPrimitive()) ||
91 if (PRINT_STATS) fNrFiltered++;
92 if (PRINT_STATS) dumpStats();
101 * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraintFactory#createCompositeOrTypeConstraint(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraint[])
103 public CompositeOrTypeConstraint createCompositeOrTypeConstraint(ITypeConstraint[] constraints){
104 ConstraintVariable left= ((SimpleTypeConstraint)constraints[0]).getLeft();
105 String bounds= ""; //$NON-NLS-1$
106 for (int i= 0; i < constraints.length; i++){
107 ConstraintVariable right= ((SimpleTypeConstraint)constraints[i]).getRight();
108 ITypeBinding binding= right.getBinding();
109 String typeName= binding.getQualifiedName();
110 bounds= bounds + typeName +","; //$NON-NLS-1$
113 if (fOrConstraints.containsKey(left)){
114 Map<String, CompositeOrTypeConstraint> m2= fOrConstraints.get(left);
115 if (m2.containsKey(bounds)){
116 if (PRINT_STATS) fNrRetrieved++;
117 if (PRINT_STATS) dumpStats();
118 return m2.get(bounds);
120 CompositeOrTypeConstraint constraint= new CompositeOrTypeConstraint(constraints);
121 m2.put(bounds, constraint);
122 if (PRINT_STATS) dumpStats();
123 if (PRINT_STATS) fNrCreated++;
127 Map<String, CompositeOrTypeConstraint> m2= new HashMap<String, CompositeOrTypeConstraint>();
128 fOrConstraints.put(left, m2);
129 CompositeOrTypeConstraint constraint= new CompositeOrTypeConstraint(constraints);
130 m2.put(bounds, constraint);
131 if (PRINT_STATS) dumpStats();
132 if (PRINT_STATS) fNrCreated++;
137 protected void dumpStats() {
138 System.out.println("Constraints: " + fNrCreated + " created, " + fNrRetrieved + " retrieved, " + fNrFiltered + " filtered"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$