📄 rangeenumerator.java
字号:
package dli2fe.helpers;
/**
* Title: Digial Library Interoperable Interface Fudan Edition
* Description: This project contains all the classes required for DLI2FE interface. Developers use these classes to implement the wrapper and client side codes. The basic functions of DLI2FE is as follows:
* Search: Search a digital library source site.
* Metadata: Fetch metadata for a site.
* ResultAccess: Get results for a given query.
* DLI2FE uses Dublin Core as the basic attribute model, DAV/DASL as the general XML-based query language and CORBA as distributed object transportation mechanism.
* Copyright: Copyright (c) 2001
* Company: Fudan University
* @author Carl Tao
* @version 1.0
*/
/**
* Enumerates integers using one-based selection.
* Examples:
* "1-" all positive integers
* "1,3-5,7" = "1,3,4,5,7"
* next() or nextRange() must be called before calling get() for the first time
*/
public class RangeEnumerator {
public static final int NONE = 0;
public static final int INFINITY = -1;
/** current value */
int value = 0;
/** limit of "5-7" would be 7 */
int limit = NONE;
/** position in the selection string */
int pos = 0;
/** selection range */
String range;
public RangeEnumerator(String selection) {
range = selection;
}
/**
* moves the pointer to the next position
* false if no more
*/
public synchronized boolean next() {
if(value < 0)
return false;
else if(limit == INFINITY || value < limit) {
value++;
return true;
}
limit = NONE;
try {
// integer expected
int n = readNextInt();
if(n > 0) {
value = n;
if(pos >= range.length())
return true; // last number in range
else {
char c = range.charAt(pos++);
if(c == '-') {
// determine the limit
if(pos >= range.length()) {
limit = INFINITY;
return true;
} else {
limit = readNextInt();
// skip the comma if any; if none - end of string reached
pos++;
if(limit >= n)
return true;
else
value = -1;
}
} else if(c == ',') {
// reset limit
limit = NONE;
return true;
} // else error
}
}
} catch (Exception any) {
}
// error
value = -1;
return false;
}
/**
* moves the pointer to the next range
* e.g., if range is 1-3,7-9,15- and get() returns 8, after nextRange(),
* get() will return 15. For all other cases, it behaves the same as next()
* false if no more
*/
public synchronized boolean nextRange() {
if (limit != NONE && limit != INFINITY) {
value = limit;
}
return this.next();
}
int readNextInt() {
int p = pos;
char c = (char)0;
// System.err.println("HERE: " + pos);
while(pos < range.length()) {
c = range.charAt(pos);
if((int)c < (int)'0' || (int)c > (int)'9')
break;
else
pos++;
}
// System.err.println("p: " + p + ", pos: " + pos + ", "); // + range.substring(pos, p);
if(pos > p)
return Integer.parseInt(range.substring(p, pos));
// else if (pos == range.length() - 1)
// return Integer.parseInt(range.substring(p));
else
throw new RuntimeException();
}
/**
* Gets the current position
* -1: invalid position
*/
public synchronized int get() {
return value;
}
/**
* Gets the current position
* -1: invalid position
*/
public synchronized int limit() {
return limit;
}
/**
* Sets the current position
*/
public synchronized void set(int nv) {
value = 0;
pos = 0;
while(true) {
next();
if(value < 0)
return; // error
if(nv < value) {
value = -1; // out of range on the left
return;
} else if( nv == value || (limit == INFINITY || nv <= limit) ) {
// within range
value = nv - 1;
return;
} // nv > value && (limit == NONE || limit < nv) => continue
}
}
public String toString() {
return range;
}
public static void main(String[] args) throws Exception {
RangeEnumerator re = new RangeEnumerator(args[0]);
if(args.length > 1)
re.set(Integer.parseInt(args[1]));
while(re.next())
System.out.println("Next: " + re.get());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -