| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2008, 2009 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.pde.api.tools.internal.provisional.comparator; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Iterator; |
| 15 | |
| 16 | import org.eclipse.core.runtime.CoreException; |
| 17 | import org.eclipse.core.runtime.IStatus; |
| 18 | import org.eclipse.core.runtime.Status; |
| 19 | import org.eclipse.osgi.util.NLS; |
| 20 | import org.eclipse.pde.api.tools.internal.model.Messages; |
| 21 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
| 22 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiScopeVisitor; |
| 23 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
| 24 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 25 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
| 26 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiScope; |
| 27 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
| 28 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
| 29 | import org.eclipse.pde.api.tools.internal.util.Util; |
| 30 | |
| 31 | /** |
| 32 | * Default implementation of a {@link IApiScope}. |
| 33 | */ |
| 34 | public class ApiScope implements IApiScope { |
| 35 | private static final IApiElement[] NO_ELEMENTS = new IApiElement[0]; |
| 36 | |
| 37 | /** |
| 38 | * Contains all API elements of this scope |
| 39 | */ |
| 40 | ArrayList elements; |
| 41 | |
| 42 | /* (non-Javadoc) |
| 43 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiScope#accept(org.eclipse.pde.api.tools.internal.provisional.model.ApiScopeVisitor) |
| 44 | */ |
| 45 | public void accept(ApiScopeVisitor visitor) throws CoreException { |
| 46 | IApiElement[] elements = getApiElements(); |
| 47 | for (int i = 0; i < elements.length; i++) { |
| 48 | IApiElement apiElement = elements[i]; |
| 49 | int type = apiElement.getType(); |
| 50 | switch(type) { |
| 51 | case IApiElement.API_TYPE_CONTAINER : { |
| 52 | IApiTypeContainer container = (IApiTypeContainer) apiElement; |
| 53 | visitor.visit(container); |
| 54 | visitor.endVisit(container); |
| 55 | break; |
| 56 | } |
| 57 | case IApiElement.API_TYPE_ROOT : { |
| 58 | IApiTypeRoot root = (IApiTypeRoot) apiElement; |
| 59 | visitor.visit(root); |
| 60 | visitor.endVisit(root); |
| 61 | break; |
| 62 | } |
| 63 | case IApiElement.BASELINE : { |
| 64 | IApiBaseline baseline = (IApiBaseline) apiElement; |
| 65 | visitor.visit(baseline); |
| 66 | visitor.endVisit(baseline); |
| 67 | break; |
| 68 | } |
| 69 | case IApiElement.COMPONENT : { |
| 70 | IApiComponent component = (IApiComponent) apiElement; |
| 71 | visitor.visit(component); |
| 72 | visitor.endVisit(component); |
| 73 | break; |
| 74 | } |
| 75 | default: |
| 76 | throw new CoreException( |
| 77 | new Status( |
| 78 | IStatus.ERROR, |
| 79 | ApiPlugin.PLUGIN_ID, |
| 80 | NLS.bind( |
| 81 | Messages.ApiScope_0, |
| 82 | Util.getApiElementType(type)))); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* (non-Javadoc) |
| 88 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiScope#addElement(org.eclipse.pde.api.tools.internal.provisional.model.IApiElement) |
| 89 | */ |
| 90 | public void addElement(IApiElement newelement) { |
| 91 | if (this.elements == null) { |
| 92 | this.elements = new ArrayList(); |
| 93 | } |
| 94 | this.elements.add(newelement); |
| 95 | } |
| 96 | |
| 97 | /* (non-Javadoc) |
| 98 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiScope#encloses(org.eclipse.pde.api.tools.internal.provisional.model.IApiElement) |
| 99 | */ |
| 100 | public boolean encloses(IApiElement element) { |
| 101 | if(element != null) { |
| 102 | IApiComponent component = element.getApiComponent(); |
| 103 | IApiComponent enclosing = null; |
| 104 | for(Iterator iter = this.elements.iterator(); iter.hasNext();) { |
| 105 | enclosing = ((IApiElement)iter.next()).getApiComponent(); |
| 106 | if(component.equals(enclosing)) { |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /* (non-Javadoc) |
| 115 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiScope#getApiElement() |
| 116 | */ |
| 117 | public IApiElement[] getApiElements() { |
| 118 | if (this.elements == null || this.elements.size() == 0) { |
| 119 | return NO_ELEMENTS; |
| 120 | } |
| 121 | return (IApiElement[]) this.elements.toArray(new IApiElement[this.elements.size()]); |
| 122 | } |
| 123 | } |