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.hbase.procedure2.store;
019
020import java.io.IOException;
021import java.util.Iterator;
022import java.util.List;
023import java.util.NoSuchElementException;
024import org.apache.hadoop.hbase.procedure2.Procedure;
025import org.apache.hadoop.hbase.procedure2.ProcedureUtil;
026import org.apache.hadoop.hbase.procedure2.store.ProcedureStore.ProcedureIterator;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * A procedure iterator which holds all the procedure protos in memory. For fast access.
031 */
032@InterfaceAudience.Private
033public class InMemoryProcedureIterator implements ProcedureIterator {
034
035  private final List<ProtoAndProcedure> procs;
036
037  private Iterator<ProtoAndProcedure> iter;
038
039  private ProtoAndProcedure current;
040
041  public InMemoryProcedureIterator(List<ProtoAndProcedure> procs) {
042    this.procs = procs;
043    reset();
044  }
045
046  @Override
047  public void reset() {
048    iter = procs.iterator();
049    if (iter.hasNext()) {
050      current = iter.next();
051    } else {
052      current = null;
053    }
054  }
055
056  @Override
057  public boolean hasNext() {
058    return current != null;
059  }
060
061  private void checkNext() {
062    if (!hasNext()) {
063      throw new NoSuchElementException();
064    }
065  }
066
067  @Override
068  public boolean isNextFinished() {
069    checkNext();
070    return ProcedureUtil.isFinished(current.getProto());
071  }
072
073  private void moveToNext() {
074    if (iter.hasNext()) {
075      current = iter.next();
076    } else {
077      current = null;
078    }
079  }
080
081  @Override
082  public void skipNext() {
083    checkNext();
084    moveToNext();
085  }
086
087  @Override
088  public Procedure<?> next() throws IOException {
089    checkNext();
090    Procedure<?> proc = current.getProcedure();
091    moveToNext();
092    return proc;
093  }
094}