KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints2 > ConstraintVariable2


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints2;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
17
18 public abstract class ConstraintVariable2 {
19
20     public static final String JavaDoc TO_STRING= "toString"; //$NON-NLS-1$
21

22     private Object JavaDoc[] fDatas;
23
24     private TypeEquivalenceSet fTypeEquivalenceSet;
25
26     protected final TType fType;
27
28     /**
29      * @param type the type
30      */

31     protected ConstraintVariable2(TType type) {
32         fType= type;
33     }
34
35     public Object JavaDoc getData(String JavaDoc name) {
36         if (fDatas == null) {
37             return null;
38         } else {
39             for (int i= 0; i < fDatas.length; i+= 2) {
40                 String JavaDoc key= (String JavaDoc) fDatas[i];
41                 if (key.equals(name))
42                     return fDatas[i + 1];
43             }
44             return null;
45         }
46     }
47
48     public TypeEquivalenceSet getTypeEquivalenceSet() {
49         return fTypeEquivalenceSet;
50     }
51
52     /**
53      * @return the type binding, or <code>null</code> iff the type constraint variable has no type in the original source (e.g. {@link CollectionElementVariable2})
54      */

55     public TType getType() {
56         return fType;
57     }
58
59     public ITypeSet getTypeEstimate() {
60         Assert.isNotNull(fTypeEquivalenceSet);
61         return fTypeEquivalenceSet.getTypeEstimate();
62     }
63
64     public void setData(String JavaDoc name, Object JavaDoc data) {
65         int index= 0;
66         if (fDatas != null) {
67             while (index < fDatas.length) {
68                 if (name.equals(fDatas[index]))
69                     break;
70                 index+= 2;
71             }
72         }
73         if (data != null) { // add
74
if (fDatas != null) {
75                 if (index == fDatas.length) {
76                     Object JavaDoc[] newTable= new Object JavaDoc[fDatas.length + 2];
77                     System.arraycopy(fDatas, 0, newTable, 0, fDatas.length);
78                     fDatas= newTable;
79                 }
80             } else {
81                 fDatas= new Object JavaDoc[2];
82             }
83             fDatas[index]= name;
84             fDatas[index + 1]= data;
85         } else { // remove
86
if (fDatas != null) {
87                 if (index != fDatas.length) {
88                     int length= fDatas.length - 2;
89                     if (length == 0) {
90                         fDatas= null;
91                     } else {
92                         Object JavaDoc[] newTable= new Object JavaDoc[length];
93                         System.arraycopy(fDatas, 0, newTable, 0, index);
94                         System.arraycopy(fDatas, index + 2, newTable, index, length - index);
95                         fDatas= newTable;
96                     }
97                 }
98             }
99         }
100     }
101
102     public void setTypeEquivalenceSet(TypeEquivalenceSet set) {
103         fTypeEquivalenceSet= set;
104     }
105
106     public String JavaDoc toString() {
107         String JavaDoc toString= (String JavaDoc) getData(TO_STRING);
108         if (toString != null)
109             return toString;
110
111         String JavaDoc name= getClass().getName();
112         int dot= name.lastIndexOf('.');
113         return name.substring(dot + 1) + ": " + fType != null ? fType.getPrettySignature() : "<NONE>"; //$NON-NLS-1$ //$NON-NLS-2$
114
}
115 }
116
Popular Tags