001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.reorg;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Comparator;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Set;
020:
021: import org.eclipse.core.runtime.Assert;
022:
023: import org.eclipse.core.resources.IFile;
024:
025: import org.eclipse.jdt.core.IClassFile;
026: import org.eclipse.jdt.core.ICompilationUnit;
027: import org.eclipse.jdt.core.IJavaElement;
028: import org.eclipse.jdt.core.ISourceReference;
029: import org.eclipse.jdt.core.JavaModelException;
030:
031: public class SourceReferenceUtil {
032:
033: //no instances
034: private SourceReferenceUtil() {
035: }
036:
037: public static IFile getFile(ISourceReference ref) {
038: ICompilationUnit unit = getCompilationUnit(ref);
039: return (IFile) unit.getPrimary().getResource();
040: }
041:
042: public static ICompilationUnit getCompilationUnit(ISourceReference o) {
043: Assert.isTrue(!(o instanceof IClassFile));
044:
045: if (o instanceof ICompilationUnit)
046: return (ICompilationUnit) o;
047: if (o instanceof IJavaElement)
048: return (ICompilationUnit) ((IJavaElement) o)
049: .getAncestor(IJavaElement.COMPILATION_UNIT);
050: return null;
051: }
052:
053: private static boolean hasParentInSet(IJavaElement elem, Set set) {
054: IJavaElement parent = elem.getParent();
055: while (parent != null) {
056: if (set.contains(parent))
057: return true;
058: parent = parent.getParent();
059: }
060: return false;
061: }
062:
063: public static ISourceReference[] removeAllWithParentsSelected(
064: ISourceReference[] elems) {
065: Set set = new HashSet(Arrays.asList(elems));
066: List result = new ArrayList(elems.length);
067: for (int i = 0; i < elems.length; i++) {
068: ISourceReference elem = elems[i];
069: if (!(elem instanceof IJavaElement))
070: result.add(elem);
071: else {
072: if (!hasParentInSet(((IJavaElement) elem), set))
073: result.add(elem);
074: }
075: }
076: return (ISourceReference[]) result
077: .toArray(new ISourceReference[result.size()]);
078: }
079:
080: /**
081: * @return IFile -> List of ISourceReference (elements from that file)
082: */
083: public static Map groupByFile(ISourceReference[] elems) {
084: Map map = new HashMap();
085: for (int i = 0; i < elems.length; i++) {
086: ISourceReference elem = elems[i];
087: IFile file = SourceReferenceUtil.getFile(elem);
088: if (!map.containsKey(file))
089: map.put(file, new ArrayList());
090: ((List) map.get(file)).add(elem);
091: }
092: return map;
093: }
094:
095: public static ISourceReference[] sortByOffset(
096: ISourceReference[] methods) {
097: Arrays.sort(methods, new Comparator() {
098: public int compare(Object o1, Object o2) {
099: try {
100: return ((ISourceReference) o2).getSourceRange()
101: .getOffset()
102: - ((ISourceReference) o1).getSourceRange()
103: .getOffset();
104: } catch (JavaModelException e) {
105: return o2.hashCode() - o1.hashCode();
106: }
107: }
108: });
109: return methods;
110: }
111: }
|