KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > diff > provider > ThreeWayDiff


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.core.diff.provider;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.team.core.diff.*;
17 import org.eclipse.team.internal.core.Messages;
18 import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter;
19
20 /**
21  * Implementation of {@link IThreeWayDiff}.
22  * <p>
23  * This class is not intended to be subclasses by clients.
24  *
25  * @since 3.2
26  */

27 public class ThreeWayDiff extends Diff implements IThreeWayDiff {
28
29     private final ITwoWayDiff localChange;
30     private final ITwoWayDiff remoteChange;
31
32     /**
33      * Create a three-way delta from the two changes. At least one change
34      * must be provided (i.e. either change may be <code>null</code>
35      * but at least one must be non-<code>null</code>).
36      * @param localChange the local change in the model object or <code>null</code> if there is no local change
37      * @param remoteChange the remote change in the model object or <code>null</code> if there is no local change
38      */

39     public ThreeWayDiff(ITwoWayDiff localChange, ITwoWayDiff remoteChange) {
40         super(calculatePath(localChange, remoteChange), calculateKind(localChange, remoteChange) | calculateDirection(localChange, remoteChange));
41         this.localChange = localChange;
42         this.remoteChange = remoteChange;
43     }
44
45     private static IPath calculatePath(ITwoWayDiff localChange, ITwoWayDiff remoteChange) {
46         if (localChange != null && remoteChange != null)
47             Assert.isTrue(localChange.getPath().equals(remoteChange.getPath()));
48         if (localChange != null)
49             return localChange.getPath();
50         if (remoteChange != null)
51             return remoteChange.getPath();
52         Assert.isLegal(false, "Either or local or remote change must be supplied"); //$NON-NLS-1$
53
return null; // Will never be reached
54
}
55
56     private static int calculateDirection(ITwoWayDiff localChange, ITwoWayDiff remoteChange) {
57         int direction = 0;
58         if (localChange != null && localChange.getKind() != NO_CHANGE) {
59             direction |= OUTGOING;
60         }
61         if (remoteChange != null && remoteChange.getKind() != NO_CHANGE) {
62             direction |= INCOMING;
63         }
64         return direction;
65     }
66     
67     private static int calculateKind(ITwoWayDiff localChange, ITwoWayDiff remoteChange) {
68         int localKind = NO_CHANGE;
69         if (localChange != null)
70             localKind = localChange.getKind();
71         int remoteKind = NO_CHANGE;
72         if (remoteChange != null)
73             remoteKind = remoteChange.getKind();
74         if (localKind == NO_CHANGE || localKind == remoteKind)
75             return remoteKind;
76         if (remoteKind == NO_CHANGE)
77             return localKind;
78         return CHANGE;
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.team.core.synchronize.IThreeWayDelta#getLocalChange()
83      */

84     public ITwoWayDiff getLocalChange() {
85         return localChange;
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.team.core.synchronize.IThreeWayDelta#getRemoteChange()
90      */

91     public ITwoWayDiff getRemoteChange() {
92         return remoteChange;
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.team.core.synchronize.IThreeWayDelta#getDirection()
97      */

98     public int getDirection() {
99         return getStatus() & CONFLICTING;
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.team.core.diff.IDiffNode#toDiffString()
104      */

105     public String JavaDoc toDiffString() {
106         int kind = getKind();
107         String JavaDoc label = ""; //$NON-NLS-1$
108
if(kind==IDiff.NO_CHANGE) {
109             label = super.toDiffString();
110         } else {
111             label = SyncInfoToDiffConverter.diffDirectionToString(getDirection());
112             label = NLS.bind(Messages.concatStrings, new String JavaDoc[] { label, super.toDiffString() });
113         }
114         return label;
115     }
116     
117     /* (non-Javadoc)
118      * @see org.eclipse.team.core.diff.provider.Diff#equals(java.lang.Object)
119      */

120     public boolean equals(Object JavaDoc obj) {
121         if (obj == this)
122             return true;
123         if (super.equals(obj)) {
124             if (obj instanceof ThreeWayDiff) {
125                 ThreeWayDiff other = (ThreeWayDiff) obj;
126                 return changesEqual(getLocalChange(), other.getLocalChange())
127                     && changesEqual(getRemoteChange(), other.getRemoteChange());
128             }
129         }
130         return false;
131     }
132
133     private boolean changesEqual(ITwoWayDiff diff, ITwoWayDiff diff2) {
134         if (diff == null)
135             return diff2 == null;
136         if (diff2 == null)
137             return false;
138         return diff.equals(diff2);
139     }
140
141 }
142
Popular Tags