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
;
23 * Data class representing selection of remote repository made by user.
24 * Selection is either a URI or a remote repository configuration.
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
{
33 private RemoteConfig config
;
35 static final RepositorySelection INVALID_SELECTION
= new RepositorySelection(
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.
43 * the new remote config. null if user chosen to specify
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$
55 * Return the selected URI.
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
68 * @return the selected URI, or <code>null</code> if there is no valid
71 public URIish
getURI(boolean pushMode
) {
72 if (isConfigSelected())
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);
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);
92 * @return the selected URI, <code>null</code> if a configuration was
95 public URIish
getURI() {
96 if (isConfigSelected())
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() {
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));
120 * @return the selected remote configuration. null if user chosen to select
123 public RemoteConfig
getConfig() {
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();
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() {
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;
162 public boolean equals(final Object obj
) {
166 if (obj
instanceof RepositorySelection
) {
167 final RepositorySelection other
= (RepositorySelection
) obj
;
168 if (uri
== null ^ other
.uri
== null)
170 if (uri
!= null && !uri
.equals(other
.uri
))
173 if (config
!= other
.config
)
182 public int hashCode() {
184 return uri
.hashCode();
185 else if (config
!= null)
186 return config
.hashCode();