Coverage Report - org.apache.johnzon.core.AbstractJsonFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJsonFactory
90 %
27/30
83 %
15/18
4,5
 
 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,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.core;
 20  
 
 21  
 import java.io.Serializable;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.HashMap;
 25  
 import java.util.Locale;
 26  
 import java.util.Map;
 27  
 import java.util.logging.Logger;
 28  
 
 29  
 public abstract class AbstractJsonFactory implements Serializable{
 30  
 
 31  366
     protected final Logger logger = Logger.getLogger(this.getClass().getName());
 32  
     
 33  
     public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy";
 34  1
     public static final BufferStrategy DEFAULT_BUFFER_STRATEGY = BufferStrategy.QUEUE;
 35  
     
 36  366
     protected final Map<String, Object> internalConfig = new HashMap<String, Object>();
 37  
     
 38  366
     protected AbstractJsonFactory(final Map<String, ?> config, Collection<String> supportedConfigKeys, Collection<String> defaultSupportedConfigKeys) {
 39  366
         if(config != null) {
 40  
             
 41  362
             if(defaultSupportedConfigKeys != null) {
 42  45
                 supportedConfigKeys = new ArrayList<String>(supportedConfigKeys);
 43  45
                 supportedConfigKeys.addAll(defaultSupportedConfigKeys);
 44  
             }
 45  
             
 46  362
             for (String configKey : config.keySet()) {
 47  360
                 if(supportedConfigKeys.contains(configKey)) {
 48  360
                     internalConfig.put(configKey, config.get(configKey));
 49  
                 } else {
 50  0
                     logger.warning(configKey + " is not supported by " + getClass().getName());
 51  
                 }
 52  360
             }
 53  
         }
 54  366
     }
 55  
 
 56  
     protected BufferStrategy getBufferProvider() {
 57  633
         final Object name = internalConfig.get(BUFFER_STRATEGY);
 58  633
         if (name != null) {
 59  0
             return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
 60  
         }
 61  633
         return DEFAULT_BUFFER_STRATEGY;
 62  
     }
 63  
 
 64  
     protected int getInt(final String key, final int defaultValue) {
 65  634
         final Object intValue = internalConfig.get(key);
 66  634
         if (intValue == null) {
 67  321
             return defaultValue;
 68  313
         } else if (Number.class.isInstance(intValue)) {
 69  4
             return Number.class.cast(intValue).intValue();
 70  
         }
 71  309
         return Integer.parseInt(intValue.toString());
 72  
     }
 73  
 
 74  
     protected boolean getBool(final String key, final boolean defaultValue) {
 75  318
         final Object boolValue = internalConfig.get(key);
 76  318
         if (boolValue == null) {
 77  316
             return defaultValue;
 78  2
         } else if (Boolean.class.isInstance(boolValue)) {
 79  2
             return Boolean.class.cast(boolValue);
 80  
         }
 81  0
         return Boolean.parseBoolean(boolValue.toString());
 82  
     }
 83  
 
 84  
 }