Coverage Report - org.apache.johnzon.core.JsonObjectBuilderImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonObjectBuilderImpl
65%
23/35
80%
8/10
1,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.math.BigDecimal;
 23  
 import java.math.BigInteger;
 24  
 import java.util.Collections;
 25  
 import java.util.LinkedHashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.json.JsonArrayBuilder;
 29  
 import javax.json.JsonObject;
 30  
 import javax.json.JsonObjectBuilder;
 31  
 import javax.json.JsonValue;
 32  
 
 33  4580
 class JsonObjectBuilderImpl implements JsonObjectBuilder, Serializable {
 34  
     private Map<String, JsonValue> tmpMap;
 35  
 
 36  
     @Override
 37  
     public JsonObjectBuilder add(final String name, final JsonValue value) {
 38  34745
         putValue(name, value);
 39  34745
         return this;
 40  
     }
 41  
 
 42  
     @Override
 43  
     public JsonObjectBuilder add(final String name, final String value) {
 44  2
         putValue(name, new JsonStringImpl(value));
 45  2
         return this;
 46  
     }
 47  
 
 48  
     @Override
 49  
     public JsonObjectBuilder add(final String name, final BigInteger value) {
 50  0
         putValue(name, new JsonNumberImpl(new BigDecimal(value)));
 51  0
         return this;
 52  
     }
 53  
 
 54  
     @Override
 55  
     public JsonObjectBuilder add(final String name, final BigDecimal value) {
 56  0
         putValue(name, new JsonNumberImpl(value));
 57  0
         return this;
 58  
     }
 59  
 
 60  
     @Override
 61  
     public JsonObjectBuilder add(final String name, final int value) {
 62  0
         putValue(name, new JsonLongImpl(value));
 63  0
         return this;
 64  
     }
 65  
 
 66  
     @Override
 67  
     public JsonObjectBuilder add(final String name, final long value) {
 68  0
         putValue(name, new JsonLongImpl(value));
 69  0
         return this;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public JsonObjectBuilder add(final String name, final double value) {
 74  0
         putValue(name, new JsonDoubleImpl(value));
 75  0
         return this;
 76  
     }
 77  
 
 78  
     @Override
 79  
     public JsonObjectBuilder add(final String name, final boolean value) {
 80  6671
         putValue(name, value ? JsonValue.TRUE : JsonValue.FALSE);
 81  6671
         return this;
 82  
     }
 83  
 
 84  
     @Override
 85  
     public JsonObjectBuilder addNull(final String name) {
 86  3035
         putValue(name, JsonValue.NULL);
 87  3035
         return this;
 88  
     }
 89  
 
 90  
     @Override
 91  
     public JsonObjectBuilder add(final String name, final JsonObjectBuilder builder) {
 92  1011
         putValue(name, builder.build());
 93  1011
         return this;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public JsonObjectBuilder add(final String name, final JsonArrayBuilder builder) {
 98  2240
         putValue(name, builder.build());
 99  2240
         return this;
 100  
     }
 101  
     
 102  
     private void putValue(String name, JsonValue value){
 103  47704
         if(name == null || value == null) {
 104  0
             throw npe();
 105  
         }
 106  
         
 107  47704
         if(tmpMap==null){
 108  4546
             tmpMap=new LinkedHashMap<String, JsonValue>();
 109  
         }
 110  
         
 111  47704
         tmpMap.put(name, value);
 112  47704
     }
 113  
     
 114  
     private static NullPointerException npe() {
 115  0
         return new NullPointerException("name or value/builder must not be null");
 116  
     }
 117  
 
 118  
     @Override
 119  
     public JsonObject build() {
 120  
         
 121  4517
         if(tmpMap==null) {
 122  2
             return new JsonObjectImpl(Collections.EMPTY_MAP);
 123  
         } else {
 124  4515
             Map<String, JsonValue> dump = (Collections.unmodifiableMap(tmpMap));
 125  4515
             tmpMap=null;
 126  4515
             return new JsonObjectImpl(dump);
 127  
         }
 128  
         
 129  
         
 130  
     }
 131  
 }