533
|
1 /**
|
|
2 *
|
|
3 */
|
|
4 package digilib.servlet;
|
|
5
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.Collections;
|
|
8 import java.util.Iterator;
|
|
9 import java.util.List;
|
|
10
|
|
11 /**
|
|
12 * @author casties
|
|
13 *
|
|
14 */
|
|
15 public class NumRange implements Iterable<Integer> {
|
|
16
|
|
17 private Integer start = 1;
|
|
18 private Integer end = Integer.MAX_VALUE;
|
|
19 private List<Integer> list = null;
|
|
20 private Integer maxnum = null;
|
|
21
|
|
22 /**
|
|
23 * @param start
|
|
24 * @param end
|
|
25 */
|
|
26 public NumRange(Integer start, Integer end) {
|
|
27 this.start = start;
|
|
28 this.end = end;
|
|
29 }
|
|
30
|
|
31 /**
|
|
32 * @param range
|
|
33 */
|
|
34 public NumRange(String range) {
|
|
35 parseString(range);
|
|
36 }
|
|
37
|
|
38 /**
|
|
39 * @param range
|
|
40 */
|
|
41 public NumRange(String range, Integer max) {
|
|
42 this.maxnum = max;
|
|
43 parseString(range);
|
|
44 }
|
|
45
|
|
46
|
|
47 public void parseString(String pages) {
|
|
48
|
|
49 ArrayList<Integer> pgs = new ArrayList<Integer>();
|
|
50
|
|
51 String intervals[] = pages.split(",");
|
|
52
|
|
53 // convert the page-interval-strings into a list containing every single
|
|
54 // page
|
|
55 for (String interval : intervals) {
|
|
56 if (interval.contains("-")) {
|
|
57 String nums[] = interval.split("-");
|
|
58 int start = Integer.valueOf(nums[0]);
|
|
59 if (nums.length > 1) {
|
|
60 // second number is end of range
|
|
61 int end = Integer.valueOf(nums[1]);
|
|
62 for (int i = start; i <= end; i++) {
|
|
63 // add all numbers to list
|
|
64 pgs.add(i);
|
|
65 }
|
|
66 } else {
|
|
67 // second number missing: range to infinity
|
|
68 pgs.add(start);
|
|
69 pgs.add(Integer.MAX_VALUE);
|
|
70 }
|
|
71 } else {
|
|
72 // single number
|
|
73 pgs.add(Integer.valueOf(interval));
|
|
74 }
|
|
75 }
|
|
76 if (intervals.length > 1) {
|
|
77 Collections.sort(pgs);
|
|
78 }
|
|
79 list = pgs;
|
|
80 }
|
|
81
|
|
82 public int getStart() {
|
|
83 if (list == null) {
|
|
84 return start;
|
|
85 } else {
|
|
86 return list.get(0);
|
|
87 }
|
|
88 }
|
|
89
|
|
90 public int getEnd() {
|
|
91 Integer last;
|
|
92 if (list == null) {
|
|
93 last = end;
|
|
94 } else {
|
|
95 last = list.get(list.size() - 1);
|
|
96 }
|
|
97 if (maxnum == null) {
|
|
98 return last;
|
|
99 } else {
|
|
100 return Math.min(last, maxnum);
|
|
101 }
|
|
102 }
|
|
103
|
|
104 public Iterator<Integer> iterator() {
|
|
105 if (list == null) {
|
|
106 // return count-based iterator
|
|
107 return new Iterator<Integer>() {
|
|
108 // anonymous inner Iterator class
|
|
109 private int num = getStart();
|
|
110 private int end = getEnd();
|
|
111
|
|
112 public boolean hasNext() {
|
|
113 return (num < end);
|
|
114 }
|
|
115
|
|
116 public Integer next() {
|
|
117 return num++;
|
|
118 }
|
|
119
|
|
120 public void remove() {
|
|
121 // don't do this
|
|
122 }
|
|
123 };
|
|
124 } else {
|
|
125 // return list-based iterator
|
|
126 return new Iterator<Integer>() {
|
|
127 // anonymous inner Iterator class
|
|
128 private int listidx = 0;
|
|
129 private int listend = list.size();
|
|
130 private int num = getStart();
|
|
131 private int end = getEnd();
|
|
132
|
|
133 public boolean hasNext() {
|
|
134 return (num < end);
|
|
135 }
|
|
136
|
|
137 public Integer next() {
|
|
138 if (listidx < listend) {
|
|
139 num = list.get(listidx++);
|
|
140 return num;
|
|
141 } else {
|
|
142 return num++;
|
|
143 }
|
|
144 }
|
|
145
|
|
146 public void remove() {
|
|
147 // don't do this
|
|
148 }
|
|
149 };
|
|
150 }
|
|
151 }
|
|
152
|
|
153 public void setMaxnum(Integer maxnum) {
|
|
154 this.maxnum = maxnum;
|
|
155 }
|
|
156
|
|
157 }
|