org.protege.osgi.framework.NameWithVersion Maven / Gradle / Ivy
package org.protege.osgi.framework;
import com.google.common.collect.Ordering;
import org.osgi.framework.Version;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 08/09/15
*/
public class NameWithVersion implements Comparable {
private final static Pattern VERSION_NUMBER_PATTERN = Pattern.compile("[0-9]+(\\.[0-9]+)*");
private final String fileName;
public NameWithVersion(String fileName) {
this.fileName = fileName;
}
private List split() {
Matcher matcher = VERSION_NUMBER_PATTERN.matcher(fileName);
int prevEnd = 0;
List components = new ArrayList<>();
while (matcher.find()) {
int start = matcher.start();
if (prevEnd != start) {
String prev = fileName.substring(prevEnd, start);
components.add(new StringComponent(prev));
}
prevEnd = matcher.end();
String versionComponents = fileName.substring(matcher.start(), matcher.end());
components.add(new VersionComponent(new Version(versionComponents)));
}
if (prevEnd < fileName.length()) {
components.add(new StringComponent(fileName.substring(prevEnd + 1)));
}
return components;
}
@Override
public String toString() {
return toStringHelper("PluginFileName")
.addValue(fileName)
.toString();
}
@Override
public int compareTo(NameWithVersion o) {
return Ordering.natural().lexicographical().compare(this.split(), o.split());
}
private abstract static class NameComponent implements Comparable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy