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.Supplier;
26  
27  import com.google.common.collect.AbstractIterator;
28  import com.google.common.collect.Iterables;
29  
30  /**
31   * Block that repeats another block until toQuit supplier returns true,
32   * but at most given number of times.
33   *
34   * If toQuit returns true on first run, block is not going
35   * to be executed at all.
36   */
37  @SuppressWarnings("rawtypes")
38  public final class RepeatUntilBlock implements Block {
39    private final Block block;
40    private final int repeatTimes;
41    private final Supplier<Boolean> toQuit;
42  
43    public RepeatUntilBlock(
44        int repeatTimes, Block block, Supplier<Boolean> toQuit) {
45      this.block = block;
46      this.repeatTimes = repeatTimes;
47      this.toQuit = toQuit;
48    }
49  
50    /**
51     * Repeat unlimited number of times, until toQuit supplier returns true.
52     */
53    public static Block unlimited(Block block, Supplier<Boolean> toQuit) {
54      return new RepeatUntilBlock(Integer.MAX_VALUE, block, toQuit);
55    }
56  
57    @Override
58    public Iterator<AbstractPiece> iterator() {
59      // nCopies uses constant memory, creating a looped list with single element
60      final Iterator<AbstractPiece> repeatIterator =
61          Iterables.concat(Collections.nCopies(repeatTimes, block)).iterator();
62      return new AbstractIterator<AbstractPiece>() {
63        @Override
64        protected AbstractPiece computeNext() {
65          if (Boolean.TRUE.equals(toQuit.get()) || !repeatIterator.hasNext()) {
66            return endOfData();
67          }
68  
69          return repeatIterator.next();
70        }
71      };
72    }
73  
74    @Override
75    public void forAllPossiblePieces(Consumer<AbstractPiece> consumer) {
76      block.forAllPossiblePieces(consumer);
77    }
78  
79    @Override
80    public String toString() {
81      return "RepeatUntilBlock(" + repeatTimes + " * " + block + ")";
82    }
83  }