001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.mapreduce.lib.join;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Iterator;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.io.Writable;
028import org.apache.hadoop.io.WritableUtils;
029import org.apache.hadoop.util.ReflectionUtils;
030
031/**
032 * This class provides an implementation of ResetableIterator. The
033 * implementation uses an {@link java.util.ArrayList} to store elements
034 * added to it, replaying them as requested.
035 * Prefer {@link StreamBackedIterator}.
036 */
037@InterfaceAudience.Public
038@InterfaceStability.Stable
039public class ArrayListBackedIterator<X extends Writable>
040    implements ResetableIterator<X> {
041
042  private Iterator<X> iter;
043  private ArrayList<X> data;
044  private X hold = null;
045  private Configuration conf = new Configuration();
046
047  public ArrayListBackedIterator() {
048    this(new ArrayList<X>());
049  }
050
051  public ArrayListBackedIterator(ArrayList<X> data) {
052    this.data = data;
053    this.iter = this.data.iterator();
054  }
055
056  public boolean hasNext() {
057    return iter.hasNext();
058  }
059
060  public boolean next(X val) throws IOException {
061    if (iter.hasNext()) {
062      ReflectionUtils.copy(conf, iter.next(), val);
063      if (null == hold) {
064        hold = WritableUtils.clone(val, null);
065      } else {
066        ReflectionUtils.copy(conf, val, hold);
067      }
068      return true;
069    }
070    return false;
071  }
072
073  public boolean replay(X val) throws IOException {
074    ReflectionUtils.copy(conf, hold, val);
075    return true;
076  }
077
078  public void reset() {
079    iter = data.iterator();
080  }
081
082  public void add(X item) throws IOException {
083    data.add(WritableUtils.clone(item, null));
084  }
085
086  public void close() throws IOException {
087    iter = null;
088    data = null;
089  }
090
091  public void clear() {
092    data.clear();
093    reset();
094  }
095}