Coverage Report - org.apache.johnzon.core.JsonArrayBuilderImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonArrayBuilderImpl
91 %
32/35
100 %
8/8
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.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.List;
 27  
 
 28  
 import javax.json.JsonArray;
 29  
 import javax.json.JsonArrayBuilder;
 30  
 import javax.json.JsonObjectBuilder;
 31  
 import javax.json.JsonValue;
 32  
 
 33  2295
 class JsonArrayBuilderImpl implements JsonArrayBuilder, Serializable {
 34  
     private List<JsonValue> tmpList;
 35  
 
 36  
     @Override
 37  
     public JsonArrayBuilder add(final JsonValue value) {
 38  4322
         addValue(value);
 39  4321
         return this;
 40  
     }
 41  
 
 42  
     @Override
 43  
     public JsonArrayBuilder add(final String value) {
 44  5
         addValue(new JsonStringImpl(value));
 45  4
         return this;
 46  
     }
 47  
 
 48  
     @Override
 49  
     public JsonArrayBuilder add(final BigDecimal value) {
 50  2
         addValue(new JsonNumberImpl(value));
 51  1
         return this;
 52  
     }
 53  
 
 54  
     @Override
 55  
     public JsonArrayBuilder add(final BigInteger value) {
 56  1
         addValue(new JsonNumberImpl(new BigDecimal(value)));
 57  0
         return this;
 58  
     }
 59  
 
 60  
     @Override
 61  
     public JsonArrayBuilder add(final int value) {
 62  12
         addValue(new JsonLongImpl(value));
 63  12
         return this;
 64  
     }
 65  
 
 66  
     @Override
 67  
     public JsonArrayBuilder add(final long value) {
 68  0
         addValue(new JsonLongImpl(value));
 69  0
         return this;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public JsonArrayBuilder add(final double value) {
 74  6
         addValue(new JsonDoubleImpl(value));
 75  3
         return this;
 76  
     }
 77  
 
 78  
     @Override
 79  
     public JsonArrayBuilder add(final boolean value) {
 80  1014
         addValue(value ? JsonValue.TRUE : JsonValue.FALSE);
 81  1014
         return this;
 82  
     }
 83  
 
 84  
     @Override
 85  
     public JsonArrayBuilder addNull() {
 86  6
         addValue(JsonValue.NULL);
 87  6
         return this;
 88  
     }
 89  
 
 90  
     @Override
 91  
     public JsonArrayBuilder add(final JsonObjectBuilder builder) {
 92  3450
         addValue(builder.build());
 93  3449
         return this;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public JsonArrayBuilder add(final JsonArrayBuilder builder) {
 98  5
         addValue(builder.build());
 99  4
         return this;
 100  
     }
 101  
     
 102  
     private void addValue(JsonValue value){
 103  8815
         if (value == null) {
 104  1
             throw npe();
 105  
         }
 106  
         
 107  8814
         if(tmpList==null){
 108  2275
             tmpList=new ArrayList<JsonValue>();
 109  
         }
 110  
         
 111  8814
         tmpList.add(value);
 112  8814
     }
 113  
 
 114  
     @Override
 115  
     public JsonArray build() {
 116  
         
 117  2271
         if(tmpList == null) {
 118  5
             return new JsonArrayImpl(Collections.EMPTY_LIST);
 119  
         } else {
 120  2266
             List<JsonValue> dump = (Collections.unmodifiableList(tmpList));
 121  2266
             tmpList=null;
 122  2266
             return new JsonArrayImpl(dump);
 123  
         }
 124  
         
 125  
     }
 126  
 
 127  
     private static NullPointerException npe() {
 128  1
         throw new NullPointerException("value/builder must not be null");
 129  
     }
 130  
 }