refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / synchronize / model / GitModelRoot.java
blobaf5632c11919f5d6fc88c09bda947dd5bdf9db26
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.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.egit.core.Activator;
18 import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData;
19 import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet;
21 /**
22 * Root of all model objects.
24 public class GitModelRoot {
26 private final GitSynchronizeDataSet gsds;
28 private GitModelObject[] children;
30 /**
31 * @param gsds
33 public GitModelRoot(GitSynchronizeDataSet gsds) {
34 this.gsds = gsds;
37 /**
38 * @return git synchronization data
40 public GitSynchronizeDataSet getGsds() {
41 return gsds;
44 /**
45 * @return children
47 public GitModelObject[] getChildren() {
48 return getChildrenImpl();
51 /**
52 * Disposes all nested resources
54 public void dispose() {
55 disposeOldChildren();
58 private GitModelObject[] getChildrenImpl() {
59 List<GitModelObject> result = new ArrayList<>();
60 try {
61 if (gsds.size() == 1) {
62 GitSynchronizeData gsd = gsds.iterator().next();
63 GitModelRepository repoModel = new GitModelRepository(gsd);
65 return repoModel.getChildren();
66 } else
67 for (GitSynchronizeData data : gsds) {
68 GitModelRepository repoModel = new GitModelRepository(data);
69 if (repoModel.getChildren().length > 0)
70 result.add(repoModel);
72 } catch (IOException e) {
73 Activator.logError(e.getMessage(), e);
75 disposeOldChildren();
76 children = result.toArray(new GitModelObject[0]);
78 return children;
81 private void disposeOldChildren() {
82 if (children == null)
83 return;
84 for (GitModelObject child : children)
85 child.dispose();