KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > util > StringArray


1 package org.jibx.binding.util;
2
3
4 /**
5  * Wrapper for arrays of ordered strings. This verifies the arrays and supports
6  * efficient lookups.
7  */

8
9 public class StringArray
10 {
11     /** Ordered array of strings. */
12     private final String JavaDoc[] m_list;
13     
14     /**
15      * Constructor from array of values. This checks the array values to make
16      * sure they're ordered and unique, and if they're not throws an exception.
17      * Once the array has been passed to this constructor it must not be
18      * modified by outside code.
19      *
20      * @param list array of values
21      */

22     public StringArray(String JavaDoc[] list) {
23         validateArray(list);
24         m_list = list;
25     }
26
27     /**
28      * Constructor from array of values to be added to base instance. This
29      * checks the array values to make sure they're ordered and unique, and if
30      * they're not throws an exception. Once the array has been passed to this
31      * constructor it must not be modified by outside code.
32      *
33      * @param list array of values
34      */

35     public StringArray(String JavaDoc[] list, StringArray base) {
36         validateArray(list);
37         m_list = mergeArrays(list, base.m_list);
38     }
39
40     /**
41      * Constructor from pair of base instances. This checks
42      * the array values to make sure they're ordered and unique, and if they're
43      * not throws an exception. Once the array has been passed to this
44      * constructor it must not be modified by outside code.
45      *
46      * @param array1 first base array
47      * @param array2 second base array
48      */

49     public StringArray(StringArray array1, StringArray array2) {
50         m_list = mergeArrays(array1.m_list, array2.m_list);
51     }
52     
53     /**
54      * Merge a pair of ordered arrays into a single array. The two source arrays
55      * must not contain any values in common.
56      *
57      * @param list1 first ordered array
58      * @param list2 second ordered array
59      * @return merged array
60      */

61     private String JavaDoc[] mergeArrays(String JavaDoc[] list1, String JavaDoc[] list2) {
62         String JavaDoc[] merge = new String JavaDoc[list1.length + list2.length];
63         int fill = 0;
64         int i = 0;
65         int j = 0;
66         while (i < list1.length && j < list2.length) {
67             String JavaDoc comp = list1[i];
68             int diff = list2[j].compareTo(list1[i]);
69             if (diff > 0) {
70                 merge[fill++] = list1[i++];
71             } else if (diff < 0) {
72                 merge[fill++] = list2[j++];
73             } else {
74                 throw new IllegalArgumentException JavaDoc
75                     ("Duplicate values in arrays");
76             }
77         }
78         if (i < list1.length) {
79             System.arraycopy(list1, i, merge, fill, list1.length-i);
80         }
81         if (j < list2.length) {
82             System.arraycopy(list2, j, merge, fill, list2.length-j);
83         }
84         return merge;
85     }
86
87     /**
88      * Make sure passed-in array contains values that are in order and without
89      * duplicate values.
90      *
91      * @param list
92      */

93     private void validateArray(String JavaDoc[] list) {
94         if (list.length > 0) {
95             String JavaDoc last = list[0];
96             int index = 0;
97             while (++index < list.length) {
98                 String JavaDoc comp = list[index];
99                 int diff = last.compareTo(comp);
100                 if (diff > 0) {
101                     throw new IllegalArgumentException JavaDoc
102                         ("Array values are not ordered");
103                 } else if (diff < 0) {
104                     last = comp;
105                 } else {
106                     throw new IllegalArgumentException JavaDoc
107                         ("Duplicate values in array");
108                 }
109             }
110         }
111     }
112     
113     /**
114      * Get string at a particular index in the list.
115      *
116      * @param index list index to be returned
117      * @return string at that index position
118      */

119     public String JavaDoc get(int index) {
120         return m_list[index];
121     }
122     
123     /**
124      * Find index of a particular string in the array. This does
125      * a binary search through the array values, using a pair of
126      * index bounds to track the subarray of possible matches at
127      * each iteration.
128      *
129      * @param value string to be found in list
130      * @return index of string in array, or <code>-1</code> if
131      * not present
132      */

133     public int indexOf(String JavaDoc value) {
134         int base = 0;
135         int limit = m_list.length - 1;
136         while (base <= limit) {
137             int cur = (base + limit) >> 1;
138             int diff = value.compareTo(m_list[cur]);
139             if (diff < 0) {
140                 limit = cur - 1;
141             } else if (diff > 0) {
142                 base = cur + 1;
143             } else {
144                 return cur;
145             }
146         }
147         return -1;
148     }
149     
150     /**
151      * Get number of values in array
152      *
153      * @return number of values in array
154      */

155     public int size() {
156         return m_list.length;
157     }
158 }
Popular Tags