1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
51
52
53
54
55
56
57
58
59 public RepeatBlock(IntSupplier repeatTimes, Block block) {
60 this.block = block;
61 this.constantRepeatTimes = false;
62 this.repeatTimes = repeatTimes;
63 }
64
65
66
67
68
69
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 }