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