StagingView wrongly sorted by state initially
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / components / RepositorySelection.java
blobcbdd4da790d901e5a4ce02de63513deaeae91b58
1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
13 package org.eclipse.egit.ui.internal.components;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
19 import org.eclipse.jgit.transport.RemoteConfig;
20 import org.eclipse.jgit.transport.URIish;
22 /**
23 * Data class representing selection of remote repository made by user.
24 * Selection is either a URI or a remote repository configuration.
25 * <p>
26 * Each immutable instance has at least one of two class fields (URI, remote
27 * config) set to null. null value indicates that it has illegal value or this
28 * form of repository selection is not selected.
30 public class RepositorySelection {
31 private URIish uri;
33 private RemoteConfig config;
35 static final RepositorySelection INVALID_SELECTION = new RepositorySelection(
36 null, null);
38 /**
39 * @param uri
40 * the new specified URI. null if the new URI is invalid or user
41 * chosen to specify repository as remote config instead of URI.
42 * @param config
43 * the new remote config. null if user chosen to specify
44 * repository as URI.
46 public RepositorySelection(final URIish uri, final RemoteConfig config) {
47 if (config != null && uri != null)
48 throw new IllegalArgumentException(
49 "URI and config cannot be set at the same time."); //$NON-NLS-1$
50 this.config = config;
51 this.uri = uri;
54 /**
55 * Return the selected URI.
56 * <p>
57 * If pushMode is <code>true</code> and a remote configuration was selected,
58 * this will try to return a push URI from that configuration, otherwise a
59 * URI; if no configuration was selected, the URI entered in the URI field
60 * will be returned.<br>
61 * If pushMode is <code>false</code> and a remote configuration was
62 * selected, this will try to return a URI from that configuration,
63 * otherwise <code>null</code> will be returned; if no configuration was
64 * selected, the URI entered in the URI field will be returned
66 * @param pushMode
67 * the push mode
68 * @return the selected URI, or <code>null</code> if there is no valid
69 * selection
71 public URIish getURI(boolean pushMode) {
72 if (isConfigSelected())
73 if (pushMode) {
74 if (config.getPushURIs().size() > 0)
75 return config.getPushURIs().get(0);
76 else if (config.getURIs().size() > 0)
77 return config.getURIs().get(0);
78 else
79 return null;
80 } else {
81 if (config.getURIs().size() > 0)
82 return config.getURIs().get(0);
83 else if (config.getPushURIs().size() > 0)
84 return config.getPushURIs().get(0);
85 else
86 return null;
88 return uri;
91 /**
92 * @return the selected URI, <code>null</code> if a configuration was
93 * selected
95 public URIish getURI() {
96 if (isConfigSelected())
97 return null;
98 return uri;
102 * @return list of all push URIs - either the one specified as custom URI or
103 * all push URIs of the selected configuration; if not push URIs
104 * were specified, the first URI is returned
106 public List<URIish> getPushURIs() {
107 if (isURISelected())
108 return Collections.singletonList(uri);
109 if (isConfigSelected()) {
110 List<URIish> pushUris = new ArrayList<>();
111 pushUris.addAll(config.getPushURIs());
112 if (pushUris.isEmpty())
113 pushUris.add(config.getURIs().get(0));
114 return pushUris;
116 return null;
120 * @return the selected remote configuration. null if user chosen to select
121 * repository as URI.
123 public RemoteConfig getConfig() {
124 return config;
128 * @return selected remote configuration name or null if selection is not a
129 * remote configuration.
131 public String getConfigName() {
132 if (isConfigSelected())
133 return config.getName();
134 return null;
138 * @return true if selection contains valid URI or remote config, false if
139 * there is no valid selection.
141 public boolean isValidSelection() {
142 return uri != null || config != null;
146 * @return true if user selected valid URI, false if user selected invalid
147 * URI or remote config.
149 public boolean isURISelected() {
150 return uri != null;
154 * @return true if user selected remote configuration, false if user
155 * selected (invalid or valid) URI.
157 public boolean isConfigSelected() {
158 return config != null;
161 @Override
162 public boolean equals(final Object obj) {
163 if (obj == this)
164 return true;
166 if (obj instanceof RepositorySelection) {
167 final RepositorySelection other = (RepositorySelection) obj;
168 if (uri == null ^ other.uri == null)
169 return false;
170 if (uri != null && !uri.equals(other.uri))
171 return false;
173 if (config != other.config)
174 return false;
176 return true;
177 } else
178 return false;
181 @Override
182 public int hashCode() {
183 if (uri != null)
184 return uri.hashCode();
185 else if (config != null)
186 return config.hashCode();
187 return 31;