📄 iteratorexpression.java
字号:
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.betwixt.expression;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
/** <p><code>IteratorExpression</code> returns an iterator over the current context.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.8.4.1 $
*/
public class IteratorExpression implements Expression {
/** Use this <code>Expression</code> to perform initial evaluation*/
private Expression expression;
/**
* Construct <code>IteratorExpression</code> using given expression for initial evaluation.
* @param expression this expression will be evaluated and the result converted to an
* iterator.
*/
public IteratorExpression(Expression expression) {
this.expression = expression;
}
/**
* Returns an interator over the current context
* @see org.apache.commons.betwixt.expression.Expression
*/
public Object evaluate(Context context) {
// evaluate wrapped expression against context
Object value = expression.evaluate( context );
// based on the class of the result,
// return an appropriate iterator
if ( value instanceof Iterator ) {
// if the value is an iterator, we're done
return (Iterator) value;
} else if ( value instanceof Collection ) {
// if it's a collection, return an iterator for that collection
Collection collection = (Collection) value;
return collection.iterator();
} else if ( value instanceof Map ) {
// if it's a map, return an iterator for the map entries
Map map = (Map) value;
return map.entrySet().iterator();
} else if ( value instanceof Enumeration ) {
// if it's an enumeration, wrap it in an EnumerationIterator
return new EnumerationIterator( (Enumeration) value );
} else if ( value != null ) {
// if we have an array return an ArrayIterator
Class type = value.getClass();
if ( type.isArray() ) {
return new ArrayIterator( value );
}
}
// we've got something we can't deal with
// so return an empty iterator
return Collections.EMPTY_LIST.iterator();
}
/**
* Do nothing
* @see org.apache.commons.betwixt.expression.Expression
*/
public void update(Context context, String newValue) {
// do nothing
}
/**
* Returns something useful for logging
* @return string useful for logging
*/
public String toString() {
return "IteratorExpression [expression=" + expression + "]";
}
/**
* <code>ArrayIterator</code> originated in commons-collections. Added
* as a private inner class to break dependency.
*
* @author James Strachan
* @author Mauricio S. Moura
* @author Michael A. Smith
* @author Neil O'Toole
* @author Stephen Colebourne
*/
private static final class ArrayIterator implements Iterator {
/** The array to iterate over */
protected Object array;
/** The start index to loop from */
protected int startIndex = 0;
/** The end index to loop to */
protected int endIndex = 0;
/** The current iterator index */
protected int index = 0;
// Constructors
// ----------------------------------------------------------------------
/**
* Constructor for use with <code>setArray</code>.
* <p>
* Using this constructor, the iterator is equivalent to an empty
* iterator until {@link #setArray(Object)}is called to establish the
* array to iterate over.
*/
public ArrayIterator() {
super();
}
/**
* Constructs an ArrayIterator that will iterate over the values in the
* specified array.
*
* @param array
* the array to iterate over.
* @throws IllegalArgumentException
* if <code>array</code> is not an array.
* @throws NullPointerException
* if <code>array</code> is <code>null</code>
*/
public ArrayIterator(final Object array) {
super();
setArray(array);
}
/**
* Constructs an ArrayIterator that will iterate over the values in the
* specified array from a specific start index.
*
* @param array
* the array to iterate over.
* @param startIndex
* the index to start iterating at.
* @throws IllegalArgumentException
* if <code>array</code> is not an array.
* @throws NullPointerException
* if <code>array</code> is <code>null</code>
* @throws IndexOutOfBoundsException
* if the index is invalid
*/
public ArrayIterator(final Object array, final int startIndex) {
super();
setArray(array);
checkBound(startIndex, "start");
this.startIndex = startIndex;
this.index = startIndex;
}
/**
* Construct an ArrayIterator that will iterate over a range of values
* in the specified array.
*
* @param array
* the array to iterate over.
* @param startIndex
* the index to start iterating at.
* @param endIndex
* the index to finish iterating at.
* @throws IllegalArgumentException
* if <code>array</code> is not an array.
* @throws NullPointerException
* if <code>array</code> is <code>null</code>
* @throws IndexOutOfBoundsException
* if either index is invalid
*/
public ArrayIterator(final Object array, final int startIndex,
final int endIndex) {
super();
setArray(array);
checkBound(startIndex, "start");
checkBound(endIndex, "end");
if (endIndex < startIndex) {
throw new IllegalArgumentException(
"End index must not be less than start index.");
}
this.startIndex = startIndex;
this.endIndex = endIndex;
this.index = startIndex;
}
/**
* Checks whether the index is valid or not.
*
* @param bound
* the index to check
* @param type
* the index type (for error messages)
* @throws IndexOutOfBoundsException
* if the index is invalid
*/
protected void checkBound(final int bound, final String type) {
if (bound > this.endIndex) {
throw new ArrayIndexOutOfBoundsException(
"Attempt to make an ArrayIterator that " + type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -