public String toString() {
return value;
<<<<<<< HEAD
}
}
}
case STRING_ITEM:
case INTEGER_ITEM:
/**
list.add(IntegerItem.ZERO);
* Represents a version list item. This class is used both for the global item list and for sub-lists (which start
* with '-(number)' in the version specification).
*/
private static class ListItem extends ArrayList- implements Item {
public int getType() {
return LIST_ITEM;
}
public boolean isNull() {
return (size() == 0);
}
void normalize() {
for (ListIterator iterator = listIterator(size()); iterator.hasPrevious(); ) {
Item item = (Item) iterator.previous();
if (item.isNull()) {
iterator.remove(); // remove null trailing items: 0, "", empty list
} else {
break;
}
}
}
public int compareTo(Item item) {
if (item == null) {
if (size() == 0) {
return 0; // 1-0 = 1- (normalize) = 1
}
Item first = (Item) get(0);
return first.compareTo(null);
}
switch (item.getType()) {
return -1; // 1-1 < 1.0.x
case STRING_ITEM:
return 1; // 1-1 > 1-sp
case LIST_ITEM:
Iterator left = iterator();
Iterator right = ((ListItem) item).iterator();
while (left.hasNext() || right.hasNext()) {
Item l = left.hasNext() ? (Item) left.next() : null;
Item r = right.hasNext() ? (Item) right.next() : null;
// if this is shorter, then invert the compare and mul with -1
int result = l == null ? -1 * r.compareTo(l) : l.compareTo(r);
if (result != 0) {
return result;
}
}
return 0;
case WILDCARD_ITEM:
return -1;
default:
throw new RuntimeException("invalid item: " + item.getClass());
}
}
public String toString() {
StringBuilder buffer = new StringBuilder("(");
for (Iterator
- iter = iterator(); iter.hasNext(); ) {
buffer.append(iter.next());
if (iter.hasNext()) {
buffer.append(',');
}
}
buffer.append(')');
return buffer.toString();
}
}
public VersionNumber(String version) {
parseVersion(version);
}
private void parseVersion(String version) {
this.value = version;
items = new ListItem();
version = version.toLowerCase(Locale.ENGLISH);
ListItem list = items;
Stack
- stack = new Stack
- ();
stack.push(list);
boolean isDigit = false;
int startIndex = 0;
for (int i = 0; i < version.length(); i++) {
char c = version.charAt(i);
if (c == '.') {
if (i == startIndex) {
} else {
}
if (size() == 0) {
return 0; // 1-0 = 1- (normalize) = 1
}
list.add(parseItem(isDigit, version.substring(startIndex, i)));
}
startIndex = i + 1;
} else if (c == '-') {
if (i == startIndex) {
list.add(IntegerItem.ZERO);
} else {
list.add(parseItem(isDigit, version.substring(startIndex, i)));
}
startIndex = i + 1;
if (isDigit) {
list.normalize(); // 1.0-* = 1-*
if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) {
// new ListItem only if previous were digits and new char is a digit,
// ie need to differentiate only 1.1 from 1-1
list.add(list = new ListItem());
stack.push(list);
}
}
} else if (c == '*') {
list.add(new WildCardItem());
startIndex = i + 1;
} else if (Character.isDigit(c)) {
if (!isDigit && i > startIndex) {
list.add(new StringItem(version.substring(startIndex, i), true));
startIndex = i;
isDigit = true;
} else if (Character.isWhitespace(c)) {
if (i > startIndex) {
if (isDigit) {
list.add(parseItem(true, version.substring(startIndex, i)));
} else {
list.add(new StringItem(version.substring(startIndex, i), true));
}
startIndex = i;
}
isDigit = false;
} else {
if (isDigit && i > startIndex) {
list.add(parseItem(true, version.substring(startIndex, i)));
startIndex = i;
}
isDigit = false;
}
}
if (version.length() > startIndex) {
list.add(parseItem(isDigit, version.substring(startIndex)));
}
while (!stack.isEmpty()) {
list = (ListItem) stack.pop();
list.normalize();
}
canonical = items.toString();
}
private static Item parseItem(boolean isDigit, String buf) {
return isDigit ? (Item) new IntegerItem(buf) : (Item) new StringItem(buf, false);
}
=======
}
}
/**
* Represents a version list item. This class is used both for the global item list and for sub-lists (which start
* with '-(number)' in the version specification).
*/
private static class ListItem extends ArrayList
- implements Item {
public int getType() {
return LIST_ITEM;
}
public boolean isNull() {
return (size() == 0);
}
void normalize() {
for (ListIterator iterator = listIterator(size()); iterator.hasPrevious(); ) {
Item item = (Item) iterator.previous();
if (item.isNull()) {
iterator.remove(); // remove null trailing items: 0, "", empty list
} else {
break;
}
}
}
public int compareTo(Item item) {
if (item == null) {
buffer.append(iter.next());
Item first = (Item) get(0);
return first.compareTo(null);
}
switch (item.getType()) {
case INTEGER_ITEM:
return -1; // 1-1 < 1.0.x
} else if (Character.isWhitespace(c)) {
return 1; // 1-1 > 1-sp
case LIST_ITEM:
Iterator left = iterator();
Iterator right = ((ListItem) item).iterator();
while (left.hasNext() || right.hasNext()) {
Item l = left.hasNext() ? (Item) left.next() : null;
Item r = right.hasNext() ? (Item) right.next() : null;
// if this is shorter, then invert the compare and mul with -1
int result = l == null ? -1 * r.compareTo(l) : l.compareTo(r);
if (result != 0) {
return result;
}
}
return 0;
case WILDCARD_ITEM:
return -1;
default:
throw new RuntimeException("invalid item: " + item.getClass());
}
}
public String toString() {
StringBuilder buffer = new StringBuilder("(");
for (Iterator
- iter = iterator(); iter.hasNext(); ) {
if (iter.hasNext()) {
buffer.append(',');
}
}
buffer.append(')');
return buffer.toString();
}
}
public VersionNumber(String version) {
parseVersion(version);
}
private void parseVersion(String version) {
this.value = version;
items = new ListItem();
version = version.toLowerCase(Locale.ENGLISH);
ListItem list = items;
Stack
- stack = new Stack
- ();
stack.push(list);
boolean isDigit = false;
int startIndex = 0;
for (int i = 0; i < version.length(); i++) {
char c = version.charAt(i);
if (c == '.') {
if (i == startIndex) {
list.add(IntegerItem.ZERO);
} else {
list.add(parseItem(isDigit, version.substring(startIndex, i)));
}
startIndex = i + 1;
} else if (c == '-') {
if (i == startIndex) {
list.add(IntegerItem.ZERO);
} else {
list.add(parseItem(isDigit, version.substring(startIndex, i)));
}
startIndex = i + 1;
if (isDigit) {
list.normalize(); // 1.0-* = 1-*
if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) {
// new ListItem only if previous were digits and new char is a digit,
// ie need to differentiate only 1.1 from 1-1
list.add(list = new ListItem());
stack.push(list);
}
}
} else if (c == '*') {
list.add(new WildCardItem());
startIndex = i + 1;
} else if (Character.isDigit(c)) {
if (!isDigit && i > startIndex) {
list.add(new StringItem(version.substring(startIndex, i), true));
startIndex = i;
}
isDigit = true;
if (i > startIndex) {
if (isDigit) {
list.add(parseItem(true, version.substring(startIndex, i)));
} else {
return isDigit ? (Item) new IntegerItem(buf) : (Item) new StringItem(buf, false);
}
>>>>>>> c61ffdd7ad2bd578a0dea618eaf1fd5dc0a20022
public int compareTo(VersionNumber o) {
list.add(new StringItem(version.substring(startIndex, i), true));
}
startIndex = i;
}
isDigit = false;
} else {
if (isDigit && i > startIndex) {
list.add(parseItem(true, version.substring(startIndex, i)));
startIndex = i;
}
isDigit = false;
}
}
if (version.length() > startIndex) {
list.add(parseItem(isDigit, version.substring(startIndex)));
}
while (!stack.isEmpty()) {
list = (ListItem) stack.pop();
list.normalize();
}
canonical = items.toString();
}
private static Item parseItem(boolean isDigit, String buf) {
return items.compareTo(o.items);
|