001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.profiler.impl;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.jetspeed.profiler.ProfileLocatorProperty;
023: import org.apache.jetspeed.profiler.rules.RuleCriterion;
024:
025: /**
026: * ProfileFallbackIterator
027: *
028: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
029: * @version $Id: ProfileFallbackIterator.java 516448 2007-03-09 16:25:47Z ate $
030: */
031: public class ProfileFallbackIterator implements Iterator {
032: private ProfileLocatorControl locator;
033: private int last = 0;
034: private int state = RuleCriterion.FALLBACK_CONTINUE;
035:
036: private ProfileFallbackIterator() {
037: }
038:
039: public ProfileFallbackIterator(ProfileLocatorControl locator) {
040: this .locator = locator;
041: last = locator.getElements().size() - 1;
042: }
043:
044: /* (non-Javadoc)
045: * @see java.util.Iterator#remove()
046: */
047: public void remove() {
048: // TODO Auto-generated method stub
049: }
050:
051: /* (non-Javadoc)
052: * @see java.util.Iterator#hasNext()
053: */
054: public boolean hasNext() {
055: boolean hasNext = false;
056:
057: List elements = locator.getElements();
058:
059: if (last < 0 || last >= elements.size()) {
060: state = RuleCriterion.FALLBACK_STOP;
061: return false;
062: }
063:
064: if (state == RuleCriterion.FALLBACK_STOP) {
065: hasNext = false;
066: } else if (state == RuleCriterion.FALLBACK_CONTINUE
067: || state == RuleCriterion.FALLBACK_LOOP) {
068: hasNext = true;
069: }
070:
071: return hasNext;
072: }
073:
074: /* (non-Javadoc)
075: * @see java.util.Iterator#next()
076: */
077: public Object next() {
078: ProfileLocatorProperty[] properties = null;
079:
080: if (last >= 0) {
081: // generate properties list to return
082: List elements = locator.getElements();
083: properties = new ProfileLocatorProperty[last + 1];
084: ProfileLocatorProperty lastElement = null;
085: Iterator it = elements.listIterator();
086: for (int count = 0; (count <= last) && it.hasNext(); count++) {
087: lastElement = (ProfileLocatorProperty) it.next();
088: properties[count] = lastElement;
089: }
090:
091: // modify iterator state based on fallback type;
092: // performed here to prevent multiple calls to
093: // hasNext() from changing iterator state
094: state = lastElement.getFallbackType();
095: last--;
096: }
097:
098: return properties;
099: }
100: }
|