Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 695ca9aca04f630a4082889ff0c652fa0636e8ec (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
/*******************************************************************************
 * Copyright (c) 2008, 2017 Code 9 and others.
 * 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:
 *     Code 9 - initial API and implementation
 *     EclipseSource - ongoing development
 *     Thomas Hallgren - Fix for bug 268659
 *     IBM - ongoing development
 *******************************************************************************/
package org.eclipse.equinox.p2.metadata;

import org.eclipse.equinox.internal.p2.core.helpers.StringHelper;

/**
 * An object representing a (id,version) pair. An instance of this class can be constructed
 * by invoking the constructor or calling {@link VersionedId#parse(String)} with a string 
 * representation of this VersionedID.
 * 
 * @noextend This class is not intended to be subclassed by clients.
 * 
 * @since 2.0
 */
public class VersionedId implements IVersionedId {
	private final String id;
	private final Version version;

	/**
	 * Creates and returns a new {@link VersionedId} from the given string specification.  
	 * The specification must be of the form "id/version", or just "id" if the version is absent
	 * <p>
	 * This factory method can be used to reconstruct a {@link VersionedId}
	 * instance from the string representation produced by a previous invocation of 
	 * {@link #toString()}.
	 * 
	 * @param spec the specification for the versioned id to create
	 * @return the parsed versioned id
	 * @throws IllegalArgumentException If <code>spec</code> is improperly
	 *         formatted.
	 */
	public static IVersionedId parse(String spec) {
		String[] segments = StringHelper.getArrayFromString(spec, '/');
		return new VersionedId(segments[0], segments.length == 1 ? null : segments[1]);
	}

	/**
	 * Creates a new versioned id with the given id and version.
	 * 
	 * @param id The identifier
	 * @param version The version
	 * @throws IllegalArgumentException If <code>version</code> is improperly
	 *         formatted.
	 */
	public VersionedId(String id, String version) {
		this.id = id;
		this.version = Version.parseVersion(version);
	}

	/**
	 * Creates a new versioned id with the given id and version.
	 * 
	 * @param id The identifier
	 * @param version The version
	 */
	public VersionedId(String id, Version version) {
		this.id = id;
		this.version = (version == null) ? Version.emptyVersion : version;
	}

	/**
	 * @return the ID of this VersionedID
	 */
	@Override
	public String getId() {
		return id;
	}

	/**
	 * @return the Version of this VersionedID
	 */
	@Override
	public Version getVersion() {
		return version;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (!(obj instanceof VersionedId))
			return false;

		VersionedId vname = (VersionedId) obj;
		return id.equals(vname.id) && version.equals(vname.version);
	}

	@Override
	public int hashCode() {
		return id.hashCode() * 31 + version.hashCode();
	}

	/**
	 * Returns a string representation of this versioned id.
	 * The result can be used to later construct an equal {@link VersionedId}
	 * instance using {{@link #parse(String)}.
	 * @return A string representation of this versioned id
	 */
	@Override
	public String toString() {
		return Version.emptyVersion.equals(version) ? id : id + '/' + version.toString();
	}
}

Back to the top