]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/features/registry/GroupReference.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / features / registry / GroupReference.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.project.features.registry;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 /**
19  * A reference to a P2 feature group installable unit (IU).
20  * 
21  * <p>
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.
25  * 
26  * <p>
27  * Serializes to the format <code>ID/version</code>
28  * 
29  * @author Tuukka Lehtonen
30  */
31 public class GroupReference implements Comparable<GroupReference> {
32
33     public static final GroupReference OMNIPRESENT = new GroupReference("omnipresent");
34
35     private final String id;
36     private final String version;
37
38     /**
39      * @return
40      */
41     public GroupReference withoutVersion() {
42         return version == null ? this : new GroupReference(id);
43     }
44
45     /**
46      * @param refs
47      * @return
48      */
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() );
53         return result;
54     }
55
56     /**
57      * @param ref
58      * @return
59      */
60     public static GroupReference parse(String ref) {
61         // TODO: better validation
62         int slash = ref.indexOf('/');
63         if (slash >= 0) {
64             String id = ref.substring(0, slash);
65             String version = ref.substring(slash + 1);
66             return new GroupReference(id, version);
67         } else {
68             return new GroupReference(ref);
69         }
70     }
71
72     /**
73      * @param id
74      */
75     public GroupReference(String id) {
76         this(id, null);
77     }
78
79     /**
80      * @param id
81      * @param version
82      */
83     public GroupReference(String id, String version) {
84         if (id == null)
85             throw new NullPointerException("null feature bundle id");
86         this.id = id;
87         this.version = version;
88     }
89
90     /**
91      * @return
92      */
93     public String getId() {
94         return id;
95     }
96
97     /**
98      * @return
99      */
100     public String getVersion() {
101         return version;
102     }
103
104     @Override
105     public String toString() {
106         return version != null ? id + "/" + version : id;
107     }
108
109     @Override
110     public int hashCode() {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + id.hashCode();
114         result = prime * result + ((version == null) ? 0 : version.hashCode());
115         return result;
116     }
117
118     @Override
119     public boolean equals(Object obj) {
120         if (this == obj)
121             return true;
122         if (obj == null)
123             return false;
124         if (getClass() != obj.getClass())
125             return false;
126         GroupReference other = (GroupReference) obj;
127         if (!id.equals(other.id))
128             return false;
129         if (version == null) {
130             if (other.version != null)
131                 return false;
132         } else if (!version.equals(other.version))
133             return false;
134         return true;
135     }
136
137     /**
138      * @param obj group reference whose id to compare to this reference's
139      * @return <code>true</code> if id's are equal
140      */
141     public boolean idEquals(GroupReference other) {
142         return id.equals(other.id);
143     }
144
145     /**
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.
150      * 
151      * @param ref
152      * @return
153      */
154     public boolean includes(GroupReference request) {
155         if (!id.equals(request.id))
156             return false;
157
158         if (version == null)
159             // Any version will do.
160             return true;
161
162         // TODO: implement version checking
163
164         // ASSERT: request.version is an absolute version, not a range
165
166         // If this.version is a absolute version, check that request.version is
167         // greater or equal than this.version.
168
169         // If this.version is a version range, check that the range includes
170         // request.version.
171
172         return true;
173     }
174
175     @Override
176     public int compareTo(GroupReference o) {
177         int idDelta = id.compareTo(o.id);
178         if (idDelta != 0)
179             return idDelta;
180
181         // TODO: comparability based on version comparability
182
183         return 0;
184     }
185
186 }