KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > version > VersionRangeMatcher


1 package fr.jayasoft.ivy.version;
2
3 import java.util.Comparator JavaDoc;
4 import java.util.regex.Matcher JavaDoc;
5 import java.util.regex.Pattern JavaDoc;
6
7 import fr.jayasoft.ivy.ArtifactInfo;
8 import fr.jayasoft.ivy.Ivy;
9 import fr.jayasoft.ivy.IvyAware;
10 import fr.jayasoft.ivy.LatestStrategy;
11 import fr.jayasoft.ivy.ModuleRevisionId;
12
13 /**
14  * Matches version ranges:
15  * [1.0,2.0] matches all versions greater or equal to 1.0 and lower or equal to 2.0
16  * [1.0,2.0[ matches all versions greater or equal to 1.0 and lower than 2.0
17  * ]1.0,2.0] matches all versions greater than 1.0 and lower or equal to 2.0
18  * ]1.0,2.0[ matches all versions greater than 1.0 and lower than 2.0
19  * [1.0,) matches all versions greater or equal to 1.0
20  * ]1.0,) matches all versions greater than 1.0
21  * (,2.0] matches all versions lower or equal to 2.0
22  * (,2.0[ matches all versions lower than 2.0
23  *
24  * This class uses a latest strategy to compare revisions.
25  * If none is set, it uses the default one of the ivy instance set through setIvy().
26  * If neither a latest strategy nor a ivy instance is set, an IllegalStateException
27  * will be thrown when calling accept().
28  *
29  * Note that it can't work with latest time strategy, cause no time is known for the limits of the range.
30  * Therefore only purely revision based LatestStrategy can be used.
31  *
32  * @author xavier hanin
33  *
34  */

