SimpleMultiplier.java revision 3233:b5d08bc0d224
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package sampleapi.util;
25
26import java.util.StringTokenizer;
27import java.util.ArrayList;
28
29/*
30 * The class implements unknown number of nested loops. The number of modifiers
31 * in class/interface definitions could be any size. Annotations are also multiplied
32 * by this class.
33 * That is, dataset xml can provide any number of modifier sets, and generator should
34 * iterate through all possible modifiers, annotations and types combinations.
35 *
36 * For example, class definition xml provides 3 modifiers sets:
37 *
38 * "public,private"
39 * "static"
40 * "final,abstract"
41 *
42 * and one types set "void,int"
43 *
44 * the class will generate the sequence like:
45 * "public static final void"
46 * "public static final int"
47 * "public static abstract void"
48 * "public static abstract int"
49 * "private static final void"
50 * "private static final int"
51 * "private static abstract void"
52 * "private static abstract int".
53 *
54 * This sequence could be processed by just one loop instead of four.
55 *
56 * In other places where the number of possible positions are known,
57 * the generator uses nested loops instead.
58 */
59public class SimpleMultiplier {
60
61    ArrayList<ArrayList<String>> valueSpace = new ArrayList<>();
62
63    int size = 0;
64    int index = 0;
65
66    public void addAxis(String values) {
67        ArrayList<String> valueAxis = new ArrayList<>();
68        StringTokenizer valuesTokens = new StringTokenizer(values, "|");
69        while (valuesTokens.hasMoreTokens())
70            valueAxis.add(valuesTokens.nextToken());
71        valueSpace.add(valueAxis);
72    }
73
74    public void initIterator() {
75        if (!valueSpace.isEmpty()) {
76            size = 1;
77            for (int i = 0; i < valueSpace.size(); i++)
78                size *= valueSpace.get(i).size();
79        }
80        index = 0;
81    }
82
83    public boolean hasNext() {
84        return index < size;
85    }
86
87    public ArrayList<String> getNext() {
88        ArrayList<String> next = new ArrayList<>();
89        int positionIndex = index;
90
91        // last added changing faster
92        for (int i = valueSpace.size() - 1; i >= 0; i--) {
93            ArrayList<String> valueAxis = valueSpace.get(i);
94            int axisSize = valueAxis.size();
95            next.add(valueAxis.get(positionIndex % axisSize));
96            positionIndex /= axisSize;
97        }
98        index += 1;
99
100        return next;
101    }
102}
103