View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
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 }