/** * Get the optional long value associated with an index. The defaultValue is * returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default value. * @return The value. */ public long optLong(int index, long defaultValue) { try { return this.getLong(index); } catch (Exception e) { return defaultValue; } }
public static long[] jsonArrayToLongArray(JSONArray jsonArray) throws JSONException { long[] ar = new long[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { ar[i] = jsonArray.getLong(i); } return ar; }
JSONArray names = json.names(); JSONArray values = json.toJSONArray(names); for(int i=0; i<values.length(); i++){ if (names.getString(i).equals("description")){ setDescription(values.getString(i)); } else if (names.getString(i).equals("expiryDate")){ String dateString = values.getString(i); setExpiryDate(stringToDateHelper(dateString)); } else if (names.getString(i).equals("id")){ setId(values.getLong(i)); } else if (names.getString(i).equals("offerCode")){ setOfferCode(values.getString(i)); } else if (names.getString(i).equals("startDate")){ String dateString = values.getString(i); setStartDate(stringToDateHelper(dateString)); } else if (names.getString(i).equals("title")){ setTitle(values.getString(i)); } }
/** * The currently active dids. Make sure to copy before modifying. */ public LinkedList<Long> active() { try { JSONArray ja = mCol.getConf().getJSONArray("activeDecks"); LinkedList<Long> result = new LinkedList<>(); for (int i = 0; i < ja.length(); i++) { result.add(ja.getLong(i)); } return result; } catch (JSONException e) { throw new RuntimeException(e); } }
long[] array = new long[jsonArray.length()]; for (int i = 0; i < array.length; i++) { array[i] = jsonArray.getLong(i);
public void _updateStats(Card card, String type, long cnt) { String key = type + "Today"; long did = card.getDid(); List<JSONObject> list = mCol.getDecks().parents(did); list.add(mCol.getDecks().get(did)); for (JSONObject g : list) { try { JSONArray a = g.getJSONArray(key); // add a.put(1, a.getLong(1) + cnt); } catch (JSONException e) { throw new RuntimeException(e); } mCol.getDecks().save(g); } }
private void remove(JSONObject graves) { // pretend to be the server so we don't set usn = -1 boolean wasServer = mCol.getServer(); mCol.setServer(true); try { // notes first, so we don't end up with duplicate graves mCol._remNotes(Utils.jsonArrayToLongArray(graves.getJSONArray("notes"))); // then cards mCol.remCards(Utils.jsonArrayToLongArray(graves.getJSONArray("cards")), false); // and decks JSONArray decks = graves.getJSONArray("decks"); for (int i = 0; i < decks.length(); i++) { mCol.getDecks().rem(decks.getLong(i), false, false); } } catch (JSONException e) { throw new RuntimeException(e); } mCol.setServer(wasServer); }
private void assertArray(List<?> actualValues, JSONArray expectedList) throws Exception { assertThat(actualValues.size()).isEqualTo(expectedList.length()); for (int i = 0; i < actualValues.size(); i++) { Object actualValue = actualValues.get(i); if (actualValue == null) { assertThat(expectedList.isNull(i)).isTrue(); } else { if (actualValue instanceof Boolean) { assertThat((Boolean) actualValue).isEqualTo(expectedList.getBoolean(i)); } else if (actualValue instanceof String) { assertThat((String) actualValue).isEqualTo(expectedList.getString(i)); } else if (actualValue instanceof Long) { assertThat((Long) actualValue).isEqualTo(expectedList.getLong(i)); } else if (actualValue instanceof Double) { assertThat((Double) actualValue).isEqualTo(expectedList.getDouble(i)); } else if (actualValue instanceof ByteArray) { assertThat((ByteArray) actualValue) .isEqualTo(ByteArray.fromBase64(expectedList.getString(i))); } else if (actualValue instanceof Struct) { Struct actualStruct = (Struct) actualValue; JSONArray expectedFields = expectedList.getJSONArray(i); assertRow(actualStruct, expectedFields); } } } } }
private List<Long> toLongList(JSONArray array) { List<Long> longs = new ArrayList<>(); for (int i = 0; i < array.length(); i++) longs.add(array.getLong(i)); return longs; } }
private void assertRow(Struct actualRow, JSONArray expectedRow) throws Exception { assertThat(actualRow.getColumnCount()).isEqualTo(expectedRow.length()); for (int i = 0; i < expectedRow.length(); i++) { switch (actualRow.getColumnType(i).getCode()) { case BOOL: assertThat(actualRow.getBoolean(i)).isEqualTo(expectedRow.getBoolean(i)); break; case STRING: assertThat(actualRow.getString(i)).isEqualTo(expectedRow.getString(i)); break; case INT64: assertThat(actualRow.getLong(i)).isEqualTo(expectedRow.getLong(i)); break; case FLOAT64: assertThat(actualRow.getDouble(i)).isEqualTo(expectedRow.getDouble(i)); break; case BYTES: assertThat(actualRow.getBytes(i)) .isEqualTo(ByteArray.fromBase64(expectedRow.getString(i))); break; case ARRAY: Type elementType = actualRow.getColumnType(i).getArrayElementType(); assertArray(getRawList(actualRow, i, elementType), expectedRow.getJSONArray(i)); break; default: Assert.fail("Unexpected type code:" + actualRow.getColumnType(i).getCode()); } } }
private List<Role> toRolesList(GuildImpl guild, JSONArray array) { LinkedList<Role> roles = new LinkedList<>(); for(int i = 0; i < array.length(); i++) { final long id = array.getLong(i); Role r = guild.getRolesMap().get(id); if (r != null) { roles.add(r); } else { getJDA().getEventCache().cache(EventCache.Type.ROLE, id, responseNumber, allContent, this::handle); EventCache.LOG.debug("Got GuildMember update but one of the Roles for the Member is not yet cached."); return null; } } return roles; } }
for (int k = 0; k < rolesJson.length(); k++) final long roleId = rolesJson.getLong(k); Role r = guild.getRolesMap().get(roleId); if (r == null)
@Override public long asLong() { return array.getLong(index); }
mentionedRoles.add(roleMentionArr.getLong(i));
final long current = ringing.getLong(i); if (current == userId)
@Override protected LongWritable getId(JSONArray jsonVertex) throws JSONException, IOException { return new LongWritable(jsonVertex.getLong(0)); }
@Override public List<Long> getList(Field field, JSONObject options, String key) { JSONArray array = (options.has(key)) ? options.getJSONArray(key) : new JSONArray(); List<Long> list = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { list.add(array.getLong(i)); } return list; }
/** * Check the public and the private sequence * @param jsonArray */ private void checkPublicAndPrivateSequence(final JSONArray jsonArray) { final long nextPublicSequnceNumber = jsonArray.getLong(jsonArray.length() - 2); final long nextPrivateSequnceNumber = jsonArray.getLong(jsonArray.length() - 1); auditPublicSequence(nextPublicSequnceNumber); auditPrivateSequence(nextPrivateSequnceNumber); }