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.Collections;
21  import java.util.Iterator;
22  
23  import org.apache.giraph.block_app.framework.piece.AbstractPiece;
24  import org.apache.giraph.function.Consumer;
25  import org.apache.giraph.function.primitive.IntSupplier;
26  
27  import com.google.common.collect.Iterables;
28  
29  /**
30   * Block that repeats another block given number of times.
31   */
32  @SuppressWarnings("rawtypes")
33  public final class RepeatBlock implements Block {
34    private final Block block;
35    private final IntSupplier repeatTimes;
36  
37    public RepeatBlock(final int repeatTimes, Block block) {
38      this.block = block;
39      this.repeatTimes = new IntSupplier() {
40        @Override
41        public int get() {
42          return repeatTimes;
43        }
44      };
45    }
46  
47    /**
48     * Creates a repeat block, that before starting execution takes number of
49     * iterations from the given supplier.
50     *
51     * This allows number of iterations to be dynamic, and depend on
52     * execution that happens before.
53     * Note - it doesn't allow for number of repetitions to change during the
54     * loop itself - as it is supplier is called only when this block gets
55     * its turn.
56     */
57    public RepeatBlock(IntSupplier repeatTimes, Block block) {
58      this.block = block;
59      this.repeatTimes = repeatTimes;
60    }
61  
62    /**
63     * Create a repeat block that executes unlimited number of times.
64     *
65     * Should rarely be used, as it will cause application never to finish,
66     * unless other unconventional ways of termination are used.
67     */
68    public static Block unlimited(Block block) {
69      return new RepeatBlock(Integer.MAX_VALUE, block);
70    }
71  
72    @Override
73    public Iterator<AbstractPiece> iterator() {
74      return Iterables.concat(
75          Collections.nCopies(repeatTimes.get(), block)).iterator();
76    }
77  
78    @Override
79    public void forAllPossiblePieces(Consumer<AbstractPiece> consumer) {
80      block.forAllPossiblePieces(consumer);
81    }
82  
83    @Override
84    public String toString() {
85      return "RepeatBlock(" + repeatTimes + " * " + block + ")";
86    }
87  }