Coverage Report - org.apache.johnzon.core.JsonGeneratorFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonGeneratorFactoryImpl
70 %
14/20
50 %
4/8
2,6
 
 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 static java.util.Arrays.asList;
 22  
 
 23  
 import java.io.OutputStream;
 24  
 import java.io.Writer;
 25  
 import java.nio.charset.Charset;
 26  
 import java.util.Collection;
 27  
 import java.util.Collections;
 28  
 import java.util.Map;
 29  
 import java.util.concurrent.ConcurrentMap;
 30  
 
 31  
 import javax.json.stream.JsonGenerator;
 32  
 import javax.json.stream.JsonGeneratorFactory;
 33  
 
 34  
 public class JsonGeneratorFactoryImpl extends AbstractJsonFactory implements JsonGeneratorFactory {    
 35  
     public static final String GENERATOR_BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer-generator";
 36  1
     public static final int DEFAULT_GENERATOR_BUFFER_LENGTH =  Integer.getInteger(GENERATOR_BUFFER_LENGTH, 64 * 1024); //64k
 37  
    
 38  1
     static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
 39  
         JsonGenerator.PRETTY_PRINTING, GENERATOR_BUFFER_LENGTH, BUFFER_STRATEGY
 40  
     );
 41  
     //key caching currently disabled
 42  3
     private final ConcurrentMap<String, String> cache = null;//new ConcurrentHashMap<String, String>();
 43  
     private final boolean pretty;
 44  
     private final BufferStrategy.BufferProvider<char[]> bufferProvider;
 45  
 
 46  
     public JsonGeneratorFactoryImpl(final Map<String, ?> config) {
 47  
         
 48  3
           super(config, SUPPORTED_CONFIG_KEYS, null); 
 49  
           
 50  3
           this.pretty = getBool(JsonGenerator.PRETTY_PRINTING, false);
 51  
           
 52  3
           final int bufferSize = getInt(GENERATOR_BUFFER_LENGTH, DEFAULT_GENERATOR_BUFFER_LENGTH);
 53  3
           if (bufferSize <= 0) {
 54  0
               throw new IllegalArgumentException("buffer length must be greater than zero");
 55  
           }
 56  
 
 57  3
           this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
 58  3
     }
 59  
 
 60  
     @Override
 61  
     public JsonGenerator createGenerator(final Writer writer) {
 62  1
         if (pretty) {
 63  0
             return new JsonPrettyGeneratorImpl(writer, bufferProvider, cache);
 64  
         }
 65  1
         return new JsonGeneratorImpl(writer, bufferProvider, cache);
 66  
     }
 67  
 
 68  
     @Override
 69  
     public JsonGenerator createGenerator(final OutputStream out) {
 70  22
         if (pretty) {
 71  1
             return new JsonPrettyGeneratorImpl(out, bufferProvider, cache);
 72  
         }
 73  21
         return new JsonGeneratorImpl(out, bufferProvider, cache);
 74  
     }
 75  
 
 76  
     @Override
 77  
     public JsonGenerator createGenerator(final OutputStream out, final Charset charset) {
 78  0
         if (pretty) {
 79  0
             return new JsonPrettyGeneratorImpl(out,charset, bufferProvider, cache);
 80  
         }
 81  0
         return new JsonGeneratorImpl(out,charset, bufferProvider, cache);
 82  
     }
 83  
 
 84  
     @Override
 85  
     public Map<String, ?> getConfigInUse() {
 86  0
         return Collections.unmodifiableMap(internalConfig);
 87  
     }
 88  
 }