1 11 package org.eclipse.ltk.internal.core.refactoring.history; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.LinkedHashSet ; 16 import java.util.Set ; 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 28 public final class RefactoringHistoryImplementation extends RefactoringHistory { 29 30 37 private static int hashCode(final Object [] 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 47 private final RefactoringDescriptorProxy[] fDescriptorProxies; 48 49 50 private boolean fSorted= false; 51 52 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 67 public boolean equals(final Object 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 85 RefactoringDescriptorProxy[] getDescriptorProxies() { 86 return fDescriptorProxies; 87 } 88 89 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 104 public int hashCode() { 105 return 31 * RefactoringHistoryImplementation.hashCode(getDescriptors()); 106 } 107 108 111 public boolean isEmpty() { 112 return fDescriptorProxies.length == 0; 113 } 114 115 118 public RefactoringHistory removeAll(final RefactoringHistory history) { 119 final Set existing= new LinkedHashSet (Arrays.asList(fDescriptorProxies)); 120 final Set other= new HashSet (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 130 public String toString() { 131 final StringBuffer buffer= new StringBuffer (256); 132 buffer.append(getClass().getName()); 133 buffer.append("[descriptors="); buffer.append(getDescriptors().toString()); 135 buffer.append(']'); 136 return buffer.toString(); 137 } 138 } | Popular Tags |