Coverage Report - org.apache.giraph.conf.JsonStringConfOption
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonStringConfOption
0%
0/38
0%
0/8
2.111
 
 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.conf;
 19  
 
 20  
 import org.apache.hadoop.conf.Configuration;
 21  
 import org.apache.log4j.Logger;
 22  
 import org.codehaus.jackson.map.ObjectMapper;
 23  
 import org.codehaus.jackson.type.TypeReference;
 24  
 
 25  
 import java.io.IOException;
 26  
 
 27  
 /**
 28  
  * JSON String configuration option
 29  
  */
 30  
 public class JsonStringConfOption extends AbstractConfOption {
 31  
   /** Logger */
 32  0
   private static final Logger LOG =
 33  0
       Logger.getLogger(JsonStringConfOption.class);
 34  
 
 35  
   /**
 36  
    * Constructor
 37  
    *
 38  
    * @param key String key name
 39  
    * @param description String description of option
 40  
    */
 41  
   public JsonStringConfOption(String key, String description) {
 42  0
     super(key, description);
 43  0
   }
 44  
 
 45  
   /**
 46  
    * Set JSON value
 47  
    *
 48  
    * @param conf Configuration
 49  
    * @param value Json value
 50  
    */
 51  
   public void set(Configuration conf, Object value) {
 52  0
     ObjectMapper mapper = new ObjectMapper();
 53  
     String jsonStr;
 54  
     try {
 55  0
       jsonStr = mapper.writeValueAsString(value);
 56  0
       conf.set(getKey(), jsonStr);
 57  0
     } catch (IOException e) {
 58  0
       throw new IllegalStateException("Failed to set " + getKey() +
 59  
           " with json value from " + value);
 60  0
     }
 61  0
   }
 62  
 
 63  
   /**
 64  
    * Get raw JSON string
 65  
    *
 66  
    * @param conf Configuration
 67  
    * @return raw JSON string value
 68  
    */
 69  
   public String getRaw(Configuration conf) {
 70  0
     return conf.get(getKey());
 71  
   }
 72  
 
 73  
   /**
 74  
    * Get JSON value
 75  
    *
 76  
    * @param <T> JSON type
 77  
    * @param conf Configuration
 78  
    * @param klass Class to read into
 79  
    * @return JSON value
 80  
    */
 81  
   public <T> T get(Configuration conf, Class<T> klass) {
 82  0
     String jsonStr = getRaw(conf);
 83  0
     T value = null;
 84  0
     if (jsonStr != null) {
 85  0
       ObjectMapper mapper = new ObjectMapper();
 86  
       try {
 87  0
         value = mapper.readValue(jsonStr, klass);
 88  0
       } catch (IOException e) {
 89  0
         throw new IllegalStateException("Failed to read json from key " +
 90  0
             getKey() + " with class " + klass);
 91  0
       }
 92  
     }
 93  0
     return value;
 94  
   }
 95  
 
 96  
   /**
 97  
    * Get JSON value
 98  
    *
 99  
    * @param <T> JSON type
 100  
    * @param conf Configuration
 101  
    * @param typeReference TypeReference for JSON type
 102  
    * @return JSON value
 103  
    */
 104  
   public <T> T get(Configuration conf, TypeReference<T> typeReference) {
 105  0
     String jsonStr = getRaw(conf);
 106  0
     T value = null;
 107  0
     if (jsonStr != null) {
 108  0
       ObjectMapper mapper = new ObjectMapper();
 109  
       try {
 110  0
         value = mapper.<T>readValue(jsonStr, typeReference);
 111  0
       } catch (IOException e) {
 112  0
         throw new IllegalStateException("Failed to read json from key " +
 113  0
             getKey() + " with class " + typeReference);
 114  0
       }
 115  
     }
 116  0
     return value;
 117  
   }
 118  
 
 119  
   /**
 120  
    * Get JSON value, or default if not present
 121  
    *
 122  
    * @param <T> JSON type
 123  
    * @param klass Class to read into
 124  
    * @param conf Configuration
 125  
    * @param defaultValue Default value if not found
 126  
    * @return JSON value
 127  
    */
 128  
   public <T> T getWithDefault(Configuration conf, Class<T> klass,
 129  
       T defaultValue) {
 130  0
     if (contains(conf)) {
 131  0
       return get(conf, klass);
 132  
     } else {
 133  0
       return defaultValue;
 134  
     }
 135  
   }
 136  
 
 137  
   @Override
 138  
   public String getDefaultValueStr() {
 139  0
     return "null";
 140  
   }
 141  
 
 142  
   @Override
 143  
   public boolean isDefaultValue(Configuration conf) {
 144  0
     return !contains(conf);
 145  
   }
 146  
 
 147  
   @Override
 148  
   public ConfOptionType getType() {
 149  0
     return ConfOptionType.STRING;
 150  
   }
 151  
 }