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 */
017package org.apache.commons.collections.primitives.adapters;
018
019import java.util.Iterator;
020
021import org.apache.commons.collections.primitives.DoubleIterator;
022
023/**
024 * Adapts a {@link java.lang.Number Number}-valued 
025 * {@link Iterator Iterator} 
026 * to the {@link DoubleIterator DoubleIterator} 
027 * interface.
028 * <p />
029 * This implementation delegates most methods
030 * to the provided {@link Iterator Iterator} 
031 * implementation in the "obvious" way.
032 *
033 * @since Commons Primitives 1.0
034 * @version $Revision: 480462 $ $Date: 2006-11-29 03:15:00 -0500 (Wed, 29 Nov 2006) $
035 * @author Rodney Waldhoff 
036 */
037public class IteratorDoubleIterator implements DoubleIterator {
038    
039    /**
040     * Create an {@link DoubleIterator DoubleIterator} wrapping
041     * the specified {@link Iterator Iterator}.  When
042     * the given <i>iterator</i> is <code>null</code>,
043     * returns <code>null</code>.
044     * 
045     * @param iterator the (possibly <code>null</code>) 
046     *        {@link Iterator Iterator} to wrap
047     * @return an {@link DoubleIterator DoubleIterator} wrapping the given 
048     *         <i>iterator</i>, or <code>null</code> when <i>iterator</i> is
049     *         <code>null</code>.
050     */
051    public static DoubleIterator wrap(Iterator iterator) {
052        return null == iterator ? null : new IteratorDoubleIterator(iterator);
053    }
054   
055    /**
056     * Creates an {@link DoubleIterator DoubleIterator} wrapping
057     * the specified {@link Iterator Iterator}.
058     * @see #wrap
059     */
060    public IteratorDoubleIterator(Iterator iterator) {
061        _iterator = iterator;
062    }
063    
064    public boolean hasNext() {
065        return _iterator.hasNext();
066    }
067    
068    public double next() {
069        return ((Number)(_iterator.next())).doubleValue();
070    }
071    
072    public void remove() {
073        _iterator.remove();
074    }
075    
076    private Iterator _iterator = null;
077
078}