001: /*******************************************************************************
002: * Copyright (c) 2007 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 org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.core.resources.IResource;
015:
016: import org.eclipse.jdt.core.IJavaElement;
017:
018: public class ReorgDestinationFactory {
019:
020: private static class Destination implements IReorgDestination {
021:
022: private final Object fDestination;
023: private final int fLocation;
024:
025: public Destination(Object destination, int location) {
026: Assert.isNotNull(destination);
027: Assert.isLegal(location == LOCATION_AFTER
028: || location == LOCATION_BEFORE
029: || location == LOCATION_ON);
030:
031: fDestination = destination;
032: fLocation = location;
033: }
034:
035: /**
036: * {@inheritDoc}
037: */
038: public Object getDestination() {
039: return fDestination;
040: }
041:
042: /**
043: * {@inheritDoc}
044: */
045: public int getLocation() {
046: return fLocation;
047: }
048:
049: }
050:
051: static final class ResourceDestination extends Destination {
052:
053: private ResourceDestination(IResource destination, int location) {
054: super (destination, location);
055: }
056:
057: public IResource getResource() {
058: return (IResource) getDestination();
059: }
060:
061: }
062:
063: static final class JavaElementDestination extends Destination {
064:
065: private JavaElementDestination(IJavaElement destination,
066: int location) {
067: super (destination, location);
068: }
069:
070: public IJavaElement getJavaElement() {
071: return (IJavaElement) getDestination();
072: }
073:
074: }
075:
076: /**
077: * Wrap the given object into a destination
078: * @param destination the object to wrap
079: * @return a reorg destination if possible reorg destination or <b>null</b> otherwise
080: */
081: public static IReorgDestination createDestination(Object destination) {
082: return createDestination(destination,
083: IReorgDestination.LOCATION_ON);
084: }
085:
086: public static IReorgDestination createDestination(
087: Object destination, int location) {
088: if (destination instanceof IJavaElement) {
089: return new JavaElementDestination(
090: (IJavaElement) destination, location);
091: }
092: if (destination instanceof IResource) {
093: return new ResourceDestination((IResource) destination,
094: location);
095: }
096:
097: return null;
098: }
099:
100: }
|