View Javadoc

1   /*
2    * @(#)$Id: BatchPowerSetValue.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      : 	Implements the power set parameter of the batch
7    *                  process.
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date             Who         Reason
14   *
15   * Dec 26, 2006     ukurmann    Initial Release
16   * Mar 22, 2007     bsigner     Cleanup
17   *
18   * -----------------------------------------------------------------------
19   *
20   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
21   *
22   * This software is the proprietary information of ETH Zurich.
23   * Use is subject to license terms.
24   * 
25   */
26  
27  
28  package org.ximtec.igesture.batch.core;
29  
30  import java.util.ArrayList;
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.StringTokenizer;
34  
35  import org.sigtec.util.Constant;
36  import org.ximtec.igesture.batch.BatchTools;
37  
38  
39  /**
40   * Implements the power set parameter of the batch process.
41   * 
42   * @version 1.0 Dec 2006
43   * @author Ueli Kurmann, igesture@uelikurmann.ch
44   * @author Beat Signer, signer@inf.ethz.ch
45   */
46  public class BatchPowerSetValue {
47  
48    
49     private List<String> values;
50  
51     private int min;
52  
53     private int max;
54  
55  
56     public BatchPowerSetValue() {
57        values = new ArrayList<String>();
58     }
59  
60  
61     public void addValue(String value) {
62        values.add(value);
63     } // addValue
64  
65  
66     public List<String> getValues() {
67        return values;
68     } // getValues
69  
70  
71     /**
72      * Creates the power set for the given list.
73      * 
74      * @param list the comma separated list.
75      * @param min the minimal length of the lists.
76      * @param max the maximal length of the lists.
77      * @return the list containing the power set of lists with the given
78      *         constraint.
79      */
80     public static List<String> createPowerSet(String list, int min, int max) {
81        final List<String> result = new ArrayList<String>();
82        final StringTokenizer tokenizer = new StringTokenizer(list.trim(),
83              Constant.COMMA);
84        final HashSet<String> set = new HashSet<String>();
85  
86        while (tokenizer.hasMoreTokens()) {
87           final String token = tokenizer.nextToken();
88           set.add(token);
89        }
90  
91        HashSet<HashSet<String>> powerSet = BatchTools.getPowerSet(set);
92        powerSet = BatchTools.filterSet(powerSet, min, max);
93  
94        for (final HashSet<String> hashSet : powerSet) {
95           final StringBuilder stringBuilder = new StringBuilder();
96  
97           for (final String s : hashSet) {
98              stringBuilder.append(s + Constant.COMMA);
99           }
100 
101          if (stringBuilder.length() > 0) {
102             result.add(stringBuilder.toString().substring(0,
103                   stringBuilder.length() - 1));
104          }
105          else {
106             result.add(stringBuilder.toString());
107          }
108 
109       }
110       return result;
111    } // createPowerSet
112 
113 
114    /**
115     * Sets the maximal length of the list,
116     * 
117     * @param max the maximal length of the list. The value should be not larger
118     *            than the length of the original list but it should be larger
119     *            than getMin().
120     */
121    public void setMax(int max) {
122       this.max = max;
123    } // setMax
124 
125 
126    /**
127     * Returns the maximal length of the list.
128     * 
129     * @return the maximal length of the list.
130     */
131    public int getMax() {
132       return max;
133    } // getMax
134 
135 
136    /**
137     * Sets the minimal length of the list.
138     * 
139     * @param min the minimal length of the list. The value should be larger than
140     *            0 and smaller than getMax().
141     */
142    public void setMin(int min) {
143       this.min = min;
144    } // setMin
145 
146 
147    /**
148     * Returns the minimal length of the list.
149     * 
150     * @return the minimal length of the power set. The length should be larger
151     *         than 0 and smaller than getMax().
152     */
153    public int getMin() {
154       return min;
155    } // getMin
156 
157 }