35 public class VersionRangeMatcher extends AbstractVersionMatcher implements IvyAware {
36     // todo: check these constants
37
private final static String JavaDoc OPEN_INC = "[";
38     private final static String JavaDoc OPEN_EXC = "]";
39     private final static String JavaDoc CLOSE_INC = "]";
40     private final static String JavaDoc CLOSE_EXC = "[";
41     private final static String JavaDoc LOWER_INFINITE = "(";
42     private final static String JavaDoc UPPER_INFINITE = ")";
43     private final static String JavaDoc SEPARATOR = ",";
44
45     // following patterns are built upon constants above and should not be modified
46
private final static String JavaDoc OPEN_INC_PATTERN = "\\"+OPEN_INC;
47     private final static String JavaDoc OPEN_EXC_PATTERN = "\\"+OPEN_EXC;
48     private final static String JavaDoc CLOSE_INC_PATTERN = "\\"+CLOSE_INC;
49     private final static String JavaDoc CLOSE_EXC_PATTERN = "\\"+CLOSE_EXC;
50     private final static String JavaDoc LI_PATTERN = "\\"+LOWER_INFINITE;
51     private final static String JavaDoc UI_PATTERN = "\\"+UPPER_INFINITE;
52     private final static String JavaDoc SEP_PATTERN = "\\"+SEPARATOR;
53
54     private final static String JavaDoc OPEN_PATTERN = "["+OPEN_INC_PATTERN+OPEN_EXC_PATTERN+"]";
55     private final static String JavaDoc CLOSE_PATTERN = "["+CLOSE_INC_PATTERN+CLOSE_EXC_PATTERN+"]";
56     private final static String JavaDoc ANY_NON_SPECIAL_PATTERN = "[^"+SEP_PATTERN+OPEN_INC_PATTERN+OPEN_EXC_PATTERN+CLOSE_INC_PATTERN+CLOSE_EXC_PATTERN+LI_PATTERN+UI_PATTERN+"]";
57     
58     private final static String JavaDoc FINITE_PATTERN = OPEN_PATTERN+"("+ANY_NON_SPECIAL_PATTERN+"+)"+SEP_PATTERN+"("+ANY_NON_SPECIAL_PATTERN+"+)"+CLOSE_PATTERN;
59     private final static String JavaDoc LOWER_INFINITE_PATTERN = LI_PATTERN+"\\,("+ANY_NON_SPECIAL_PATTERN+"+)"+CLOSE_PATTERN;
60     private final static String JavaDoc UPPER_INFINITE_PATTERN = OPEN_PATTERN+"("+ANY_NON_SPECIAL_PATTERN+"+)\\,"+UI_PATTERN;
61     
62     private final static Pattern JavaDoc FINITE_RANGE = Pattern.compile(FINITE_PATTERN);
63     private final static Pattern JavaDoc LOWER_INFINITE_RANGE = Pattern.compile(LOWER_INFINITE_PATTERN);
64     private final static Pattern JavaDoc UPPER_INFINITE_RANGE = Pattern.compile(UPPER_INFINITE_PATTERN);
65     private final static Pattern JavaDoc ALL_RANGE = Pattern.compile(FINITE_PATTERN+"|"+LOWER_INFINITE_PATTERN+"|"+UPPER_INFINITE_PATTERN);
66     
67     private final class MRIDArtifactInfo implements ArtifactInfo {
68         private ModuleRevisionId _mrid;
69
70         public MRIDArtifactInfo(ModuleRevisionId id) {
71             _mrid = id;
72         }
73
74         public long getLastModified() {
75             return 0;
76         }
77
78         public String JavaDoc getRevision() {
79             return _mrid.getRevision();
80         }
81     }
82
83     private final Comparator JavaDoc COMPARATOR = new Comparator JavaDoc() {
84         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
85             if (o1.equals(o2)) {
86                 return 0;
87             }
88             ArtifactInfo art1 = new MRIDArtifactInfo((ModuleRevisionId)o1);
89             ArtifactInfo art2 = new MRIDArtifactInfo((ModuleRevisionId)o2);
90             ArtifactInfo art = getLatestStrategy().findLatest(new ArtifactInfo[] {art1,art2}, null);
91             return art == art1 ? -1 : 1;
92         }
93     };
94     
95
96     private LatestStrategy _latestStrategy;
97     
98     private String JavaDoc _latestStrategyName = "default";
99
100     public VersionRangeMatcher() {
101         super("version-range");
102     }
103
104     public VersionRangeMatcher(String JavaDoc name) {
105         super(name);
106     }
107
108     public VersionRangeMatcher(String JavaDoc name, LatestStrategy strategy) {
109         super(name);
110         _latestStrategy = strategy;
111     }
112
113     public VersionRangeMatcher(String JavaDoc name, Ivy ivy) {
114         super(name);
115         setIvy(ivy);
116     }
117
118     public boolean isDynamic(ModuleRevisionId askedMrid) {
119         return ALL_RANGE.matcher(askedMrid.getRevision()).matches();
120     }
121
122     public boolean accept(ModuleRevisionId askedMrid, ModuleRevisionId foundMrid) {
123         String JavaDoc revision = askedMrid.getRevision();
124         Matcher JavaDoc m;
125         m = FINITE_RANGE.matcher(revision);
126         if (m.matches()) {
127             String JavaDoc lower = m.group(1);
128             String JavaDoc upper = m.group(2);
129             return isUpper(askedMrid, lower, foundMrid, revision.startsWith(OPEN_INC))
130             && isLower(askedMrid, upper, foundMrid, revision.endsWith(CLOSE_INC));
131         }
132         m = LOWER_INFINITE_RANGE.matcher(revision);
133         if (m.matches()) {
134             String JavaDoc upper = m.group(1);
135             return isLower(askedMrid, upper, foundMrid, revision.endsWith(CLOSE_INC));
136         }
137         m = UPPER_INFINITE_RANGE.matcher(revision);
138         if (m.matches()) {
139             String JavaDoc lower = m.group(1);
140             return isUpper(askedMrid, lower, foundMrid, revision.startsWith(OPEN_INC));
141         }
142         return false;
143     }
144
145     private boolean isLower(ModuleRevisionId askedMrid, String JavaDoc revision, ModuleRevisionId foundMrid, boolean inclusive) {
146         return COMPARATOR.compare(ModuleRevisionId.newInstance(askedMrid, revision), foundMrid) <= (inclusive ? 0 : -1);
147     }
148
149     private boolean isUpper(ModuleRevisionId askedMrid, String JavaDoc revision, ModuleRevisionId foundMrid, boolean inclusive) {
150         return COMPARATOR.compare(ModuleRevisionId.newInstance(askedMrid, revision), foundMrid) >= (inclusive ? 0 : 1);
151     }
152
153     public LatestStrategy getLatestStrategy() {
154         if (_latestStrategy == null) {
155             if (getIvy() == null) {
156                 throw new IllegalStateException JavaDoc("no ivy instance nor latest strategy configured in version range matcher "+this);
157             }
158             if (_latestStrategyName == null) {
159                 throw new IllegalStateException JavaDoc("null latest strategy defined in version range matcher "+this);
160             }
161             _latestStrategy = getIvy().getLatestStrategy(_latestStrategyName);
162             if (_latestStrategy == null) {
163                 throw new IllegalStateException JavaDoc("unknown latest strategy '"+_latestStrategyName+"' configured in version range matcher "+this);
164             }
165         }
166         return _latestStrategy;
167     }
168
169     public void setLatestStrategy(LatestStrategy latestStrategy) {
170         _latestStrategy = latestStrategy;
171     }
172     
173     public void setLatest(String JavaDoc latestStrategyName) {
174         _latestStrategyName = latestStrategyName;
175     }
176
177 }
178
Popular Tags