-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageRange.java
More file actions
51 lines (45 loc) · 1.69 KB
/
PageRange.java
File metadata and controls
51 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package laserfiche;
import java.util.HashSet;
public class PageRange {
HashSet<Integer> set;
public PageRange(String s){
if(null == s) throw new NullPointerException("Error: input string is null");
if(s.length() <= 0) throw new IllegalArgumentException("Error: input string size shouldn't be 0");
String[] ss = s.split(",");
set = new HashSet<Integer>();
for(int i=0; i<ss.length; ++i){
boolean hasHyphen = false, hasColon=false;
if(ss[i].length()<=0) throw new IllegalArgumentException("Error: no string before comma");
if(ss[i].contains(Character.toString( ':' ))) hasHyphen=true;
if(ss[i].contains(Character.toString( '-' ))) hasColon=true;
if(hasHyphen && hasColon) throw new IllegalArgumentException("Error: can't exist Colon and Hyphen simutaneously");
if(!hasHyphen && !hasColon){
try{
set.add(Integer.valueOf(ss[i]));
}catch(NumberFormatException e){
throw new IllegalArgumentException("Error: 1 string format is wrong");
}
}else{
String[] sss = null;
if(hasHyphen) sss = ss[i].split(":");
if(hasColon) sss = ss[i].split("-");
if(sss != null && sss.length != 2) throw new IllegalArgumentException("Error: no two values with separator");
try{
int start = Integer.valueOf(sss[0]);
int end = Integer.valueOf(sss[1]);
for(int j=start; j<=end; ++j) set.add(j);
}catch(NumberFormatException e){
throw new IllegalArgumentException("Error: 2 string format is wrong");
}
}
}
}
public boolean isValid(int v){
if(null == set) return false;
return set.contains(v);
}
public static void main(String[] args){
PageRange pr = new PageRange("5-1000,1,6,10,20");
System.out.println(pr.isValid(10000));
}
}