1 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 27 public class ThreeWayDiff extends Diff implements IThreeWayDiff { 28 29 private final ITwoWayDiff localChange; 30 private final ITwoWayDiff remoteChange; 31 32 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"); return null; } 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 84 public ITwoWayDiff getLocalChange() { 85 return localChange; 86 } 87 88 91 public ITwoWayDiff getRemoteChange() { 92 return remoteChange; 93 } 94 95 98 public int getDirection() { 99 return getStatus() & CONFLICTING; 100 } 101 102 105 public String toDiffString() { 106 int kind = getKind(); 107 String label = ""; if(kind==IDiff.NO_CHANGE) { 109 label = super.toDiffString(); 110 } else { 111 label = SyncInfoToDiffConverter.diffDirectionToString(getDirection()); 112 label = NLS.bind(Messages.concatStrings, new String [] { label, super.toDiffString() }); 113 } 114 return label; 115 } 116 117 120 public boolean equals(Object 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 |