| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2017 Code 9 and others. |
| 3 |
* |
| 4 |
* 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/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* Code 9 - initial API and implementation |
| 13 |
* EclipseSource - ongoing development |
| 14 |
* Thomas Hallgren - Fix for bug 268659 |
| 15 |
* IBM - ongoing development |
| 16 |
*******************************************************************************/ |
| 17 |
package org.eclipse.equinox.p2.metadata; |
| 18 |
|
| 19 |
import org.eclipse.equinox.internal.p2.core.helpers.StringHelper; |
| 20 |
|
| 21 |
/** |
| 22 |
* An object representing a (id,version) pair. An instance of this class can be constructed |
| 23 |
* by invoking the constructor or calling {@link VersionedId#parse(String)} with a string |
| 24 |
* representation of this VersionedID. |
| 25 |
* |
| 26 |
* @noextend This class is not intended to be subclassed by clients. |
| 27 |
* |
| 28 |
* @since 2.0 |
| 29 |
*/ |
| 30 |
public class VersionedId implements IVersionedId { |
| 31 |
private final String id; |
| 32 |
private final Version version; |
| 33 |
|
| 34 |
/** |
| 35 |
* Creates and returns a new {@link VersionedId} from the given string specification. |
| 36 |
* The specification must be of the form "id/version", or just "id" if the version is absent |
| 37 |
* <p> |
| 38 |
* This factory method can be used to reconstruct a {@link VersionedId} |
| 39 |
* instance from the string representation produced by a previous invocation of |
| 40 |
* {@link #toString()}. |
| 41 |
* |
| 42 |
* @param spec the specification for the versioned id to create |
| 43 |
* @return the parsed versioned id |
| 44 |
* @throws IllegalArgumentException If <code>spec</code> is improperly |
| 45 |
* formatted. |
| 46 |
*/ |
| 47 |
public static IVersionedId parse(String spec) { |
| 48 |
String[] segments = StringHelper.getArrayFromString(spec, '/'); |
| 49 |
return new VersionedId(segments[0], segments.length == 1 ? null : segments[1]); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates a new versioned id with the given id and version. |
| 54 |
* |
| 55 |
* @param id The identifier |
| 56 |
* @param version The version |
| 57 |
* @throws IllegalArgumentException If <code>version</code> is improperly |
| 58 |
* formatted. |
| 59 |
*/ |
| 60 |
public VersionedId(String id, String version) { |
| 61 |
this.id = id; |
| 62 |
this.version = Version.parseVersion(version); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Creates a new versioned id with the given id and version. |
| 67 |
* |
| 68 |
* @param id The identifier |
| 69 |
* @param version The version |
| 70 |
*/ |
| 71 |
public VersionedId(String id, Version version) { |
| 72 |
this.id = id; |
| 73 |
this.version = (version == null) ? Version.emptyVersion : version; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return the ID of this VersionedID |
| 78 |
*/ |
| 79 |
@Override |
| 80 |
public String getId() { |
| 81 |
return id; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return the Version of this VersionedID |
| 86 |
*/ |
| 87 |
@Override |
| 88 |
public Version getVersion() { |
| 89 |
return version; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public boolean equals(Object obj) { |
| 94 |
if (this == obj) |
| 95 |
return true; |
| 96 |
|
| 97 |
if (!(obj instanceof VersionedId)) |
| 98 |
return false; |
| 99 |
|
| 100 |
VersionedId vname = (VersionedId) obj; |
| 101 |
return id.equals(vname.id) && version.equals(vname.version); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public int hashCode() { |
| 106 |
return id.hashCode() * 31 + version.hashCode(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns a string representation of this versioned id. |
| 111 |
* The result can be used to later construct an equal {@link VersionedId} |
| 112 |
* instance using {{@link #parse(String)}. |
| 113 |
* @return A string representation of this versioned id |
| 114 |
*/ |
| 115 |
@Override |
| 116 |
public String toString() { |
| 117 |
return Version.emptyVersion.equals(version) ? id : id + '/' + version.toString(); |
| 118 |
} |
| 119 |
} |