View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.profiler.impl;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.apache.jetspeed.profiler.ProfileLocatorProperty;
23  import org.apache.jetspeed.profiler.rules.RuleCriterion;
24  
25  /***
26   * ProfileFallbackIterator
27   *
28   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
29   * @version $Id: ProfileFallbackIterator.java 516448 2007-03-09 16:25:47Z ate $
30   */
31  public class ProfileFallbackIterator implements Iterator
32  {
33      private ProfileLocatorControl locator;
34      private int last = 0;
35      private int state = RuleCriterion.FALLBACK_CONTINUE;     
36      private ProfileFallbackIterator()
37      {
38      }
39      
40      public ProfileFallbackIterator(ProfileLocatorControl locator)
41      {
42          this.locator = locator;
43          last = locator.getElements().size() - 1;
44      }
45      
46      /* (non-Javadoc)
47       * @see java.util.Iterator#remove()
48       */
49      public void remove()
50      {
51          // TODO Auto-generated method stub
52      }
53      
54      /* (non-Javadoc)
55       * @see java.util.Iterator#hasNext()
56       */
57      public boolean hasNext()
58      {
59          boolean hasNext = false;
60          
61          List elements = locator.getElements();
62          
63          if (last < 0 || last >= elements.size())
64          {
65              state = RuleCriterion.FALLBACK_STOP;
66              return false;
67          }
68          
69          if (state == RuleCriterion.FALLBACK_STOP)
70          {
71              hasNext = false;
72          }        
73          else if (state == RuleCriterion.FALLBACK_CONTINUE ||
74                   state == RuleCriterion.FALLBACK_LOOP)
75          {
76              hasNext = true;
77          }
78          
79          return hasNext;
80      }
81      
82      /* (non-Javadoc)
83       * @see java.util.Iterator#next()
84       */
85      public Object next()
86      {
87          ProfileLocatorProperty [] properties = null;
88  
89          if (last >= 0)
90          {
91              // generate properties list to return
92              List elements = locator.getElements();
93              properties = new ProfileLocatorProperty[last+1];
94              ProfileLocatorProperty lastElement = null;
95              Iterator it = elements.listIterator();
96              for (int count = 0; (count <= last) && it.hasNext(); count++)
97              {
98                  lastElement = (ProfileLocatorProperty)it.next();
99                  properties[count] = lastElement;
100             }
101 
102             // modify iterator state based on fallback type;
103             // performed here to prevent multiple calls to
104             // hasNext() from changing iterator state
105             state = lastElement.getFallbackType();
106             last--;
107         }
108 
109         return properties;
110     }
111 }