AI2 Component  (Version nb184)
YailList.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2019 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
10 import gnu.lists.LList;
11 import gnu.lists.Pair;
12 import gnu.math.IntNum;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Set;
17 import org.json.JSONException;
18 
25 @SuppressWarnings("rawtypes")
26 public class YailList extends Pair implements YailObject {
27 
28  // Component writers take note!
29  // If you want to pass back a list to the blocks language, the
30  // straightforward way to do this is simply to pass
31  // back an ArrayList. If you construct a YailList to return
32  // to codeblocks, you must guarantee that the elements of the list
33  // are "sanitized". That is, you must pass back a tree whose
34  // subtrees are themselves YailLists, and whose leaves are all
35  // legitimate Yail data types. See the definition of sanitization
36  // in runtime.scm.
37 
41  public YailList() {
42  super(YailConstants.YAIL_HEADER, LList.Empty);
43  }
44 
45  private YailList(Object cdrval) {
46  super(YailConstants.YAIL_HEADER, cdrval);
47  }
48 
52  public static YailList makeEmptyList() {
53  return new YailList();
54  }
55 
59  public static YailList makeList(Object[] objects) {
60  LList newCdr = Pair.makeList(objects, 0);
61  return new YailList(newCdr);
62  }
63 
67  public static YailList makeList(List vals) {
68  LList newCdr = Pair.makeList(vals);
69  return new YailList(newCdr);
70  }
71 
75  public static YailList makeList(Collection vals) {
76  List valsList = new ArrayList(vals);
77 
78  LList newCdr = Pair.makeList(valsList);
79  return new YailList(newCdr);
80  }
81 
85  public static YailList makeList(Set vals) {
86  // LList newCdr = Pair.makeList(vals.toArray(new Object[vals.size()]), 0);
87  List valsList = new ArrayList(vals);
88 
89  LList newCdr = Pair.makeList(valsList);
90  return new YailList(newCdr);
91  }
92 
96  @Override
97  public Object[] toArray() {
98  if (cdr instanceof Pair) {
99  return ((Pair) cdr).toArray();
100  } else if (cdr instanceof LList) {
101  return ((LList) cdr).toArray();
102  } else {
103  throw new YailRuntimeError("YailList cannot be represented as an array", "YailList Error.");
104  }
105  }
106 
114  public String[] toStringArray() {
115  int size = this.size();
116  String[] objects = new String[size];
117  for (int i = 1; i <= size; i++) {
118  objects[i - 1] = YailListElementToString(get(i));
119  }
120  return objects;
121  }
122 
131  public static String YailListElementToString(Object element) {
132  if (element instanceof IntNum) {
133  return ((IntNum) element).toString(10);
134  } else if (element instanceof Long) {
135  return Long.toString((Long) element);
136  } else if (Number.class.isInstance(element)) {
137  return YailNumberToString.format(((Number) element).doubleValue());
138  } else {
139  return String.valueOf(element);
140  }
141  }
142 
148  public String toJSONString() {
149  try {
150  StringBuilder json = new StringBuilder();
151  String separator = "";
152  json.append('[');
153  int size = this.size();
154  for (int i = 1; i <= size; i++) {
155  Object value = get(i);
156  json.append(separator).append(JsonUtil.getJsonRepresentation(value));
157  separator = ",";
158  }
159  json.append(']');
160 
161  return json.toString();
162 
163  } catch (JSONException e) {
164  throw new YailRuntimeError("List failed to convert to JSON.", "JSON Creation Error.");
165  }
166  }
167 
171  @Override
172  public int size() {
173  return super.size() - 1;
174  }
175 
179  @Override
180  public String toString() {
181  if (cdr instanceof Pair) {
182  return ((Pair) cdr).toString();
183  } else if (cdr instanceof LList) {
184  return ((LList) cdr).toString();
185  } else {
186  throw new RuntimeException("YailList cannot be represented as a String");
187  }
188  }
189 
193  public String getString(int index) {
194  return get(index + 1).toString();
195  }
196 
200  public Object getObject(int index) {
201  return get(index + 1);
202  }
203 }
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.runtime.util.YailNumberToString.format
static String format(double number)
Definition: YailNumberToString.java:58
com.google.appinventor.components.runtime.util.YailList.YailListElementToString
static String YailListElementToString(Object element)
Definition: YailList.java:131
com.google.appinventor.components
com.google.appinventor.components.runtime.util.YailList.toArray
Object[] toArray()
Definition: YailList.java:97
com.google.appinventor.components.runtime.util.YailConstants
Definition: YailConstants.java:15
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(Object[] objects)
Definition: YailList.java:59
com.google.appinventor.components.runtime.util.JsonUtil
Definition: JsonUtil.java:42
com.google.appinventor.components.runtime.util.YailList.getString
String getString(int index)
Definition: YailList.java:193
com.google.appinventor.components.runtime.util.YailList.toStringArray
String[] toStringArray()
Definition: YailList.java:114
com.google.appinventor.components.runtime.errors.YailRuntimeError
Definition: YailRuntimeError.java:14
com.google.appinventor.components.runtime.util.YailConstants.YAIL_HEADER
static final SimpleSymbol YAIL_HEADER
Definition: YailConstants.java:18
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.YailList.toJSONString
String toJSONString()
Definition: YailList.java:148
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(Set vals)
Definition: YailList.java:85
com.google.appinventor.components.runtime.util.YailObject
Definition: YailObject.java:19
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(List vals)
Definition: YailList.java:67
com.google
com
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(Collection vals)
Definition: YailList.java:75
com.google.appinventor.components.runtime.util.YailList.toString
String toString()
Definition: YailList.java:180
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.util.YailList.makeEmptyList
static YailList makeEmptyList()
Definition: YailList.java:52
com.google.appinventor.components.runtime.util.JsonUtil.getJsonRepresentation
static String getJsonRepresentation(Object value)
Definition: JsonUtil.java:246
com.google.appinventor.components.runtime.util.YailList.getObject
Object getObject(int index)
Definition: YailList.java:200
com.google.appinventor.components.runtime.util.YailNumberToString
Definition: YailNumberToString.java:21
com.google.appinventor.components.runtime.util.YailList.YailList
YailList()
Definition: YailList.java:41
com.google.appinventor.components.runtime.util.YailList.size
int size()
Definition: YailList.java:172
com.google.appinventor