KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > variants > ThreeWayResourceComparator


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.variants;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.team.core.TeamException;
15 import org.eclipse.team.internal.core.TeamPlugin;
16
17 /**
18  * A resource comparator that uses the <code>ThreeWaySynchronizer</code>
19  * to compare local resources to their resource variants. The local state
20  * is determined using the local modification state and the remote state
21  * is determined by comparing the base bytes to the remote bytes obtained
22  * from the synchronizer.
23  *
24  * @since 3.0
25  */

26 public class ThreeWayResourceComparator implements IResourceVariantComparator {
27     
28     private ThreeWaySynchronizer synchronizer;
29     
30     /**
31      * Create a three-way resource comparator that uses the <code>ThreeWaySynchronizer</code>
32      * to compare a local resource to a resource variant.
33      * @param synchronizer
34      */

35     public ThreeWayResourceComparator(ThreeWaySynchronizer synchronizer) {
36         this.synchronizer = synchronizer;
37     }
38     
39     /* (non-Javadoc)
40      * @see org.eclipse.team.core.variants.IResourceVariantComparator#compare(org.eclipse.core.resources.IResource, org.eclipse.team.core.variants.IResourceVariant)
41      */

42     public boolean compare(IResource local, IResourceVariant remote) {
43         // First, ensure the resources are the same gender
44
if ((local.getType() == IResource.FILE) == remote.isContainer()) {
45             return false;
46         }
47         try {
48             // If the file is locally modified, it cannot be in sync
49
if (local.getType() == IResource.FILE && getSynchronizer().isLocallyModified(local)) {
50                 return false;
51             }
52             // If there is no base, the local cannot match the remote
53
if (getSynchronizer().getBaseBytes(local) == null) return false;
54             // Otherwise, assume they are the same if the remote equals the base
55
return equals(getSynchronizer().getBaseBytes(local), getBytes(remote));
56         } catch (TeamException e) {
57             TeamPlugin.log(e);
58             return false;
59         }
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.team.core.variants.IResourceVariantComparator#compare(org.eclipse.team.core.variants.IResourceVariant, org.eclipse.team.core.variants.IResourceVariant)
64      */

65     public boolean compare(IResourceVariant base, IResourceVariant remote) {
66         byte[] bytes1 = getBytes(base);
67         byte[] bytes2 = getBytes(remote);
68         return equals(bytes1, bytes2);
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.team.core.variants.IResourceVariantComparator#isThreeWay()
73      */

74     public boolean isThreeWay() {
75         return true;
76     }
77     
78     private ThreeWaySynchronizer getSynchronizer() {
79         return synchronizer;
80     }
81     
82     private byte[] getBytes(IResourceVariant remote) {
83         return remote.asBytes();
84     }
85     
86     private boolean equals(byte[] syncBytes, byte[] oldBytes) {
87         if (syncBytes.length != oldBytes.length) return false;
88         for (int i = 0; i < oldBytes.length; i++) {
89             if (oldBytes[i] != syncBytes[i]) return false;
90         }
91         return true;
92     }
93 }
94
Popular Tags