Coverage Report - org.apache.giraph.comm.RequestRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestRegistry
76%
13/17
50%
4/8
3.667
RequestRegistry$Type
100%
6/6
N/A
3.667
 
 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.comm;
 20  
 
 21  
 import java.util.EnumMap;
 22  
 import java.util.Map;
 23  
 
 24  
 /**
 25  
  * Registry of the requests that are supported.
 26  
  */
 27  9
 public class RequestRegistry {
 28  
   /** Mapping of enum type to class type */
 29  9
   @SuppressWarnings("rawtypes")
 30  
   private final Map<Type, Class<? extends WritableRequest>> requestMap =
 31  
       new EnumMap<Type, Class<? extends WritableRequest>>(Type.class);
 32  
   /** If finalized, nothing can be added. */
 33  9
   private boolean shutdown = false;
 34  
 
 35  
   /**
 36  
    * Type of the request
 37  
    */
 38  10
   public enum Type {
 39  
     /** Sending vertices request */
 40  1
     SEND_VERTEX_REQUEST,
 41  
     /** Sending a partition of messages for next superstep */
 42  1
     SEND_PARTITION_MESSAGES_REQUEST,
 43  
     /**
 44  
      * Sending a partition of messages for current superstep
 45  
      * (used during partition exchange)
 46  
      */
 47  1
     SEND_PARTITION_CURRENT_MESSAGES_REQUEST,
 48  
     /** Send a partition of mutations */
 49  1
     SEND_PARTITION_MUTATIONS_REQUEST,
 50  
     /** Sending messages request */
 51  1
     SEND_MESSAGES_REQUEST,
 52  
   }
 53  
 
 54  
   /**
 55  
    * Register a writable request by type and class.
 56  
    *
 57  
    * @param writableRequest Request to be registered.
 58  
    */
 59  
   public void registerClass(WritableRequest<?, ?, ?, ?> writableRequest) {
 60  36
     if (shutdown) {
 61  0
       throw new IllegalStateException(
 62  
           "registerClass: Cannot call this after shutting down!");
 63  
     }
 64  36
     if (requestMap.put(writableRequest.getType(),
 65  
         writableRequest.getClass()) != null) {
 66  0
       throw new IllegalArgumentException("registerClass: Class " +
 67  
           writableRequest.getClass() + " already exists!");
 68  
     }
 69  36
   }
 70  
 
 71  
   /**
 72  
    * Get a class (must be finalized)
 73  
    *
 74  
    * @param type Type of the request to get
 75  
    * @return Class of the type
 76  
    */
 77  
   @SuppressWarnings("rawtypes")
 78  
   public Class<? extends WritableRequest> getClass(Type type) {
 79  3
     if (!shutdown) {
 80  0
       throw new IllegalStateException(
 81  
           "getClass: Illegal to get class before finalized");
 82  
     }
 83  
 
 84  3
     Class<? extends WritableRequest> writableRequestClass =
 85  
         requestMap.get(type);
 86  3
     if (writableRequestClass == null) {
 87  0
       throw new IllegalArgumentException(
 88  
           "getClass: Couldn't find type " + type);
 89  
     }
 90  
 
 91  3
     return writableRequestClass;
 92  
   }
 93  
 
 94  
   /**
 95  
    * No more requests can be registered.
 96  
    */
 97  
   public void shutdown() {
 98  9
     shutdown = true;
 99  9
   }
 100  
 }