/** * Throws an exception if the argument is not null. * * @param argument the object to check * @param name the name of the parameter */ public static void notNull(final Object argument, final String name) { if (argument == null) { s_logger.error("Argument {} was null", name); throw new QuandlRuntimeException("Value " + name + " was null"); } }
/** * Throws an exception if the array argument is not null or empty. * * @param <E> type of array * @param argument the object to check * @param name the name of the parameter */ public static <E> void notNullOrEmpty(final E[] argument, final String name) { if (argument == null) { s_logger.error("Argument {} was null", name); throw new QuandlRuntimeException("Value " + name + " was null"); } else if (argument.length == 0) { s_logger.error("Argument {} was empty array", name); throw new QuandlRuntimeException("Value " + name + " was empty array"); } }
/** * Get the number of documents per page in this result set. Throws a QuandlRuntimeException if Quandl response doesn't contain this field, * although it should. * * @return the number of documents per page in this result set */ public int getDocumentsPerPage() { try { final int perPage = _jsonObject.getJSONObject(META_OBJECT_FIELD).getInt("per_page"); return perPage; } catch (JSONException ex) { throw new QuandlRuntimeException("Could not find total_count field in results from Quandl", ex); } }
/** * Get the current page of this result set. You can specify the page number in the request. Throws a QuandlRuntimeException if Quandl * response doesn't contain this field, although it should. * * @return the page number of this result set */ public int getCurrentPage() { try { final int currentPage = _jsonObject.getJSONObject(META_OBJECT_FIELD).getInt("current_page"); return currentPage; } catch (JSONException ex) { throw new QuandlRuntimeException("Could not find total_count field in results from Quandl", ex); } }
/** * {@inheritDoc} */ public JSONObject process(final InputStream inputStream, final Request request) { JSONTokener tokeniser = new JSONTokener(new InputStreamReader(inputStream)); try { return new JSONObject(tokeniser); } catch (JSONException e) { throw new QuandlRuntimeException("Error parsing JSON", e); } } }
/** * Throws an exception if the string argument is not null or empty. * * @param argument the String to check * @param name the name of the parameter */ public static void notNullOrEmpty(final String argument, final String name) { if (argument == null) { s_logger.error("Argument {} was null", name); throw new QuandlRuntimeException("Value " + name + " was null"); } else if (argument.length() == 0) { s_logger.error("Argument {} was empty string", name); throw new QuandlRuntimeException("Value " + name + " was empty string"); } } }
/** * Get the total number of documents in this result set. Throws a QuandlRuntimeException if Quandl response doesn't contain this field, * although it should. * * @return the total number of documents in this result set */ public int getTotalDocuments() { try { final int totalDocs = _jsonObject.getJSONObject(META_OBJECT_FIELD).getInt("total_count"); return totalDocs; } catch (JSONException ex) { throw new QuandlRuntimeException("Could not find total_count field in results from Quandl", ex); } }
/** * Throws an exception if the collection argument is not null or empty. * * @param <E> type of array * @param argument the object to check * @param name the name of the parameter */ public static <E> void notNullOrEmpty(final Collection<E> argument, final String name) { if (argument == null) { s_logger.error("Argument {} was null", name); throw new QuandlRuntimeException("Value " + name + " was null"); } else if (argument.size() == 0) { s_logger.error("Argument {} was empty collection", name); throw new QuandlRuntimeException("Value " + name + " was empty collection"); } }
/** * Pretty print a JSONObject as an indented piece of JSON code. Throws a QuandlRuntimeException if it can't render the JSONObject to a * String. * * @param jsonObject the pre-parsed JSON object to pretty-print, not null * @return a String representation of the object, probably multi-line. */ public static String toPrettyPrintedString(final JSONObject jsonObject) { ArgumentChecker.notNull(jsonObject, "jsonObject"); try { return jsonObject.toString(JSON_INDENT) + LINE_SEPARATOR; } catch (JSONException ex) { s_logger.error("Problem converting JSONObject to String", ex); throw new QuandlRuntimeException("Problem converting JSONObject to String", ex); } }
/** * Get a LocalDate field (converted from a String internally). Throws a QuandlRuntimeException if it cannot find the field * * @param fieldName the name of the field * @return the field value, or null if the field is null */ public LocalDate getLocalDate(final String fieldName) { try { if (_jsonObject.isNull(fieldName)) { return null; } else { return LocalDate.parse(_jsonObject.getString(fieldName), DATE_FORMATTER); } } catch (JSONException ex) { throw new QuandlRuntimeException("Cannot find field", ex); } }
/** * Extract a HeaderDefinition from the meta data. Throws a QuandlRuntimeException if it cannot construct a valid HeaderDefinition * * @return the header definition, not null */ public HeaderDefinition getHeaderDefinition() { JSONArray jsonArray = null; try { jsonArray = _jsonObject.getJSONArray(COLUMN_NAMES_FIELD); List<String> columnNames = new ArrayList<String>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { columnNames.add(jsonArray.getString(i)); } return HeaderDefinition.of(columnNames); } catch (JSONException ex) { s_logger.error("Metadata had unexpected structure - could not extract column_names field. Was:\n{}", _jsonObject.toString()); throw new QuandlRuntimeException("Metadata had unexpected structure", ex); } }
/** * Get a Double field. This attempts to work around the stupid NaN is null behavior by explicitly testing for null. Throws a * QuandlRuntimeException if it cannot find the field * * @param fieldName the name of the field * @return the field value, or null if the field is null */ public Double getDouble(final String fieldName) { try { if (_jsonObject.isNull(fieldName)) { return null; } else { return _jsonObject.getDouble(fieldName); } } catch (JSONException ex) { throw new QuandlRuntimeException("Cannot find field", ex); } }
/** * Extract a list of MetaDataResult objects, each one representing a match. Throws a QuandlRuntimeException if it cannot construct a valid * HeaderDefinition * * @return the header definition, not null */ public List<MetaDataResult> getMetaDataResultList() { JSONArray jsonArray = null; try { jsonArray = _jsonObject.getJSONArray(DATASETS_ARRAY_FIELD); List<MetaDataResult> metaDataResults = new ArrayList<MetaDataResult>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { metaDataResults.add(MetaDataResult.of(jsonArray.getJSONObject(i))); } return metaDataResults; } catch (JSONException ex) { s_logger.error("Metadata had unexpected structure - could not extract datasets field, was:\n{}", _jsonObject.toString()); throw new QuandlRuntimeException("Metadata had unexpected structure", ex); } }
private Row(final HeaderDefinition headerDefinition, final String[] values) { if (headerDefinition.size() != values.length) { s_logger.error("Attempt to create a Row with a header definition containing {} columns and a values array containing {} values", headerDefinition.size(), values.length); throw new QuandlRuntimeException("headerDefinition and values array are of differing length"); } _headerDefinition = headerDefinition; _values = values; }
/** * Get a LocalDate field (converted from a String internally). Throws a QuandlRuntimeException if it cannot find the field * * @param fieldName the name of the field * @return the field value, or null if the field is null */ public OffsetDateTime getOffsetDateTime(final String fieldName) { try { if (_jsonObject.isNull(fieldName)) { return null; } else { return OffsetDateTime.parse(_jsonObject.getString(fieldName), DATE_TIME_FORMATTER); } } catch (JSONException ex) { throw new QuandlRuntimeException("Cannot find field", ex); } }
/** * Create a ResultChecker using the resource folder 'testresults' with it's filename counter set to zero. */ public ResultChecker() { File file; try { file = new File(RecordingRESTDataProvider.class.getResource("testresults/").toURI()); s_logger.info(file.getAbsolutePath()); _baseDir = file; } catch (URISyntaxException ex) { throw new QuandlRuntimeException("Problem parsing path of testdata directory", ex); } }
/** * Create a ResultSaver using the resource folder 'testresults' with it's filename counter set to zero. */ public ResultSaver() { File file; try { file = new File(RecordingRESTDataProvider.class.getResource("testresults/").toURI()); s_logger.info(file.getAbsolutePath()); _baseDir = file; } catch (URISyntaxException ex) { throw new QuandlRuntimeException("Problem parsing path of testdata directory", ex); } }
/** * Create a ResultChecker using the resource folder 'testresults' with it's filename counter set to zero. */ public ResultChecker() { File file; try { file = new File(RecordingRESTDataProvider.class.getResource("testresults/").toURI()); s_logger.info(file.getAbsolutePath()); _baseDir = file; } catch (URISyntaxException ex) { throw new QuandlRuntimeException("Problem parsing path of testdata directory", ex); } }
/** * Create a ResultSaver using the resource folder 'testresults' with it's filename counter set to zero. */ public ResultSaver() { File file; try { file = new File(RecordingRESTDataProvider.class.getResource("testresults/").toURI()); s_logger.info(file.getAbsolutePath()); _baseDir = file; } catch (URISyntaxException ex) { throw new QuandlRuntimeException("Problem parsing path of testdata directory", ex); } }
@Override public MetaDataResult getMetaData(final MultiMetaDataRequest request) { ArgumentChecker.notNull(request, "request"); Map<String, HeaderDefinition> multipleHeaderDefinition = getMultipleHeaderDefinition(request); try { JSONObject result = new JSONObject(); if (multipleHeaderDefinition.size() > 0) { result.append(JSON_COLUMN_NAMES_FIELD, DATE_COLUMN); result.append(JSON_COLUMNS_FIELD, DATE_COLUMN); } for (Map.Entry<String, HeaderDefinition> entry : multipleHeaderDefinition.entrySet()) { String quandlCode = entry.getKey(); HeaderDefinition headerDef = entry.getValue(); for (String columnName : headerDef.getColumnNames()) { if (!columnName.equals(DATE_COLUMN)) { // skip Date column for each data set. result.append(JSON_COLUMN_NAMES_FIELD, quandlCode + " - " + columnName); result.append(JSON_COLUMNS_FIELD, columnName); } } } result.put(JSON_DATA_FIELD, new JSONArray()); result.put(JSON_ERRORS_FIELD, Collections.emptyMap()); result.put(JSON_FREQUENCY_FIELD, (Object) null); result.put(JSON_FROM_DATE_FIELD, (Object) null); result.put(JSON_TO_DATE_FIELD, (Object) null); return MetaDataResult.of(result); } catch (JSONException ex) { throw new QuandlRuntimeException("Problem building JSON response", ex); } }