This project has retired. For details please refer to its Attic page.
RepeatBlock 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.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 boolean constantRepeatTimes;
36    private final IntSupplier repeatTimes;
37  
38    public RepeatBlock(final int repeatTimes, Block block) {
39      this.block = block;
40      this.constantRepeatTimes = true;
41      this.repeatTimes = new IntSupplier() {
42        @Override
43        public int get() {
44          return repeatTimes;
45        }
46      };
47    }
48  
49    /**
50     * Creates a repeat block, that before starting execution takes number of
51     * iterations from the given supplier.
52     *
53     * This allows number of iterations to be dynamic, and depend on
54     * execution that happens before.
55     * Note - it doesn't allow for number of repetitions to change during the
56     * loop itself - as it is supplier is called only when this block gets
57     * its turn.
58     */
59    public RepeatBlock(IntSupplier repeatTimes, Block block) {
60      this.block = block;
61      this.constantRepeatTimes = false;
62      this.repeatTimes = repeatTimes;
63    }
64  
65    /**
66     * Create a repeat block that executes unlimited number of times.
67     *
68     * Should rarely be used, as it will cause application never to finish,
69     * unless other unconventional ways of termination are used.
70     */
71    public static Block unlimited(Block block) {
72      return new RepeatBlock(Integer.MAX_VALUE, block);
73    }
74  
75    @Override
76    public Iterator<AbstractPiece> iterator() {
77      return Iterables.concat(
78          Collections.nCopies(repeatTimes.get(), block)).iterator();
79    }
80  
81    @Override
82    public void forAllPossiblePieces(Consumer<AbstractPiece> consumer) {
83      block.forAllPossiblePieces(consumer);
84    }
85  
86    @Override
87    public PieceCount getPieceCount() {
88      return constantRepeatTimes ?
89          block.getPieceCount().multiply(repeatTimes.get()) :
90          PieceCount.createUnknownCount();
91    }
92  
93    @Override
94    public String toString() {
95      return "RepeatBlock(" + repeatTimes + " * " + block + ")";
96    }
97  }