Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3817873be92a4de41201147e1e1151e8ac49257f (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.core.mapping.provider;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.team.core.diff.provider.TwoWayDiff;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.mapping.IResourceDiff;

/**
 * Implementation of {@link IResourceDiff}.
 * <p>
 * This class may be subclassed by clients.
 *
 * @since 3.2
 */
public class ResourceDiff extends TwoWayDiff implements IResourceDiff {

	private final IFileRevision before;
	private final IFileRevision after;
	private final IResource resource;

	/**
	 * Create a two-way resource diff
	 * @param resource the resource
	 * @param kind the kind of change (ADDED, REMOVED or CHANGED)
	 * @param flags additional flags that describe the change
	 * @param before the before state of the model object
	 * @param after the after state of the model object
	 */
	public ResourceDiff(IResource resource, int kind, int flags, IFileRevision before, IFileRevision after) {
		super(resource.getFullPath(), kind, flags);
		this.resource = resource;
		this.before = before;
		this.after = after;
	}

	/**
	 * Convenience constructor for creating a simple folder diff
	 * that consists of a resource and a kind only. It is equivalent to
	 * <code>ResourceDiff(resource, kind, 0, null, null)<code>
	 * @param resource a resource
	 * @param kind the kind of change (ADDED, REMOVED or CHANGED)
	 */
	public ResourceDiff(IResource resource, int kind) {
		this(resource, kind, 0, null, null);
		Assert.isTrue(resource.getType() != IResource.FILE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.diff.IResourceDiff#getBeforeState()
	 */
	public IFileRevision getBeforeState() {
		return before;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.diff.IResourceDiff#getAfterState()
	 */
	public IFileRevision getAfterState() {
		return after;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.diff.IResourceDiff#getResource()
	 */
	public IResource getResource() {
		return resource;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.diff.provider.Diff#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (super.equals(obj)) {
			if (obj instanceof ResourceDiff) {
				ResourceDiff other = (ResourceDiff) obj;
				return getResource().equals(getResource())
					&& revisionsEqual(getBeforeState(), other.getBeforeState())
					&& revisionsEqual(getAfterState(), other.getAfterState());
			}
		}
		return false;
	}

	private boolean revisionsEqual(IFileRevision revision, IFileRevision revision2) {
		if (revision == null)
			return revision2 == null;
		if (revision2 == null)
			return false;
		return revision.equals(revision2);
	}

}

Back to the top