Git Repositories View: Refactoring first part
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / tree / RepositoryTreeNode.java
blobbfa20c2394abb22c05c8bda7b02036eab506c5b1
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
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
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.repository.tree;
13 import java.io.File;
15 import org.eclipse.jgit.lib.Ref;
16 import org.eclipse.jgit.lib.Repository;
18 /**
19 * A node in the Git Repositories view tree
21 * @param <T>
22 * the type
24 public abstract class RepositoryTreeNode<T> implements Comparable<RepositoryTreeNode> {
26 private final Repository myRepository;
28 private final T myObject;
30 private final RepositoryTreeNodeType myType;
32 private final RepositoryTreeNode myParent;
34 /**
35 * Constructs a node
37 * @param parent
38 * the parent (may be null)
39 * @param type
40 * the type
41 * @param repository
42 * the {@link Repository}
43 * @param treeObject
44 * an object (depending on the type)
46 public RepositoryTreeNode(RepositoryTreeNode parent,
47 RepositoryTreeNodeType type, Repository repository, T treeObject) {
48 myParent = parent;
49 myRepository = repository;
50 myType = type;
51 myObject = treeObject;
54 @SuppressWarnings("unchecked")
55 private RepositoryTreeNode<Repository> getRepositoryNode() {
56 if (myType == RepositoryTreeNodeType.REPO) {
57 return (RepositoryTreeNode<Repository>) this;
58 } else {
59 return getParent().getRepositoryNode();
63 /**
64 * @return the parent, or null
66 public RepositoryTreeNode getParent() {
67 return myParent;
70 /**
71 * @return the type
73 public RepositoryTreeNodeType getType() {
74 return myType;
77 /**
78 * @return the repository
80 public Repository getRepository() {
81 return myRepository;
84 /**
85 * Depending on the node type, the returned type is:
87 * <table border=1>
88 * <th>Type</th>
89 * <th>Object type</th>
90 * <tr>
91 * <td>{@link RepositoryTreeNodeType#BRANCHES}</td>
92 * <td>{@link String}</td>
93 * </tr>
94 * <tr>
95 * <td>{@link RepositoryTreeNodeType#LOCALBRANCHES}</td>
96 * <td>{@link String}</td>
97 * </tr>
98 * <tr>
99 * <td>{@link RepositoryTreeNodeType#REMOTEBRANCHES}</td>
100 * <td>{@link String}</td>
101 * </tr>
102 * <tr>
103 * <td>{@link RepositoryTreeNodeType#TAGS}</td>
104 * <td>{@link String}</td>
105 * </tr>
106 * <tr>
107 * <td>{@link RepositoryTreeNodeType#REMOTE}</td>
108 * <td>{@link String}</td>
109 * </tr>
110 * <tr>
111 * <td>{@link RepositoryTreeNodeType#REMOTES}</td>
112 * <td>{@link String}</td>
113 * </tr>
114 * <tr>
115 * <td>{@link RepositoryTreeNodeType#REPO}</td>
116 * <td>{@link Repository}</td>
117 * </tr>
118 * </table>
120 * @return the type-specific object
122 public T getObject() {
123 return myObject;
126 @Override
127 public int hashCode() {
128 final int prime = 31;
129 int result = 1;
130 switch (myType) {
131 case REPO:
132 // fall through
133 case REMOTES:
134 // fall through
135 case LOCALBRANCHES:
136 // fall through
137 case REMOTEBRANCHES:
138 // fall through
139 case BRANCHES:
140 // fall through
141 case SYMBOLICREFS:
142 // fall through
143 case WORKINGDIR:
144 result = prime
145 * result
146 + ((myObject == null) ? 0 : ((Repository) myObject)
147 .getDirectory().hashCode());
148 break;
149 case REF:
150 // fall through
151 case TAG:
152 // fall through
153 case SYMBOLICREF:
154 result = prime
155 * result
156 + ((myObject == null) ? 0 : ((Ref) myObject).getName()
157 .hashCode());
158 break;
159 case FILE:
160 // fall through
161 case FOLDER:
162 result = prime
163 * result
164 + ((myObject == null) ? 0 : ((File) myObject).getPath()
165 .hashCode());
166 break;
167 case TAGS:
168 // fall through
169 case REMOTE:
170 // fall through
171 case PUSH:
172 // fall through
173 case FETCH:
174 // fall through
175 case ERROR:
176 result = prime * result
177 + ((myObject == null) ? 0 : myObject.hashCode());
181 result = prime * result
182 + ((myParent == null) ? 0 : myParent.hashCode());
183 result = prime
184 * result
185 + ((myRepository == null) ? 0 : myRepository.getDirectory()
186 .hashCode());
187 result = prime * result + ((myType == null) ? 0 : myType.hashCode());
188 return result;
191 @Override
192 public boolean equals(Object obj) {
193 if (this == obj)
194 return true;
195 if (obj == null)
196 return false;
197 if (getClass() != obj.getClass())
198 return false;
200 RepositoryTreeNode other = (RepositoryTreeNode) obj;
202 if (myType == null) {
203 if (other.myType != null)
204 return false;
205 } else if (!myType.equals(other.myType))
206 return false;
207 if (myParent == null) {
208 if (other.myParent != null)
209 return false;
210 } else if (!myParent.equals(other.myParent))
211 return false;
212 if (myRepository == null) {
213 if (other.myRepository != null)
214 return false;
215 } else if (!myRepository.getDirectory().equals(
216 other.myRepository.getDirectory()))
217 return false;
218 if (myObject == null) {
219 if (other.myObject != null)
220 return false;
221 } else if (!checkObjectsEqual(other.myObject))
222 return false;
224 return true;
227 public int compareTo(RepositoryTreeNode otherNode) {
228 int typeDiff = this.myType.ordinal() - otherNode.getType().ordinal();
229 if (typeDiff != 0)
230 return typeDiff;
232 // we only implement this for sorting, so we only have to
233 // implement this for nodes that can be on the same level
234 // i.e. siblings to each other
236 switch (myType) {
238 case BRANCHES:
239 // fall through
240 case LOCALBRANCHES:
241 // fall through
242 case REMOTEBRANCHES:
243 // fall through
244 case REMOTES:
245 // fall through
246 case SYMBOLICREFS:
247 // fall through
248 case TAGS:
249 // fall through
250 case ERROR:
251 // fall through
252 case WORKINGDIR:
253 return 0;
255 case FETCH:
256 // fall through
257 case PUSH:
258 // fall through
259 case REMOTE:
260 return ((String) myObject)
261 .compareTo((String) otherNode.getObject());
262 case FILE:
263 // fall through
264 case FOLDER:
265 return ((File) myObject).getName().compareTo(
266 ((File) otherNode.getObject()).getName());
267 case TAG:
268 // fall through
269 case SYMBOLICREF:
270 // fall through
271 case REF:
272 return ((Ref) myObject).getName().compareTo(
273 ((Ref) otherNode.getObject()).getName());
274 case REPO:
275 int nameCompare = ((Repository) myObject).getDirectory()
276 .getParentFile().getName().compareTo(
277 (((Repository) otherNode.getObject())
278 .getDirectory().getParentFile().getName()));
279 if (nameCompare != 0)
280 return nameCompare;
281 // if the name is not unique, let's look at the whole path
282 return ((Repository) myObject).getDirectory().getParentFile()
283 .getParentFile().getPath().compareTo(
284 (((Repository) otherNode.getObject())
285 .getDirectory().getParentFile()
286 .getParentFile().getPath()));
289 return 0;
292 private boolean checkObjectsEqual(Object otherObject) {
293 switch (myType) {
294 case REPO:
295 // fall through
296 case REMOTES:
297 // fall through
298 case BRANCHES:
299 // fall through
300 case LOCALBRANCHES:
301 // fall through
302 case REMOTEBRANCHES:
303 // fall through
304 case SYMBOLICREFS:
305 // fall through
306 case ERROR:
307 // fall through
308 case WORKINGDIR:
309 return ((Repository) myObject).getDirectory().equals(
310 ((Repository) otherObject).getDirectory());
311 case REF:
312 // fall through
313 case TAG:
314 // fall through
315 case SYMBOLICREF:
316 return ((Ref) myObject).getName().equals(
317 ((Ref) otherObject).getName());
318 case FOLDER:
319 // fall through
320 case FILE:
321 return ((File) myObject).getPath().equals(
322 ((File) otherObject).getPath());
323 case REMOTE:
324 // fall through
325 case FETCH:
326 // fall through
327 case PUSH:
328 // fall through
329 case TAGS:
330 return myObject.equals(otherObject);
332 return false;