]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/typeconstraints/TypeConstraintFactory.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / typeconstraints / TypeConstraintFactory.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
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<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>>();
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         // 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);
40                                 } else {
41                                         return storeConstraint(v1, v2, operator, m3);
42                                 }
43                         } else {
44                                 Map<ConstraintOperator, SimpleTypeConstraint> m3= new HashMap<ConstraintOperator, SimpleTypeConstraint>();
45                                 return v2.generated_7946975818548066197(v1, this, operator, m2, m3);
46                         }
47                 } else {
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>();
51                         m2.put(v2, m3);
52                         return storeConstraint(v1, v2, operator, m3);
53                 }
54         }
55
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();
60                 return constraint;
61         }
62
63         public ITypeConstraint[] createConstraint(ConstraintVariable v1, ConstraintVariable v2, ConstraintOperator operator){
64                 return operator.generated_4909450032677160968(v1, v2, this);
65         }
66
67         public ITypeConstraint[] createSubtypeConstraint(ConstraintVariable v1, ConstraintVariable v2){
68                 return createConstraint(v1, v2, ConstraintOperator.createSubTypeOperator());
69         }
70
71         public ITypeConstraint[] createStrictSubtypeConstraint(ConstraintVariable v1, ConstraintVariable v2){
72                 return createConstraint(v1, v2, ConstraintOperator.createStrictSubtypeOperator());
73         }
74
75         public ITypeConstraint[] createEqualsConstraint(ConstraintVariable v1, ConstraintVariable v2){
76                 return createConstraint(v1, v2, ConstraintOperator.createEqualsOperator());
77         }
78
79         public ITypeConstraint[] createDefinesConstraint(ConstraintVariable v1, ConstraintVariable v2){
80                 return createConstraint(v1, v2, ConstraintOperator.createDefinesOperator());
81         }
82
83         /**
84          * {@inheritDoc}
85          * Avoid creating constraints involving primitive types and self-constraints.
86          */
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()) ||
90                                         v1 == v2) {
91                         if (PRINT_STATS) fNrFiltered++;
92                         if (PRINT_STATS) dumpStats();
93                         return true;
94                 }
95                 return false;
96         }
97
98         /*
99          * (non-Javadoc)
100          *
101          * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraintFactory#createCompositeOrTypeConstraint(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraint[])
102          */
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$
111                 }
112
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);
119                         } else {
120                                 CompositeOrTypeConstraint constraint= new CompositeOrTypeConstraint(constraints);
121                                 m2.put(bounds, constraint);
122                                 if (PRINT_STATS) dumpStats();
123                                 if (PRINT_STATS) fNrCreated++;
124                                 return constraint;
125                         }
126                 } else {
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++;
133                         return constraint;
134                 }
135         }
136
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$
139         }
140
141 }