1 package org.lcsim.detector.identifier;
2
3 import java.util.Arrays;
4
5
6
7
8
9
10
11
12 public class IdentifierContext
13 implements IIdentifierContext
14 {
15 int[] indices;
16 int startIndex;
17 int endIndex;
18 boolean isRange;
19
20 public IdentifierContext(int startIndex, int endIndex)
21 {
22 if (startIndex > endIndex)
23 throw new IllegalArgumentException("startIndex bigger than endIndex!");
24 isRange = true;
25 this.startIndex = startIndex;
26 this.endIndex = endIndex;
27 indices = new int[endIndex - startIndex + 1];
28 int index = startIndex;
29 for (int i=0; i<indices.length; i++)
30 {
31 indices[i] = index;
32 ++index;
33 }
34 }
35
36 public IdentifierContext(int[] indices)
37 {
38 this.indices = new int[indices.length];
39 System.arraycopy(indices, 0, this.indices, 0, indices.length);
40 Arrays.sort(indices);
41 startIndex = indices[0];
42 endIndex = indices[indices.length - 1];
43 isRange = true;
44 for (int i=0; i<indices.length; i++)
45 {
46 if (i != indices.length - 1)
47 {
48 if (indices[i+1] != indices[i] + 1)
49 {
50
51 isRange = false;
52 break;
53 }
54 }
55 }
56 }
57
58 public int getEndIndex()
59 {
60 return endIndex;
61 }
62
63 public int getStartIndex()
64 {
65 return startIndex;
66 }
67
68 public int[] getIndices()
69 {
70 return indices;
71 }
72
73 public boolean isRange()
74 {
75 return isRange;
76 }
77
78 public boolean isValidIndex(int index)
79 {
80 return Arrays.binarySearch(indices, index) > -1;
81 }
82
83 public int getIndex(int i)
84 {
85 if (i < 0 || i > getNumberOfIndices() - 1)
86 throw new IllegalArgumentException("The index " + i + " is invalid!");
87 return indices[i];
88 }
89
90 public int getNumberOfIndices()
91 {
92 return indices.length;
93 }
94 }