KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > site > ReachablePlugin


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.site;
12
13 import org.eclipse.osgi.service.resolver.VersionRange;
14 import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
15 import org.eclipse.pde.internal.build.IPDEBuildConstants;
16 import org.eclipse.update.core.*;
17 import org.osgi.framework.Version;
18
19 public class ReachablePlugin implements Comparable JavaDoc {
20     private static final Version GENERIC_VERSION = new Version(IPDEBuildConstants.GENERIC_VERSION_NUMBER);
21     private static final Version MAX_VERSION = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
22     public static final VersionRange WIDEST_RANGE = new VersionRange(new Version(0, 0, 0), true, MAX_VERSION, true);
23
24     private String JavaDoc id;
25     private VersionRange range;
26
27     public ReachablePlugin(String JavaDoc id, VersionRange range) {
28         this.id = id;
29         this.range = range;
30     }
31
32     public ReachablePlugin(IPluginEntry entry) {
33         id = entry.getVersionedIdentifier().getIdentifier();
34         Version version = new Version(entry.getVersionedIdentifier().getVersion().toString());
35         if (version.equals(GENERIC_VERSION)) {
36             range = WIDEST_RANGE;
37         } else if (version.getQualifier().endsWith(IBuildPropertiesConstants.PROPERTY_QUALIFIER)) {
38             if (version.getMicro() == 0) {
39                 range = new VersionRange(new Version(version.getMajor(), version.getMinor(), 0), true, new Version(version.getMajor(), version.getMinor() + 1, 0), false);
40             } else {
41                 range = new VersionRange(new Version(version.getMajor(), version.getMinor(), version.getMicro()), true, new Version(version.getMajor(), version.getMinor(), version.getMicro() + 1), false);
42             }
43         } else {
44             range = new VersionRange(version, true, version, true);
45         }
46     }
47
48     public ReachablePlugin(IImport existingImport) {
49         id = existingImport.getVersionedIdentifier().getIdentifier();
50         range = constructRange(new Version(existingImport.getVersionedIdentifier().toString()), existingImport.getRule());
51     }
52
53     private VersionRange constructRange(Version initialValue, int ruleCode) {
54         switch (ruleCode) {
55             case IUpdateConstants.RULE_NONE :
56             case IUpdateConstants.RULE_EQUIVALENT : //[1.0.0, 1.1.0)
57
return new VersionRange(initialValue, true, new Version(initialValue.getMajor(), initialValue.getMinor() + 1, 0), false);
58
59             case IUpdateConstants.RULE_PERFECT : //[1.0.0, 1.0.0]
60
return new VersionRange(initialValue, true, initialValue, true);
61
62             case IUpdateConstants.RULE_COMPATIBLE : //[1.1.0, 2.0.0)
63
return new VersionRange(initialValue, true, new Version(initialValue.getMajor() + 1, 0, 0), false);
64
65             case IUpdateConstants.RULE_GREATER_OR_EQUAL ://[1.0.0, 999.999.999)
66
return new VersionRange(initialValue, true, new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE), true);
67             default :
68                 return null;
69         }
70
71     }
72
73     public String JavaDoc getId() {
74         return id;
75     }
76
77     public VersionRange getRange() {
78         return range;
79     }
80
81     public int compareTo(Object JavaDoc o) {
82         if (o instanceof ReachablePlugin) {
83             ReachablePlugin toCompare = (ReachablePlugin) o;
84             int result = id.compareTo(toCompare.id);
85             if (result != 0)
86                 return result;
87             //We want the object with the widest version range to sort first
88
result = substract(toCompare.range.getMaximum(), toCompare.range.getMinimum()).compareTo(substract(range.getMaximum(), range.getMinimum()));
89             if (result != 0)
90                 return result;
91             if (this.equals(o))
92                 return 0;
93             result = range.getMinimum().compareTo(toCompare.range.getMaximum());
94             if (result != 0)
95                 return result;
96             //Give up
97
return -1;
98         }
99         return -1;
100     }
101
102     private Version substract(Version v1, Version v2) { //v1 - v2 where v1 is always greater or equals to v2
103
int major, minor, micro = 0;
104         int carry = 0;
105         if (v1.getMicro() < v2.getMicro()) {
106             micro = Integer.MAX_VALUE - v2.getMicro() + v1.getMicro();
107             carry = 1;
108         } else {
109             micro = v1.getMicro() - v2.getMicro();
110             carry = 0;
111         }
112         if (v1.getMinor() < v2.getMinor() + carry) {
113             minor = Integer.MAX_VALUE - (v2.getMinor() + carry) + v1.getMinor();
114             carry = 1;
115         } else {
116             minor = v1.getMinor() - (v2.getMinor() + carry);
117             carry = 0;
118         }
119         if (v1.getMajor() < v2.getMajor() + carry) {
120             major = Integer.MAX_VALUE - (v2.getMajor() + carry) + v1.getMajor();
121             carry = 1;
122         } else {
123             major = v1.getMajor() - (v2.getMajor() + carry);
124             carry = 0;
125         }
126         return new Version(major, minor, micro);
127     }
128
129     public boolean equals(Object JavaDoc obj) {
130         if (obj instanceof ReachablePlugin) {
131             ReachablePlugin toCompare = (ReachablePlugin) obj;
132             if (!id.equals(toCompare.id))
133                 return false;
134             if (range.getIncludeMinimum() != toCompare.range.getIncludeMinimum())
135                 return false;
136             if (range.getIncludeMaximum() != toCompare.range.getIncludeMaximum())
137                 return false;
138             return range.getMinimum().equals(toCompare.range.getMinimum()) && range.getMaximum().equals(toCompare.range.getMaximum());
139         }
140         return false;
141     }
142
143     public int hashCode() {
144         return id.hashCode() + range.hashCode() * 17;
145     }
146
147     public String JavaDoc toString() {
148         return id + ' ' + range.toString();
149     }
150 }
151
Popular Tags