This project has retired. For details please refer to its Attic page.
RepeatUntilBlock xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.block_app.framework.block;
19  
20  import java.util.Iterator;
21  
22  import org.apache.giraph.block_app.framework.piece.AbstractPiece;
23  import org.apache.giraph.function.Consumer;
24  import org.apache.giraph.function.Supplier;
25  
26  import com.google.common.collect.AbstractIterator;
27  import com.google.common.collect.Iterators;
28  
29  /**
30   * Block that repeats another block until toQuit supplier returns true,
31   * but at most given number of times.
32   *
33   * If toQuit returns true on first run, block is not going
34   * to be executed at all.
35   */
36  @SuppressWarnings("rawtypes")
37  public final class RepeatUntilBlock implements Block {
38    private final Block block;
39    private final int repeatTimes;
40    private final Supplier<Boolean> toQuit;
41  
42    public RepeatUntilBlock(
43        int repeatTimes, Block block, Supplier<Boolean> toQuit) {
44      this.block = block;
45      this.repeatTimes = repeatTimes;
46      this.toQuit = toQuit;
47    }
48  
49    /**
50     * Repeat unlimited number of times, until toQuit supplier returns true.
51     */
52    public static Block unlimited(Block block, Supplier<Boolean> toQuit) {
53      return new RepeatUntilBlock(Integer.MAX_VALUE, block, toQuit);
54    }
55  
56    @Override
57    public Iterator<AbstractPiece> iterator() {
58      return Iterators.concat(new AbstractIterator<Iterator<AbstractPiece>>() {
59        private int index = 0;
60  
61        @Override
62        protected Iterator<AbstractPiece> computeNext() {
63          if (index >= repeatTimes || Boolean.TRUE.equals(toQuit.get())) {
64            return endOfData();
65          }
66          index++;
67          return block.iterator();
68        }
69      });
70    }
71  
72    @Override
73    public void forAllPossiblePieces(Consumer<AbstractPiece> consumer) {
74      block.forAllPossiblePieces(consumer);
75    }
76  
77    @Override
78    public PieceCount getPieceCount() {
79      return PieceCount.createUnknownCount();
80    }
81  
82    @Override
83    public String toString() {
84      return "RepeatUntilBlock(" + repeatTimes + " * " + block + ")";
85    }
86  }