blob: 51120519c72dded62930418b27c9f3d792e3e027 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2017 IBM Corporation and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.dltk.internal.ui.refactoring.reorg;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.dltk.core.IModelElement;
public class ReorgDestinationFactory {
private static class Destination implements IReorgDestination {
private final Object fDestination;
private final int fLocation;
public Destination(Object destination, int location) {
Assert.isNotNull(destination);
Assert.isLegal(location == LOCATION_AFTER
|| location == LOCATION_BEFORE || location == LOCATION_ON);
fDestination = destination;
fLocation = location;
}
@Override
public Object getDestination() {
return fDestination;
}
@Override
public int getLocation() {
return fLocation;
}
}
static final class ResourceDestination extends Destination {
private ResourceDestination(IResource destination, int location) {
super(destination, location);
}
public IResource getResource() {
return (IResource) getDestination();
}
}
static final class ModelElementDestination extends Destination {
private ModelElementDestination(IModelElement destination, int location) {
super(destination, location);
}
public IModelElement getJavaElement() {
return (IModelElement) getDestination();
}
}
/**
* Wrap the given object into a destination
*
* @param destination
* the object to wrap
* @return a reorg destination if possible reorg destination or <b>null</b>
* otherwise
*/
public static IReorgDestination createDestination(Object destination) {
return createDestination(destination, IReorgDestination.LOCATION_ON);
}
public static IReorgDestination createDestination(Object destination,
int location) {
if (destination instanceof IModelElement) {
return new ModelElementDestination((IModelElement) destination,
location);
}
if (destination instanceof IResource) {
return new ResourceDestination((IResource) destination, location);
}
return null;
}
}