1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.block_app.framework.block;
1920import java.util.Collections;
21import java.util.Iterator;
2223import org.apache.giraph.block_app.framework.piece.AbstractPiece;
24import org.apache.giraph.function.Consumer;
25import org.apache.giraph.function.primitive.IntSupplier;
2627import com.google.common.collect.Iterables;
2829/**30 * Block that repeats another block given number of times.31 */32 @SuppressWarnings("rawtypes")
33publicfinalclassRepeatBlockimplementsBlock {
34privatefinalBlock block;
35privatefinalboolean constantRepeatTimes;
36privatefinalIntSupplier repeatTimes;
3738publicRepeatBlock(finalint repeatTimes, Block block) {
39this.block = block;
40this.constantRepeatTimes = true;
41this.repeatTimes = newIntSupplier() {
42 @Override
43publicint get() {
44return repeatTimes;
45 }
46 };
47 }
4849/**50 * Creates a repeat block, that before starting execution takes number of51 * iterations from the given supplier.52 *53 * This allows number of iterations to be dynamic, and depend on54 * execution that happens before.55 * Note - it doesn't allow for number of repetitions to change during the56 * loop itself - as it is supplier is called only when this block gets57 * its turn.58 */59publicRepeatBlock(IntSupplier repeatTimes, Block block) {
60this.block = block;
61this.constantRepeatTimes = false;
62this.repeatTimes = repeatTimes;
63 }
6465/**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 */71publicstaticBlock unlimited(Block block) {
72returnnewRepeatBlock(Integer.MAX_VALUE, block);
73 }
7475 @Override
76public Iterator<AbstractPiece> iterator() {
77return Iterables.concat(
78 Collections.nCopies(repeatTimes.get(), block)).iterator();
79 }
8081 @Override
82publicvoid forAllPossiblePieces(Consumer<AbstractPiece> consumer) {
83 block.forAllPossiblePieces(consumer);
84 }
8586 @Override
87publicPieceCount getPieceCount() {
88return constantRepeatTimes ?
89 block.getPieceCount().multiply(repeatTimes.get()) :
90 PieceCount.createUnknownCount();
91 }
9293 @Override
94public String toString() {
95return"RepeatBlock(" + repeatTimes + " * " + block + ")";
96 }
97 }