This project has retired. For details please refer to its Attic page.
ComputationDoneName 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.zk;
20  
21  import javax.annotation.concurrent.Immutable;
22  
23  import static com.google.common.base.Preconditions.checkArgument;
24  import static com.google.common.base.Preconditions.checkNotNull;
25  
26  /**
27   * This name is used by each worker as a file to let the ZooKeeper
28   * servers know that they can shutdown.
29   */
30  @Immutable
31  public class ComputationDoneName {
32    /** Will end the name (easy to detect if this is a name match) */
33    private static final String COMPUTATION_DONE_SUFFIX =
34        ".COMPUTATION_DONE";
35    /** Unique worker id */
36    private final int workerId;
37    /** Name as a string */
38    private final String name;
39  
40    /**
41     * Constructor.
42     *
43     * @param workerId Unique worker id
44     */
45    public ComputationDoneName(int workerId) {
46      this.workerId = workerId;
47      this.name = Integer.toString(workerId) + COMPUTATION_DONE_SUFFIX;
48    }
49  
50    public int getWorkerId() {
51      return workerId;
52    }
53  
54    public String getName() {
55      return name;
56    }
57  
58    /**
59     * Create this object from a name (if possible).  If the name is not
60     * able to be parsed this will throw various runtime exceptions.
61     *
62     * @param name Name to parse
63     * @return ComputationDoneName object that represents this name
64     */
65    public static final ComputationDoneName fromName(String name) {
66      checkNotNull(name, "name is null");
67      checkArgument(name.endsWith(COMPUTATION_DONE_SUFFIX),
68          "Name %s is not a valid ComputationDoneName", name);
69  
70      return new ComputationDoneName(
71          Integer.parseInt(name.replace(COMPUTATION_DONE_SUFFIX, "")));
72    }
73  
74    /**
75     * Is this string a ComputationDoneName?
76     *
77     * @param name Name to check
78     * @return True if matches the format of a ComputationDoneName,
79     *         false otherwise
80     */
81    public static final boolean isName(String name) {
82      return name.endsWith(COMPUTATION_DONE_SUFFIX);
83    }
84  }