Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Element.java
Go to the documentation of this file.
1package org.dartlang.vm.service.element;
2
3import com.google.gson.JsonArray;
4import com.google.gson.JsonElement;
5import com.google.gson.JsonNull;
6import com.google.gson.JsonObject;
7
8import java.util.ArrayList;
9import java.util.List;
10
11/**
12 * Superclass for all observatory elements.
13 */
14public class Element {
15 protected final JsonObject json;
16
17 public Element(JsonObject json) {
18 this.json = json;
19 }
20
21 /**
22 * A utility method to handle null values and JsonNull values.
23 */
24 String getAsString(String name) {
25 final JsonElement element = json.get(name);
26 return (element == null || element == JsonNull.INSTANCE) ? null : element.getAsString();
27 }
28
29 /**
30 * A utility method to handle null values and JsonNull values.
31 */
32 int getAsInt(String name) {
33 final JsonElement element = json.get(name);
34 return (element == null || element == JsonNull.INSTANCE) ? -1 : element.getAsInt();
35 }
36
37 /**
38 * A utility method to handle null values and JsonNull values.
39 */
40 boolean getAsBoolean(String name) {
41 final JsonElement element = json.get(name);
42 return (element == null || element == JsonNull.INSTANCE) ? false : element.getAsBoolean();
43 }
44
45 /**
46 * Return the underlying JSON backing this element.
47 */
48 public JsonObject getJson() {
49 return json;
50 }
51
52 /**
53 * Return a specific JSON member as a list of integers.
54 */
55 List<Integer> getListInt(String memberName) {
56 return jsonArrayToListInt(json.getAsJsonArray(memberName));
57 }
58
59 /**
60 * Return a specific JSON member as a list of strings.
61 */
62 List<String> getListString(String memberName) {
63 return jsonArrayToListString(json.getAsJsonArray(memberName));
64 }
65
66 /**
67 * Return a specific JSON member as a list of list of integers.
68 */
69 List<List<Integer>> getListListInt(String memberName) {
70 JsonArray array = json.getAsJsonArray(memberName);
71 if (array == null) {
72 return null;
73 }
74 int size = array.size();
75 List<List<Integer>> result = new ArrayList<>();
76 for (int index = 0; index < size; ++index) {
77 result.add(jsonArrayToListInt(array.get(index).getAsJsonArray()));
78 }
79 return result;
80 }
81
82 private List<Integer> jsonArrayToListInt(JsonArray array) {
83 int size = array.size();
84 List<Integer> result = new ArrayList<>();
85 for (int index = 0; index < size; ++index) {
86 result.add(array.get(index).getAsInt());
87 }
88 return result;
89 }
90
91 private List<String> jsonArrayToListString(JsonArray array) {
92 int size = array.size();
93 List<String> result = new ArrayList<>();
94 for (int index = 0; index < size; ++index) {
95 final JsonElement elem = array.get(index);
96 result.add(elem == JsonNull.INSTANCE ? null : elem.getAsString());
97 }
98 return result;
99 }
100}
List< List< Integer > > getListListInt(String memberName)
Definition Element.java:69
List< Integer > getListInt(String memberName)
Definition Element.java:55
List< String > getListString(String memberName)
Definition Element.java:62
GAsyncResult * result
const char * name
Definition fuchsia.cc:50