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.Iterator;
2122import org.apache.giraph.block_app.framework.piece.AbstractPiece;
23import org.apache.giraph.function.Consumer;
24import org.apache.giraph.function.Supplier;
2526/**27 * Block that executes one of two branches based on a condition28 */29 @SuppressWarnings("rawtypes")
30publicfinalclassIfBlockimplementsBlock {
31privatefinalBlock thenBlock;
32privatefinalBlock elseBlock;
33privatefinal Supplier<Boolean> condition;
3435publicIfBlock(
36 Supplier<Boolean> condition, Block thenBlock, Block elseBlock) {
37this.condition = condition;
38this.thenBlock = thenBlock;
39this.elseBlock = elseBlock;
40 }
4142publicIfBlock(Supplier<Boolean> condition, Block thenBlock) {
43this.condition = condition;
44this.thenBlock = thenBlock;
45this.elseBlock = newEmptyBlock();
46 }
4748 @Override
49public Iterator<AbstractPiece> iterator() {
50if (Boolean.TRUE.equals(condition.get())) {
51return thenBlock.iterator();
52 } else {
53return elseBlock.iterator();
54 }
55 }
5657 @Override
58publicvoid forAllPossiblePieces(Consumer<AbstractPiece> consumer) {
59 thenBlock.forAllPossiblePieces(consumer);
60 elseBlock.forAllPossiblePieces(consumer);
61 }
6263 @Override
64publicPieceCount getPieceCount() {
65PieceCount thenCount = thenBlock.getPieceCount();
66PieceCount elseCount = elseBlock.getPieceCount();
67return thenCount.equals(elseCount) ?
68 thenCount : PieceCount.createUnknownCount();
69 }
7071 @Override
72public String toString() {
73if (elseBlock instanceof EmptyBlock) {
74return"IfBlock(" + thenBlock + ")";
75 }
76return"IfBlock(" + thenBlock + " , " + elseBlock + ")";
77 }
78 }