]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/LoggedNewNameQueries.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / reorg / LoggedNewNameQueries.java
1 /*******************************************************************************
2  * Copyright (c) 2006, 2008 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 package org.eclipse.jdt.internal.corext.refactoring.reorg;
12
13 import org.eclipse.core.resources.IResource;
14
15 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
16
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.IPackageFragment;
19 import org.eclipse.jdt.core.IPackageFragmentRoot;
20
21 /**
22  * Logged implementation of new name queries.
23  *
24  * @since 3.3
25  */
26 public final class LoggedNewNameQueries implements INewNameQueries {
27
28         /** Default implementation of a new name query */
29         private final class NewNameQuery implements INewNameQuery {
30
31                 /** The name */
32                 private final String fName;
33
34                 /** The object */
35                 private final Object fObject;
36
37                 /**
38                  * Creates a new new name query.
39                  *
40                  * @param object
41                  *            the object
42                  * @param name
43                  *            the initial suggested name
44                  */
45                 public NewNameQuery(final Object object, String name) {
46                         fObject= object;
47                         fName= name;
48                 }
49
50                 /**
51                  * Returns the new name of the compilation unit, without any extension.
52                  *
53                  * @return the new name, or <code>null</code>
54                  */
55                 private String getCompilationUnitName() {
56                         String name= fLog.getNewName(fObject);
57                         if (name != null) {
58                                 int index= name.lastIndexOf('.');
59                                 if (index > 0)
60                                         name= name.substring(0, index);
61                         }
62                         return name;
63                 }
64
65                 /**
66                  * {@inheritDoc}
67                  */
68                 public String getNewName() {
69                         String name= null;
70                         if (fObject instanceof ICompilationUnit)
71                                 name= getCompilationUnitName();
72                         else
73                                 name= fLog.getNewName(fObject);
74                         if (name == null)
75                                 name= fName;
76                         return fName;
77                 }
78         }
79
80         /** The reorg execution log */
81         private final ReorgExecutionLog fLog;
82
83         /**
84          * Creates a new logged new name queries.
85          *
86          * @param log
87          *            the reorg execution log
88          */
89         public LoggedNewNameQueries(final ReorgExecutionLog log) {
90                 fLog= log;
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         public INewNameQuery createNewCompilationUnitNameQuery(final ICompilationUnit unit, final String initialSuggestedName) {
97                 return new NewNameQuery(unit, initialSuggestedName);
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         public INewNameQuery createNewPackageFragmentRootNameQuery(final IPackageFragmentRoot root, final String initialSuggestedName) {
104                 return new NewNameQuery(root, initialSuggestedName);
105         }
106
107         /**
108          * {@inheritDoc}
109          */
110         public INewNameQuery createNewPackageNameQuery(final IPackageFragment fragment, final String initialSuggestedName) {
111                 return new NewNameQuery(fragment, initialSuggestedName);
112         }
113
114         /**
115          * {@inheritDoc}
116          */
117         public INewNameQuery createNewResourceNameQuery(final IResource resource, final String initialSuggestedName) {
118                 return new NewNameQuery(resource, initialSuggestedName);
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         public INewNameQuery createNullQuery() {
125                 return createStaticQuery(null);
126         }
127
128         /**
129          * {@inheritDoc}
130          */
131         public INewNameQuery createStaticQuery(final String name) {
132                 return new INewNameQuery() {
133
134                         public String getNewName() {
135                                 return name;
136                         }
137                 };
138         }
139 }