Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4f903b16ade1c3c4d7df58e2b73c257856474dde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2011, 2016 Obeo and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *     Philip Langer - improve JavaDoc
 *******************************************************************************/
package org.eclipse.emf.compare.ide.utils;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.compare.ide.EMFCompareIDEPlugin;
import org.eclipse.emf.compare.utils.IDiagnosable;

/**
 * A Resource Traversal is no more than a set of resources used by the synchronization model to determine
 * which resources to load as part of a given logical model.
 * 
 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
 */
@Beta
public class StorageTraversal implements IAdaptable, IDiagnosable {
	/** The set of storages that are part of this traversal. */
	private Set<? extends IStorage> storages;

	/** The diagnostic of the errors that may occur during loading of the storages. */
	private Diagnostic diagnostic;

	/**
	 * Creates our traversal given its set of resources.
	 * 
	 * @param storages
	 *            The set of resources that are part of this traversal.
	 */
	public StorageTraversal(Set<? extends IStorage> storages) {
		this(storages,
				new BasicDiagnostic(EMFCompareIDEPlugin.PLUGIN_ID, 0, null, new Object[] {storages, }));
	}

	/**
	 * Creates our traversal given its set of resources.
	 * 
	 * @param storages
	 *            The set of resources that are part of this traversal.
	 * @param diagnostic
	 *            diagnostic of the errors that may occur during loading of the storages.
	 */
	public StorageTraversal(Set<? extends IStorage> storages, Diagnostic diagnostic) {
		this.storages = storages;
		this.diagnostic = Preconditions.checkNotNull(diagnostic);
	}

	/**
	 * Returns a mutable copy of the set of resources that are part of this traversal.
	 * 
	 * @return A mutable copy of the set of resources that are part of this traversal.
	 */
	public Set<? extends IStorage> getStorages() {
		return new LinkedHashSet<IStorage>(storages);
	}

	/**
	 * Removes the given storage from this traversal.
	 * 
	 * @param storage
	 *            The storage to be removed.
	 * @since 3.1
	 */
	public void removeStorage(IStorage storage) {
		storages.remove(storage);
	}

	/**
	 * Returns the diagnostic of the storages of this traversal.
	 * 
	 * @return the diagnostic
	 */
	public Diagnostic getDiagnostic() {
		return diagnostic;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.utils.IDiagnosable#setDiagnostic(org.eclipse.emf.common.util.Diagnostic)
	 */
	public void setDiagnostic(Diagnostic diagnostic) {
		this.diagnostic = diagnostic;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
		if (adapter == org.eclipse.core.resources.mapping.ResourceTraversal.class) {
			// Team's resource traversal only knows about IResources.
			final List<IResource> resources = Lists.newArrayListWithCapacity(storages.size());
			for (IStorage storage : storages) {
				if (storage instanceof IFile) {
					resources.add((IFile)storage);
				} else {
					/*
					 * Use a file handle. Since files can be both local and remote, they might not even exist
					 * in the current workspace. It will be the responsibility of the user to either get the
					 * remote or local content. The traversal itself only tells "all" potential resources
					 * linked to the current.
					 */
					resources.add(ResourcesPlugin.getWorkspace().getRoot()
							.getFile(ResourceUtil.getFixedPath(storage)));
				}
			}
			final IResource[] resourceArray = resources.toArray(new IResource[resources.size()]);
			return new org.eclipse.core.resources.mapping.ResourceTraversal(resourceArray,
					IResource.DEPTH_ZERO, IResource.NONE);
		}
		return null;
	}

	/** {@inheritDoc} */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof StorageTraversal) {
			return storages.equals(((StorageTraversal)obj).storages);
		}
		return false;
	}

	/** {@inheritDoc} */
	@Override
	public int hashCode() {
		return storages.hashCode();
	}
}

Back to the top