Coverage Report - org.apache.johnzon.core.JsonArrayImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonArrayImpl
37%
28/74
21%
9/42
2,4
 
 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.AbstractList;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import javax.json.JsonArray;
 27  
 import javax.json.JsonNumber;
 28  
 import javax.json.JsonObject;
 29  
 import javax.json.JsonString;
 30  
 import javax.json.JsonValue;
 31  
 
 32  6
 class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray, Serializable {
 33  2271
     private Integer hashCode = null;
 34  
     private final List<JsonValue> unmodifieableBackingList;
 35  2271
     private int size = -1;
 36  
 
 37  
     JsonArrayImpl(final List<JsonValue> backingList) {
 38  2271
         super();
 39  2271
         this.unmodifieableBackingList = backingList;
 40  2271
     }
 41  
 
 42  
     private <T> T value(final int idx, final Class<T> type) {
 43  23
         if (idx > unmodifieableBackingList.size()) {
 44  0
             throw new IndexOutOfBoundsException(idx + "/" + unmodifieableBackingList.size());
 45  
         }
 46  23
         return type.cast(unmodifieableBackingList.get(idx));
 47  
     }
 48  
 
 49  
     @Override
 50  
     public JsonObject getJsonObject(final int index) {
 51  0
         return value(index, JsonObject.class);
 52  
     }
 53  
 
 54  
     @Override
 55  
     public JsonArray getJsonArray(final int index) {
 56  0
         return value(index, JsonArray.class);
 57  
     }
 58  
 
 59  
     @Override
 60  
     public JsonNumber getJsonNumber(final int index) {
 61  5
         return value(index, JsonNumber.class);
 62  
     }
 63  
 
 64  
     @Override
 65  
     public JsonString getJsonString(final int index) {
 66  2
         return value(index, JsonString.class);
 67  
     }
 68  
 
 69  
     @Override
 70  
     public <T extends JsonValue> List<T> getValuesAs(final Class<T> clazz) {
 71  0
         return (List<T>) unmodifieableBackingList;
 72  
     }
 73  
 
 74  
     @Override
 75  
     public String getString(final int index) {
 76  6
         return value(index, JsonString.class).getString();
 77  
     }
 78  
 
 79  
     @Override
 80  
     public String getString(final int index, final String defaultValue) {
 81  0
         JsonValue val = null;
 82  0
         int s = size;
 83  
 
 84  0
         if (s == -1) {
 85  0
             s = unmodifieableBackingList.size();
 86  0
             size = s;
 87  
         }
 88  
 
 89  0
         if (index > s - 1 || !((val = get(index)) instanceof JsonString)) {
 90  0
             return defaultValue;
 91  
         } else {
 92  0
             return JsonString.class.cast(val).getString();
 93  
         }
 94  
     }
 95  
 
 96  
     @Override
 97  
     public int getInt(final int index) {
 98  10
         return value(index, JsonNumber.class).intValue();
 99  
     }
 100  
 
 101  
     @Override
 102  
     public int getInt(final int index, final int defaultValue) {
 103  0
         JsonValue val = null;
 104  0
         int s = size;
 105  
 
 106  0
         if (s == -1) {
 107  0
             s = unmodifieableBackingList.size();
 108  0
             size = s;
 109  
         }
 110  
 
 111  0
         if (index > s - 1 || !((val = get(index)) instanceof JsonNumber)) {
 112  0
             return defaultValue;
 113  
         } else {
 114  0
             return JsonNumber.class.cast(val).intValue();
 115  
         }
 116  
     }
 117  
 
 118  
     @Override
 119  
     public boolean getBoolean(final int index) {
 120  0
         final JsonValue val = value(index, JsonValue.class);
 121  
 
 122  0
         if (val == JsonValue.TRUE) {
 123  0
             return true;
 124  0
         } else if (val == JsonValue.FALSE) {
 125  0
             return false;
 126  
         } else {
 127  0
             throw new ClassCastException();
 128  
         }
 129  
 
 130  
     }
 131  
 
 132  
     @Override
 133  
     public boolean getBoolean(final int index, final boolean defaultValue) {
 134  
 
 135  0
         int s = size;
 136  
 
 137  0
         if (s == -1) {
 138  0
             s = unmodifieableBackingList.size();
 139  0
             size = s;
 140  
         }
 141  
 
 142  0
         if (index > s - 1) {
 143  0
             return defaultValue;
 144  
         }
 145  
 
 146  0
         final JsonValue val = get(index);
 147  
 
 148  0
         if (val == JsonValue.TRUE) {
 149  0
             return true;
 150  0
         } else if (val == JsonValue.FALSE) {
 151  0
             return false;
 152  
         } else {
 153  0
             return defaultValue;
 154  
         }
 155  
 
 156  
     }
 157  
 
 158  
     @Override
 159  
     public boolean isNull(final int index) {
 160  0
         return value(index, JsonValue.class) == JsonValue.NULL;
 161  
     }
 162  
 
 163  
     @Override
 164  
     public ValueType getValueType() {
 165  1
         return ValueType.ARRAY;
 166  
     }
 167  
 
 168  
     @Override
 169  
     public String toString() {
 170  17
         final StringBuilder builder = new StringBuilder("[");
 171  17
         final Iterator<JsonValue> it = unmodifieableBackingList.iterator();
 172  17
         boolean hasNext = it.hasNext();
 173  52
         while (hasNext) {
 174  35
             final JsonValue jsonValue = it.next();
 175  35
             if (JsonString.class.isInstance(jsonValue)) {
 176  8
                 builder.append(jsonValue.toString());
 177  
             } else {
 178  27
                 builder.append(jsonValue != JsonValue.NULL ? jsonValue.toString() : JsonChars.NULL);
 179  
             }
 180  35
             hasNext = it.hasNext();
 181  35
             if (hasNext) {
 182  21
                 builder.append(",");
 183  
             }
 184  35
         }
 185  17
         return builder.append(']').toString();
 186  
     }
 187  
 
 188  
     @Override
 189  
     public boolean equals(final Object obj) {
 190  0
         return JsonArrayImpl.class.isInstance(obj)
 191  0
                 && unmodifieableBackingList.equals(JsonArrayImpl.class.cast(obj).unmodifieableBackingList);
 192  
     }
 193  
 
 194  
     @Override
 195  
     public int hashCode() {
 196  0
         Integer h = hashCode;
 197  0
         if (h == null) {
 198  0
             h = unmodifieableBackingList.hashCode();
 199  0
             hashCode = h;
 200  
         }
 201  0
         return h;
 202  
     }
 203  
 
 204  
     @Override
 205  
     public JsonValue get(final int index) {
 206  6
         return unmodifieableBackingList.get(index);
 207  
     }
 208  
 
 209  
     @Override
 210  
     public int size() {
 211  17
         return unmodifieableBackingList.size();
 212  
     }
 213  
 }