comparison servlet/src/digilib/util/NumRange.java @ 557:0885f5ca5b24 digilibPDF

more refactoring and rearranging pdf and image generation works now
author robcast
date Thu, 16 Dec 2010 21:19:11 +0100
parents servlet/src/digilib/servlet/NumRange.java@f140d5ee8c0b
children
comparison
equal deleted inserted replaced
556:5cc180bb0a5c 557:0885f5ca5b24
1 /**
2 *
3 */
4 package digilib.util;
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 if (intervals.length == 1) {
63 // optimized case of just one interval
64 this.start = start;
65 this.end = end;
66 this.list = null;
67 return;
68 }
69 for (int i = start; i <= end; i++) {
70 // add all numbers to list
71 pgs.add(i);
72 }
73 } else {
74 // second number missing: range to infinity
75 pgs.add(start);
76 pgs.add(Integer.MAX_VALUE);
77 }
78 } else {
79 // single number
80 pgs.add(Integer.valueOf(interval));
81 }
82 }
83 if (intervals.length > 1) {
84 Collections.sort(pgs);
85 }
86 list = pgs;
87 }
88
89 public int getStart() {
90 if (list == null) {
91 return start;
92 } else {
93 return list.get(0);
94 }
95 }
96
97 public int getEnd() {
98 Integer last;
99 if (list == null) {
100 last = end;
101 } else {
102 last = list.get(list.size() - 1);
103 }
104 if (maxnum == null) {
105 return last;
106 } else {
107 return Math.min(last, maxnum);
108 }
109 }
110
111 public Iterator<Integer> iterator() {
112 if (list == null) {
113 // return count-based iterator
114 return new Iterator<Integer>() {
115 // anonymous inner Iterator class
116 private int num = getStart();
117 private int end = getEnd();
118
119 public boolean hasNext() {
120 return (num <= end);
121 }
122
123 public Integer next() {
124 return num++;
125 }
126
127 public void remove() {
128 // don't do this
129 }
130 };
131 } else {
132 // return list-based iterator
133 return new Iterator<Integer>() {
134 // anonymous inner Iterator class
135 private int listidx = 0;
136 private int listend = list.size();
137 private int num = getStart();
138 private int end = getEnd();
139
140 public boolean hasNext() {
141 return (num <= end);
142 }
143
144 public Integer next() {
145 if (listidx < listend - 1) {
146 num = list.get(listidx++);
147 return num;
148 } else if (listidx == listend - 1) {
149 // last element in list
150 int n = list.get(listidx++);
151 if (n == Integer.MAX_VALUE) {
152 // open end -- continue
153 num++;
154 return num++;
155 } else {
156 num = n;
157 return num++;
158 }
159 } else {
160 return num++;
161 }
162 }
163
164 public void remove() {
165 // don't do this
166 }
167 };
168 }
169 }
170
171 public void setMaxnum(Integer maxnum) {
172 this.maxnum = maxnum;
173 }
174
175 }