refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / synchronize / model / GitModelCache.java
blobfe2da95f50768bb8a66eb9d4d64e1b7c22fe5a63
1 /*******************************************************************************
2 * Copyright (C) 2010, 2013 Dariusz Luksza <dariusz@luksza.org> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.synchronize.model;
13 import java.util.Map;
15 import org.eclipse.compare.structuremergeviewer.Differencer;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change;
19 import org.eclipse.egit.ui.internal.UIText;
20 import org.eclipse.egit.ui.internal.synchronize.model.TreeBuilder.FileModelFactory;
21 import org.eclipse.egit.ui.internal.synchronize.model.TreeBuilder.TreeModelFactory;
22 import org.eclipse.jgit.lib.Repository;
24 /**
25 * Git cache representation in EGit Change Set
27 public class GitModelCache extends GitModelObjectContainer {
29 private final Path location;
31 private final Repository repo;
33 private GitModelObject[] children;
35 /**
36 * Constructs model node that represents current status of Git cache.
38 * @param parent
39 * parent object
41 * @param repo
42 * repository associated with this object
43 * @param cache
44 * cache containing all changed objects
46 public GitModelCache(GitModelRepository parent, Repository repo,
47 Map<String, Change> cache) {
48 this(parent, repo, cache, new FileModelFactory() {
49 @Override
50 public GitModelBlob createFileModel(
51 GitModelObjectContainer objParent, Repository nestedRepo,
52 Change change, IPath path) {
53 return new GitModelCacheFile(objParent, nestedRepo, change,
54 path);
57 @Override
58 public boolean isWorkingTree() {
59 return false;
61 });
64 /**
65 * @param parent
66 * parent object
67 * @param repo
68 * repository associated with this object
69 * @param changes
70 * list of changes associated with this object
71 * @param fileFactory
72 * leaf instance factory
74 protected GitModelCache(GitModelRepository parent, final Repository repo,
75 Map<String, Change> changes, final FileModelFactory fileFactory) {
76 super(parent);
77 this.repo = repo;
78 this.location = new Path(repo.getWorkTree().toString());
80 this.children = TreeBuilder.build(this, repo, changes, fileFactory,
81 new TreeModelFactory() {
82 @Override
83 public GitModelTree createTreeModel(
84 GitModelObjectContainer parentObject,
85 IPath fullPath,
86 int kind) {
87 return new GitModelCacheTree(parentObject, repo,
88 fullPath, fileFactory);
90 });
93 @Override
94 public String getName() {
95 return UIText.GitModelIndex_index;
98 @Override
99 public GitModelObject[] getChildren() {
100 return children;
103 @Override
104 public int getKind() {
105 return Differencer.CHANGE | Differencer.RIGHT;
108 @Override
109 public int repositoryHashCode() {
110 return repo.getWorkTree().hashCode();
113 @Override
114 public boolean equals(Object obj) {
115 if (obj == this)
116 return true;
118 if (obj instanceof GitModelCache
119 && !(obj instanceof GitModelWorkingTree)) {
120 GitModelCache left = (GitModelCache) obj;
121 return left.getParent().equals(getParent());
124 return false;
127 @Override
128 public int hashCode() {
129 return repositoryHashCode();
132 @Override
133 public IPath getLocation() {
134 return location;
137 @Override
138 public String toString() {
139 return "ModelCache"; //$NON-NLS-1$
142 @Override
143 public void dispose() {
144 if (children != null) {
145 for (GitModelObject object : children)
146 object.dispose();
147 children = null;