org.apache.jackrabbit.spi.commons.iterator.Iterators Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.jackrabbit.spi.commons.iterator;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.apache.commons.collections.iterators.EmptyIterator;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.collections.iterators.IteratorChain;
import org.apache.commons.collections.iterators.SingletonIterator;
import org.apache.commons.collections.iterators.TransformIterator;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import java.util.Collection;
import java.util.Iterator;
/**
* Utility class containing type safe adapters for some of the iterators of
* commons-collections.
*/
public final class Iterators {
private Iterators() {
super();
}
/**
* Returns an iterator containing the single element element
of
* type T
.
*
* @param
* @param element
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator singleton(T element) {
return new SingletonIterator(element);
}
/**
* Returns an empty iterator of type T
.
*
* @param
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator empty() {
return EmptyIterator.INSTANCE;
}
/**
* Returns an iterator for the concatenation of iterator1
and
* iterator2
.
*
* @param
* @param iterator1
* @param iterator2
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator iteratorChain(Iterator iterator1, Iterator iterator2) {
return new IteratorChain(iterator1, iterator2);
}
/**
* Returns an iterator for the concatenation of all the given iterators
.
*
* @param
* @param iterators
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator iteratorChain(Iterator[] iterators) {
return new IteratorChain(iterators);
}
/**
* Returns an iterator for the concatenation of all the given iterators
.
*
* @param
* @param iterators
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator iteratorChain(Collection iterators) {
return new IteratorChain(iterators);
}
/**
* Returns an iterator for elements of an array of values
.
*
* @param
* @param values the array to iterate over.
* @param from the index to start iterating at.
* @param to the index to finish iterating at.
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator arrayIterator(T[] values, int from, int to) {
return new ArrayIterator(values, from, to);
}
/**
* Returns an iterator with elements from an original iterator
where the
* given predicate
matches removed.
*
* @param
* @param iterator
* @param predicate
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator filterIterator(Iterator iterator,
final Predicate predicate) {
return new FilterIterator(iterator, new org.apache.commons.collections.Predicate() {
public boolean evaluate(Object object) {
return predicate.evaluate((T) object);
}
});
}
/**
* Returns an iterator with elements of an original iterator
transformed by
* a transformer
.
*
* @param
* @param
* @param
* @param iterator
* @param transformer
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator transformIterator(Iterator iterator,
final Transformer transformer) {
return new TransformIterator(iterator, new org.apache.commons.collections.Transformer() {
public Object transform(Object input) {
return transformer.transform((S) input);
}
});
}
/**
* Returns an iterator of {@link Property} from a {@link PropertyIterator}.
*
* @param propertyIterator
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator properties(PropertyIterator propertyIterator) {
return propertyIterator;
}
/**
* Returns an iterator of {@link Node} from a {@link NodeIterator}.
* @param nodeIterator
* @return
*/
@SuppressWarnings("unchecked")
public static Iterator nodes(NodeIterator nodeIterator) {
return nodeIterator;
}
}