This project has retired. For details please refer to its Attic page.
BlockApiHandle 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  package org.apache.giraph.block_app.framework.api;
19  
20  import static com.google.common.base.Preconditions.checkNotNull;
21  
22  /**
23   * Class that contains references to Block Api objects.
24   *
25   * One general use-case for this is for applications to indirectly get a handle
26   * on the Block Api objects and  implement operations that (i) depend on the
27   * Block Api interfaces, (ii) are not in the context of a Piece when defined,
28   * and (iii) are in the context of a Piece when executed.
29   *
30   * To do this, as opposed to defining an application as a
31   * {@link org.apache.giraph.block_app.framework.block.Block}, define
32   * your application as a
33   * {@link org.apache.giraph.block_app.framework.block.BlockWithApiHandle}.
34   *
35   * NOTE: Depending on the context in which this class is used, some of the
36   * handles may not be set. For instance, the {@link masterApi} is not set when
37   * this is in the context of a worker. Trying to get access to a handle when
38   * it is not set will result in a runtime exception. Instead, you should first
39   * use methods like the {@link #isMasterApiSet()} to check.
40   *
41   * The *Api fields are transient as we do not need/want to serialize them. They
42   * will be set at the appropriate time by the framework.
43   */
44  public class BlockApiHandle {
45    private transient BlockMasterApi masterApi;
46    private transient BlockWorkerReceiveApi workerReceiveApi;
47    private transient BlockWorkerSendApi workerSendApi;
48    private transient BlockWorkerContextReceiveApi workerContextReceiveApi;
49    private transient BlockWorkerContextSendApi workerContextSendApi;
50  
51    public void setMasterApi(BlockMasterApi api) {
52      this.masterApi = api;
53    }
54  
55    public void setWorkerReceiveApi(BlockWorkerReceiveApi api) {
56      this.workerReceiveApi = api;
57    }
58  
59    public void setWorkerSendApi(BlockWorkerSendApi api) {
60      this.workerSendApi = api;
61    }
62  
63    public void setWorkerContextReceiveApi(BlockWorkerContextReceiveApi api) {
64      this.workerContextReceiveApi = api;
65    }
66  
67    public void setWorkerContextSendApi(BlockWorkerContextSendApi api) {
68      this.workerContextSendApi = api;
69    }
70  
71    public boolean isMasterApiSet() {
72      return masterApi != null;
73    }
74  
75    public boolean isWorkerReceiveApiSet() {
76      return workerReceiveApi != null;
77    }
78  
79    public boolean isWorkerSendApiSet() {
80      return workerSendApi != null;
81    }
82  
83    public boolean isWorkerContextReceiveApiSet() {
84      return workerContextReceiveApi != null;
85    }
86  
87    public boolean isWorkerContextSendApiSet() {
88      return workerContextSendApi != null;
89    }
90  
91    public BlockMasterApi getMasterApi() {
92      checkNotNull(masterApi,
93        "BlockMasterApi not valid in this context.");
94      return masterApi;
95    }
96  
97    public BlockWorkerReceiveApi getWorkerReceiveApi() {
98      checkNotNull(workerReceiveApi,
99        "BlockWorkerReceiveApi not valid in this context.");
100     return workerReceiveApi;
101   }
102 
103   public BlockWorkerSendApi getWorkerSendApi() {
104     checkNotNull(workerSendApi,
105       "BlockWorkerSendApi not valid in this context.");
106     return workerSendApi;
107   }
108 
109   public BlockWorkerContextReceiveApi getWorkerContextReceiveApi() {
110     checkNotNull(workerContextReceiveApi,
111       "BlockWorkerContextReceiveApi not valid in this context");
112     return workerContextReceiveApi;
113   }
114 
115   public BlockWorkerContextSendApi getWorkerContextSendApi() {
116     checkNotNull(workerContextSendApi,
117       "BlockWorkerContextSendApi not valid in this context");
118     return workerContextSendApi;
119   }
120 }