KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > history > RefactoringHistoryImplementation


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.ltk.internal.core.refactoring.history;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.LinkedHashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
21 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
22
23 /**
24  * Default implementation of a refactoring history.
25  *
26  * @since 3.2
27  */

28 public final class RefactoringHistoryImplementation extends RefactoringHistory {
29
30     /**
31      * Returns a hash code value for the array
32      *
33      * @param array
34      * the array to create a hash code value for
35      * @return a hash code value for the array
36      */

37     private static int hashCode(final Object JavaDoc[] array) {
38         if (array == null)
39             return 0;
40         int result= 1;
41         for (int index= 0; index < array.length; index++)
42             result= 31 * result + (array[index] == null ? 0 : array[index].hashCode());
43         return result;
44     }
45
46     /** The refactoring descriptor proxies */
47     private final RefactoringDescriptorProxy[] fDescriptorProxies;
48
49     /** Is the refactoring history already sorted? */
50     private boolean fSorted= false;
51
52     /**
53      * Creates a new refactoring history implementation.
54      *
55      * @param proxies
56      * the refactoring descriptor proxies
57      */

58     public RefactoringHistoryImplementation(final RefactoringDescriptorProxy[] proxies) {
59         Assert.isNotNull(proxies);
60         fDescriptorProxies= new RefactoringDescriptorProxy[proxies.length];
61         System.arraycopy(proxies, 0, fDescriptorProxies, 0, proxies.length);
62     }
63
64     /**
65      * {@inheritDoc}
66      */

67     public boolean equals(final Object JavaDoc object) {
68         if (this == object)
69             return true;
70         if (object == null)
71             return false;
72         if (getClass() != object.getClass())
73             return false;
74         final RefactoringHistoryImplementation other= (RefactoringHistoryImplementation) object;
75         if (!Arrays.equals(getDescriptors(), other.getDescriptors()))
76             return false;
77         return true;
78     }
79
80     /**
81      * Returns the descriptor proxies, in no particular order.
82      *
83      * @return the descriptor proxies
84      */

85     RefactoringDescriptorProxy[] getDescriptorProxies() {
86         return fDescriptorProxies;
87     }
88
89     /**
90      * {@inheritDoc}
91      */

92     public RefactoringDescriptorProxy[] getDescriptors() {
93         if (!fSorted && fDescriptorProxies.length > 1)
94             RefactoringHistoryManager.sortRefactoringDescriptorsDescending(fDescriptorProxies);
95         fSorted= true;
96         final RefactoringDescriptorProxy[] proxies= new RefactoringDescriptorProxy[fDescriptorProxies.length];
97         System.arraycopy(fDescriptorProxies, 0, proxies, 0, fDescriptorProxies.length);
98         return proxies;
99     }
100
101     /**
102      * {@inheritDoc}
103      */

104     public int hashCode() {
105         return 31 * RefactoringHistoryImplementation.hashCode(getDescriptors());
106     }
107
108     /**
109      * {@inheritDoc}
110      */

111     public boolean isEmpty() {
112         return fDescriptorProxies.length == 0;
113     }
114
115     /**
116      * {@inheritDoc}
117      */

118     public RefactoringHistory removeAll(final RefactoringHistory history) {
119         final Set JavaDoc existing= new LinkedHashSet JavaDoc(Arrays.asList(fDescriptorProxies));
120         final Set JavaDoc other= new HashSet JavaDoc(Arrays.asList(history.getDescriptors()));
121         existing.removeAll(other);
122         final RefactoringDescriptorProxy[] proxies= new RefactoringDescriptorProxy[existing.size()];
123         existing.toArray(proxies);
124         return new RefactoringHistoryImplementation(proxies);
125     }
126
127     /**
128      * {@inheritDoc}
129      */

130     public String JavaDoc toString() {
131         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(256);
132         buffer.append(getClass().getName());
133         buffer.append("[descriptors="); //$NON-NLS-1$
134
buffer.append(getDescriptors().toString());
135         buffer.append(']');
136         return buffer.toString();
137     }
138 }
Popular Tags