001package org.hl7.fhir.utilities.json.model;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import java.util.List;
006
007import org.hl7.fhir.utilities.json.JsonException;
008
009
010public class JsonArray extends JsonElement implements Iterable<JsonElement> {
011  private List<JsonElement> items = new ArrayList<>();
012  private List<Boolean> noCommas; // validator use
013  private List<Boolean> unQuoted; // validator use
014  private boolean extraComma; // json5 support
015  
016  public List<String> asStrings() {
017    List<String> list = new ArrayList<>();
018    for (JsonElement n : items) {
019      if (n instanceof JsonPrimitive) {
020        list.add(n.asJsonPrimitive().getValue());
021      }
022    }
023    return list;
024  }
025  
026  public List<JsonElement> getItems() {
027    return items;
028  }
029
030  public List<JsonObject> asJsonObjects() {
031    List<JsonObject> list = new ArrayList<>();
032    for (JsonElement n : items) {
033      if (n instanceof JsonObject) {
034        list.add((JsonObject) n);
035      }
036    }
037    return list;    
038  }
039  
040  public JsonElementType type() {
041    return JsonElementType.ARRAY;
042  }
043  
044  public JsonArray add(JsonElement node) throws JsonException {
045    check(node != null, "null object in JsonArray.add()");
046    items.add(node);
047    return this;
048  }
049  
050  public JsonArray add(int i, JsonElement node) throws JsonException {
051    check(node != null, "null object in JsonArray.add()");
052    items.add(i, node);
053    return this;
054  }
055  
056  public JsonArray add(String value) throws JsonException {
057    check(value != null, "null value in JsonArray.add()");
058    items.add(new JsonString(value));
059    return this;
060  }
061
062  public Integer size() {
063    return items.size();
064  }
065
066  public boolean isNoComma(int i) {
067    return noCommas == null ? false : noCommas.get(i);
068  }
069
070  public boolean isUnquoted(int i) {
071    return unQuoted == null ? false : unQuoted.get(i);
072  }
073  
074  // for the parser only
075  public void addForParser(JsonElement e, boolean itemNoComma, boolean unquoted) throws JsonException {
076    check(e != null, "null object in JsonArray.add()");
077    items.add(e);
078    if (noCommas == null) {
079      noCommas = new ArrayList<>();
080    }
081    noCommas.add(itemNoComma);    
082    if (unQuoted == null) {
083      unQuoted = new ArrayList<>();
084    }
085    unQuoted.add(unquoted);    
086  }
087
088
089  public JsonObject findByStringProp(String prop, String value) {
090    for (JsonObject obj : asJsonObjects()) {
091      if (obj.has(prop) && value.equals(obj.asString(prop))) 
092        return obj;
093    }
094    return null;
095  }
096  
097  public Iterator<JsonElement> iterator() {
098    return items.iterator();
099  }
100
101  public JsonElement get(int i) {
102    return items.get(i);
103  }
104
105  public JsonArray deepCopy() {
106    return (JsonArray) make().copy(this);
107  }
108
109  @Override
110  protected JsonElement copy(JsonElement other) {
111    JsonArray o = (JsonArray) other;
112    for (JsonElement p : o.getItems()) {
113      add(p.deepCopy());
114    }
115    return this;
116  }
117  
118  @Override
119  protected JsonElement make() {
120    return new JsonArray();
121  }
122  
123  @Override
124  public String toString() {
125    StringBuilder b = new StringBuilder();
126    b.append("[ ");
127    boolean first = true;
128    for (JsonElement p : items) {
129      if (first) first = false; else b.append(", ");
130      b.append(p.toString());
131    }
132    b.append(" ]");
133    return b.toString();
134  }
135
136  public boolean isExtraComma() {
137    return extraComma;
138  }
139
140  public void setExtraComma(boolean extraComma) {
141    this.extraComma = extraComma;
142  }
143
144  public void remove(JsonElement e) {
145    items.remove(e);
146    
147  }
148  
149}