1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 Obeo.
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
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.emf
.compare
.ide
.utils
;
13 import com
.google
.common
.annotations
.Beta
;
14 import com
.google
.common
.base
.Preconditions
;
15 import com
.google
.common
.collect
.Lists
;
17 import java
.util
.LinkedHashSet
;
18 import java
.util
.List
;
21 import org
.eclipse
.core
.resources
.IFile
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.resources
.IStorage
;
24 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
25 import org
.eclipse
.core
.runtime
.IAdaptable
;
26 import org
.eclipse
.emf
.common
.util
.BasicDiagnostic
;
27 import org
.eclipse
.emf
.common
.util
.Diagnostic
;
28 import org
.eclipse
.emf
.compare
.ide
.EMFCompareIDEPlugin
;
29 import org
.eclipse
.emf
.compare
.utils
.IDiagnosable
;
32 * A Resource Traversal is no more than a set of resources used by the synchronization model to determine
33 * which resources to load as part of a given logical model.
35 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
38 public class StorageTraversal
implements IAdaptable
, IDiagnosable
{
39 /** The set of storages that are part of this traversal. */
40 private Set
<?
extends IStorage
> storages
;
42 /** The diagnostic of the errors that may occur during loading of the storages. */
43 private Diagnostic diagnostic
;
46 * Creates our traversal given its set of resources.
49 * The set of resources that are part of this traversal.
51 public StorageTraversal(Set
<?
extends IStorage
> storages
) {
52 this(storages
, new BasicDiagnostic(EMFCompareIDEPlugin
.PLUGIN_ID
, 0, null, new Object
[] {storages
, }));
56 * Creates our traversal given its set of resources.
59 * The set of resources that are part of this traversal.
61 * diagnostic of the errors that may occur during loading of the storages.
63 public StorageTraversal(Set
<?
extends IStorage
> storages
, Diagnostic diagnostic
) {
64 this.storages
= storages
;
65 this.diagnostic
= Preconditions
.checkNotNull(diagnostic
);
69 * Returns the set of resources that are part of this traversal.
71 * Note that this is the original set, and that any modification on the returned {@link Set} will affect
75 * @return The set of resources that are part of this traversal.
77 public Set
<?
extends IStorage
> getStorages() {
78 return new LinkedHashSet
<IStorage
>(storages
);
82 * Removes the given storage from this traversal.
85 * The storage to be removed.
88 public void removeStorage(IStorage storage
) {
89 storages
.remove(storage
);
93 * Returns the diagnostic of the storages of this traversal.
95 * @return the diagnostic
97 public Diagnostic
getDiagnostic() {
104 * @see org.eclipse.emf.compare.utils.IDiagnosable#setDiagnostic(org.eclipse.emf.common.util.Diagnostic)
106 public void setDiagnostic(Diagnostic diagnostic
) {
107 this.diagnostic
= diagnostic
;
113 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
115 @SuppressWarnings("unchecked")
116 public Object
getAdapter(@SuppressWarnings("rawtypes") Class adapter
) {
117 if (adapter
== org
.eclipse
.core
.resources
.mapping
.ResourceTraversal
.class) {
118 // Team's resource traversal only knows about IResources.
119 final List
<IResource
> resources
= Lists
.newArrayListWithCapacity(storages
.size());
120 for (IStorage storage
: storages
) {
121 if (storage
instanceof IFile
) {
122 resources
.add((IFile
)storage
);
125 * Use a file handle. Since files can be both local and remote, they might not even exist
126 * in the current workspace. It will be the responsibility of the user to either get the
127 * remote or local content. The traversal itself only tells "all" potential resources
128 * linked to the current.
130 resources
.add(ResourcesPlugin
.getWorkspace().getRoot().getFile(
131 ResourceUtil
.getFixedPath(storage
)));
134 final IResource
[] resourceArray
= resources
.toArray(new IResource
[resources
.size()]);
135 return new org
.eclipse
.core
.resources
.mapping
.ResourceTraversal(resourceArray
,
136 IResource
.DEPTH_ZERO
, IResource
.NONE
);
143 public boolean equals(Object obj
) {
144 if (obj
instanceof StorageTraversal
) {
145 return storages
.equals(((StorageTraversal
)obj
).storages
);
152 public int hashCode() {
153 return storages
.hashCode();