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