This project has retired. For details please refer to its Attic page.
PieceCount 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  
19  package org.apache.giraph.block_app.framework.block;
20  
21  import com.google.common.base.Objects;
22  
23  /**
24   * Number of pieces
25   */
26  public class PieceCount {
27    private boolean known;
28    private int count;
29  
30    public PieceCount(int count) {
31      known = true;
32      this.count = count;
33    }
34  
35    private PieceCount() {
36      known = false;
37    }
38  
39    public static PieceCount createUnknownCount() {
40      return new PieceCount();
41    }
42  
43  
44    public PieceCount add(PieceCount other) {
45      if (!this.known || !other.known) {
46        known = false;
47      } else {
48        count += other.count;
49      }
50      return this;
51    }
52  
53    public PieceCount multiply(int value) {
54      count *= value;
55      return this;
56    }
57  
58    public int getCount() {
59      if (known) {
60        return count;
61      } else {
62        throw new IllegalStateException(
63            "Can't get superstep count when it's unknown");
64      }
65    }
66  
67    public boolean isKnown() {
68      return known;
69    }
70  
71    @Override
72    public boolean equals(Object obj) {
73      if (this == obj) {
74        return true;
75      }
76      if (obj instanceof PieceCount) {
77        PieceCount other = (PieceCount) obj;
78        if (known) {
79          return other.known && other.count == count;
80        } else {
81          return !other.known;
82        }
83      }
84      return false;
85    }
86  
87    @Override
88    public int hashCode() {
89      return Objects.hashCode(known, count);
90    }
91  }