KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > TeamStateDescription


1 /*******************************************************************************
2  * Copyright (c) 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.ui.synchronize;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.team.core.diff.IThreeWayDiff;
17 import org.eclipse.team.core.diff.provider.Diff;
18 import org.eclipse.team.ui.mapping.ITeamStateDescription;
19
20 /**
21  * An implementation of {@link ITeamStateDescription}.
22  * <p>
23  * This class may be subclassed by clients.
24  * @since 3.2
25  */

26 public class TeamStateDescription implements ITeamStateDescription {
27
28     private int state;
29     private Map JavaDoc properties = new HashMap JavaDoc();
30
31     /**
32      * Create a description with the given state.
33      * @param state the state
34      */

35     public TeamStateDescription(int state) {
36         this.state = state;
37     }
38
39     /* (non-Javadoc)
40      * @see org.eclipse.team.ui.mapping.IDecoratedStateDescription#getStateFlags()
41      */

42     public int getStateFlags() {
43         return state;
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.team.ui.mapping.IDecoratedStateDescription#getKind()
48      */

49     public int getKind() {
50         return getStateFlags() & Diff.KIND_MASK;
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.team.ui.mapping.IDecoratedStateDescription#getDirection()
55      */

56     public int getDirection() {
57         return getStateFlags() & IThreeWayDiff.DIRECTION_MASK;
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.team.ui.mapping.ITeamStateDescription#getProperties()
62      */

63     public String JavaDoc[] getPropertyNames() {
64         return (String JavaDoc[]) properties.keySet().toArray(new String JavaDoc[properties.size()]);
65     }
66     
67     /* (non-Javadoc)
68      * @see org.eclipse.team.ui.mapping.ITeamStateDescription#getProperty(java.lang.String)
69      */

70     public Object JavaDoc getProperty(String JavaDoc property) {
71         return properties.get(property);
72     }
73     
74     /**
75      * Set the given property to the given value
76      * @param property the property
77      * @param value the value
78      */

79     public void setProperty(String JavaDoc property, Object JavaDoc value) {
80         properties.put(property, value);
81     }
82     
83     /* (non-Javadoc)
84      * @see java.lang.Object#equals(java.lang.Object)
85      */

86     public boolean equals(Object JavaDoc obj) {
87         if (obj instanceof TeamStateDescription) {
88             TeamStateDescription dsd = (TeamStateDescription) obj;
89             if (dsd.getStateFlags() == state) {
90                 if (haveSameProperties(this, dsd)) {
91                     String JavaDoc[] properties = getPropertyNames();
92                     for (int i = 0; i < properties.length; i++) {
93                         String JavaDoc property = properties[i];
94                         Object JavaDoc o1 = this.getProperty(property);
95                         Object JavaDoc o2 = dsd.getProperty(property);
96                         if (!o1.equals(o2)) {
97                             return false;
98                         }
99                     }
100                     return true;
101                 }
102             }
103             return false;
104         }
105         return super.equals(obj);
106     }
107
108     private boolean haveSameProperties(TeamStateDescription d1, TeamStateDescription d2) {
109         String JavaDoc[] p1 = d1.getPropertyNames();
110         String JavaDoc[] p2 = d2.getPropertyNames();
111         if (p1.length != p2.length) {
112             return false;
113         }
114         for (int i = 0; i < p1.length; i++) {
115             String JavaDoc s1 = p1[i];
116             boolean found = false;
117             for (int j = 0; j < p2.length; j++) {
118                 String JavaDoc s2 = p2[j];
119                 if (s1.equals(s2)) {
120                     found = true;
121                     break;
122                 }
123             }
124             if (!found)
125                 return false;
126         }
127         return true;
128     }
129
130 }
131
Popular Tags