1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.util;
25
26 import java.util.*;
27
28
29 public class Selector implements Enumeration {
30 Object[][] data;
31 int count = 0;
32
33 public Selector(Object[] input) {
34 Assert.notNull(input);
35 int m = 1;
36 Vector v;
37 for( int i = 0; i < input.length; i++ ) {
38 if ( input[i] instanceof Vector )
39 m *= ((Vector)input[i]).size();
40 else if ( input[i] instanceof Object[] )
41 m *= ((Object[])input[i]).length;
42 else
43 Assert.notNull(null);
44 }
45 count = 0;
46 data = new Object[m][input.length];
47 Object[] choice = new Object[input.length];
48 select(0,input,choice);
49 count = 0;
50 }
51
52 public boolean hasMoreElements() {
53 return count < data.length;
54 }
55
56 public Object nextElement() {
57 return data[count++];
58 }
59
60 public void reset() {
61 count = 0;
62 }
63
64 protected void select(int position, Object[] input, Object[] choice) {
65 if ( position >= input.length ) {
66 for( int i = 0; i < input.length; i++ )
67 data[count][i] = choice[i];
68 count++;
69 return;
70 }
71
72 if ( input[position] instanceof Vector ) {
73 Vector v = (Vector)input[position];
74 for( int i = 0; i < v.size(); i++ )
75 {
76 choice[position] = v.elementAt(i);
77 select(position+1,input,choice);
78 }
79 }
80 else if ( input[position] instanceof Object[] ) {
81 Object[] v = (Object[])input[position];
82 for( int i = 0; i < v.length; i++ ) {
83 choice[position] = v[i];
84 select(position+1,input,choice);
85 }
86 }
87 }
88
89 public static void main(String[] arg) {
90 String[][] data = { {"a", "b", "c"},
91 {"1", "2", "3", "4"},
92 {"u", "v", "x", "y", "z"}
93 };
94
95 Selector selector = new Selector(data);
96 int i = 1;
97 while( selector.hasMoreElements() ) {
98 Object[] results = (Object[])selector.nextElement();
99 System.out.print((i++) + " ");
100 for( int j = 0; j < results.length; j++ )
101 System.out.print((String)results[j] + " ");
102 System.out.println();
103 }
104 }
105 }