1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.parser.action;
23 
24 import java.util.Iterator;
25 
26 /** Iterate over two sources of actions, both given as an Iterable<IAction>
27  *  object that can be null.
28 */
29 public class ActionIterator implements Iterable<IAction>
30 {
ActionIterator( final Iterable<IAction> aOneStateActions, final Iterable<IAction> aAllStateActions)31     public ActionIterator (
32         final Iterable<IAction> aOneStateActions,
33         final Iterable<IAction> aAllStateActions)
34     {
35         maOneStateActions = aOneStateActions;
36         maAllStateActions = aAllStateActions;
37     }
38 
39 
40 
41 
iterator()42     @Override public Iterator<IAction> iterator()
43     {
44         return new Iterator<IAction>()
45         {
46             Iterator<IAction> maIterator = null;
47             int mnPhase = 0;
48 
49             @Override
50             public boolean hasNext()
51             {
52                 while(true)
53                 {
54                     if (mnPhase == 2)
55                         return false;
56                     else if (mnPhase == 0)
57                     {
58                         if (maIterator == null)
59                             if (maOneStateActions == null)
60                             {
61                                 mnPhase = 1;
62                                 continue;
63                             }
64                             else
65                                 maIterator = maOneStateActions.iterator();
66                         if (maIterator.hasNext())
67                             return true;
68                         else
69                         {
70                             maIterator = null;
71                             mnPhase = 1;
72                         }
73                     }
74                     else if (mnPhase == 1)
75                     {
76                         if (maIterator == null)
77                             if (maAllStateActions == null)
78                             {
79                                 mnPhase = 2;
80                                 return false;
81                             }
82                             else
83                                 maIterator = maAllStateActions.iterator();
84                         if (maIterator.hasNext())
85                             return true;
86                         else
87                         {
88                             mnPhase = 2;
89                         }
90                     }
91                 }
92             }
93 
94 
95 
96 
97             @Override
98             public IAction next()
99             {
100                 return maIterator.next();
101             }
102 
103 
104 
105 
106             @Override
107             public void remove()
108             {
109             }
110         };
111     }
112 
113 
114 
115 
116     private final Iterable<IAction> maOneStateActions;
117     private final Iterable<IAction> maAllStateActions;
118 }
119