Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29c2d93c8b19e65d3e5adb3c8c178e2ab15054e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Repository id
 */
public class RepositoryId implements IRepositoryIdProvider, Serializable {

	/** serialVersionUID */
	private static final long serialVersionUID = -57313931704393200L;

	/**
	 * Create repository from url.
	 *
	 * @see #createFromId(String)
	 * @param url
	 * @return repository or null if parsing fails
	 */
	public static RepositoryId createFromUrl(URL url) {
		return url != null ? createFromId(url.getPath()) : null;
	}

	/**
	 * Create repository from id. The id is split on the '/' character and the
	 * first two non-empty segments are interpreted to be the repository owner
	 * and name.
	 *
	 * @param id
	 * @return repository
	 */
	public static RepositoryId createFromId(String id) {
		if (id == null || id.length() == 0)
			return null;
		String owner = null;
		String name = null;
		for (String segment : id.split("/")) //$NON-NLS-1$
			if (segment.length() > 0)
				if (owner == null)
					owner = segment;
				else if (name == null)
					name = segment;
				else
					break;

		return owner != null && owner.length() > 0 && name != null
				&& name.length() > 0 ? new RepositoryId(owner, name) : null;
	}

	/**
	 * Create from string URL
	 *
	 * @see #createFromUrl(URL)
	 * @param url
	 * @return repository or null if it could not be parsed from URL path
	 */
	public static RepositoryId createFromUrl(String url) {
		if (url == null || url.length() == 0)
			return null;
		try {
			return createFromUrl(new URL(url));
		} catch (MalformedURLException e) {
			return null;
		}
	}

	/**
	 * Create repository id from given owner and name.
	 *
	 * @param owner
	 * @param name
	 * @return repository id
	 */
	public static RepositoryId create(String owner, String name) {
		return new RepositoryId(owner, name);
	}

	private final String owner;

	private final String name;

	/**
	 * Create repository id with given owner and name. This constructor
	 * validates the parameters and throws an {@link IllegalArgumentException}
	 * if either is null or empty.
	 *
	 * @param owner
	 *            must be non-null and non-empty
	 * @param name
	 *            must be non-null and non-empty
	 */
	public RepositoryId(final String owner, final String name) {
		if (owner == null)
			throw new IllegalArgumentException("Owner cannot be null"); //$NON-NLS-1$
		if (owner.length() == 0)
			throw new IllegalArgumentException("Owner cannot be empty"); //$NON-NLS-1$
		if (name == null)
			throw new IllegalArgumentException("Name cannot be null"); //$NON-NLS-1$
		if (name.length() == 0)
			throw new IllegalArgumentException("Name cannot be empty"); //$NON-NLS-1$

		this.owner = owner;
		this.name = name;
	}

	/**
	 * @return owner
	 */
	public String getOwner() {
		return owner;
	}

	/**
	 * @return name
	 */
	public String getName() {
		return name;
	}

	public String generateId() {
		return owner + "/" + name; //$NON-NLS-1$
	}

	@Override
	public int hashCode() {
		return generateId().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof RepositoryId))
			return false;
		RepositoryId other = (RepositoryId) obj;
		return name.equals(other.name) && owner.equals(other.owner);
	}

	@Override
	public String toString() {
		return generateId();
	}
}

Back to the top