1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.project.features.registry;
\r
14 import java.util.Collection;
\r
15 import java.util.HashSet;
\r
16 import java.util.Set;
\r
19 * A reference to a P2 feature group installable unit (IU).
\r
22 * Contains an ID and an optional version specifier. If a version is not
\r
23 * specified, it is <code>null</code>. The version can be either a single
\r
24 * version or a version range.
\r
27 * Serializes to the format <code>ID/version</code>
\r
29 * @author Tuukka Lehtonen
\r
31 public class GroupReference implements Comparable<GroupReference> {
\r
33 public static final GroupReference OMNIPRESENT = new GroupReference("omnipresent");
\r
35 private final String id;
\r
36 private final String version;
\r
41 public GroupReference withoutVersion() {
\r
42 return version == null ? this : new GroupReference(id);
\r
49 public static Collection<GroupReference> stripVersions(Collection<GroupReference> refs) {
\r
50 Set<GroupReference> result = new HashSet<GroupReference>();
\r
51 for (GroupReference ref : refs)
\r
52 result.add( ref.withoutVersion() );
\r
60 public static GroupReference parse(String ref) {
\r
61 // TODO: better validation
\r
62 int slash = ref.indexOf('/');
\r
64 String id = ref.substring(0, slash);
\r
65 String version = ref.substring(slash + 1);
\r
66 return new GroupReference(id, version);
\r
68 return new GroupReference(ref);
\r
75 public GroupReference(String id) {
\r
83 public GroupReference(String id, String version) {
\r
85 throw new NullPointerException("null feature bundle id");
\r
87 this.version = version;
\r
93 public String getId() {
\r
100 public String getVersion() {
\r
105 public String toString() {
\r
106 return version != null ? id + "/" + version : id;
\r
110 public int hashCode() {
\r
111 final int prime = 31;
\r
113 result = prime * result + id.hashCode();
\r
114 result = prime * result + ((version == null) ? 0 : version.hashCode());
\r
119 public boolean equals(Object obj) {
\r
124 if (getClass() != obj.getClass())
\r
126 GroupReference other = (GroupReference) obj;
\r
127 if (!id.equals(other.id))
\r
129 if (version == null) {
\r
130 if (other.version != null)
\r
132 } else if (!version.equals(other.version))
\r
138 * @param obj group reference whose id to compare to this reference's
\r
139 * @return <code>true</code> if id's are equal
\r
141 public boolean idEquals(GroupReference other) {
\r
142 return id.equals(other.id);
\r
146 * Checks whether this feature reference fulfills the demand imposed by the
\r
147 * specified feature reference. Fulfillment implies matching feature ID and
\r
148 * a version match or a version range for this feature reference that includes the
\r
149 * absolute version in the specified feature reference.
\r
154 public boolean includes(GroupReference request) {
\r
155 if (!id.equals(request.id))
\r
158 if (version == null)
\r
159 // Any version will do.
\r
162 // TODO: implement version checking
\r
164 // ASSERT: request.version is an absolute version, not a range
\r
166 // If this.version is a absolute version, check that request.version is
\r
167 // greater or equal than this.version.
\r
169 // If this.version is a version range, check that the range includes
\r
170 // request.version.
\r
176 public int compareTo(GroupReference o) {
\r
177 int idDelta = id.compareTo(o.id);
\r
181 // TODO: comparability based on version comparability
\r