@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + _headerDefinition.hashCode(); result = prime * result + _rows.hashCode(); return result; }
/** * 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); } }
/** * Create a RowDefinition. In rare situations, duplicates can occur. These will be renamed so: * * <pre> * column, column, column, ... * </pre> * * becomes: * * <pre> * column, column.1, column.2, ... * </pre> * * for lookup and display purposes. A warning will be printed to the logger. * * @param columnNames a list of strings, each naming a column * @return the instance */ public static HeaderDefinition of(final List<String> columnNames) { ArgumentChecker.notNull(columnNames, "columnNames"); return new HeaderDefinition(columnNames); }
@Test public void testHeaderDefinition() { HeaderDefinition headerDefinition1 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition2 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition3 = HeaderDefinition.of("B", "A", "C", "D"); // test ordering counts HeaderDefinition headerDefinition4 = HeaderDefinition.of("A", "B", "C", "D", "E"); HeaderDefinition headerDefinitionEmpty = HeaderDefinition.of(); HeaderDefinition headerDefinitionEmpty2 = HeaderDefinition.of(); Assert.assertEquals(headerDefinition1, headerDefinition1); Assert.assertEquals(headerDefinition1, headerDefinition2); Assert.assertEquals(headerDefinition1.hashCode(), headerDefinition2.hashCode()); Assert.assertNotEquals(headerDefinition2, headerDefinition3); Assert.assertNotEquals(headerDefinition2.hashCode(), headerDefinition3.hashCode()); Assert.assertNotEquals(headerDefinition1, headerDefinition4); Assert.assertNotEquals(headerDefinition1.hashCode(), headerDefinition4.hashCode()); Assert.assertNotEquals(null, headerDefinition1); Assert.assertNotEquals(null, headerDefinitionEmpty); Assert.assertEquals(headerDefinitionEmpty, headerDefinitionEmpty2); Assert.assertEquals(headerDefinitionEmpty.hashCode(), headerDefinitionEmpty2.hashCode()); }
resultTableWidth += table.getHeaderDefinition().size() - 1; // exclude the date column. final List<String> names = table.getHeaderDefinition().getColumnNames(); final Iterator<String> iter = names.iterator(); if (!iter.hasNext()) { final HeaderDefinition headerDefinition = HeaderDefinition.of(columnNames); for (final Entry<LocalDate, String[]> entry : rows.entrySet()) { final Row row = Row.of(headerDefinition, entry.getValue());
private static int[] maximumWidths(final TabularResult result) { HeaderDefinition headerDefinition = result.getHeaderDefinition(); int[] maxWidths = new int[headerDefinition.size()]; int count = 0; for (String columnName : headerDefinition.getColumnNames()) { maxWidths[count++] = columnName.length(); } Iterator<Row> iterator = result.iterator(); while (iterator.hasNext()) { Row row = iterator.next(); for (int i = 0; i < row.size(); i++) { String columnValue = row.getString(i); if (columnValue != null) { maxWidths[i] = Math.max(maxWidths[i], columnValue.length()); } else { maxWidths[i] = Math.max(maxWidths[i], NULL_SIZE); } } } return maxWidths; } }
/** * Returns an entry from a given column name as a String (possibly null). Throws an IllegalArgumentException if the name is not defined in * the header definition. Empty is returned as the empty String rather than null. * * @param column the column name of the entry, not null * @return the entry, can be null */ public String getString(final String column) { return _values[_headerDefinition.columnIndex(column)]; }
/** * Get an iterator to return the names of all the columns in order. * * @return the iterator */ public Iterator<String> iterator() { return getColumnNames().iterator(); }
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; }
private static void header(final StringBuilder sb, final int[] maxWidths, final HeaderDefinition headerDefinition) { Iterator<String> iterator = headerDefinition.iterator(); for (int width : maxWidths) { String value = iterator.next(); sb.append("| "); if (value != null) { sb.append(value); sb.append(repeat(width - value.length(), ' ')); } else { sb.append(NULL); sb.append(repeat(width - NULL_SIZE, ' ')); } sb.append(" "); } sb.append("|"); sb.append("\n"); }
@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof TabularResult)) { return false; } TabularResult other = (TabularResult) obj; if (!_headerDefinition.equals(other._headerDefinition)) { return false; } if (!_rows.equals(other._rows)) { return false; } return true; }
@Test public void testHeaderDefinition() { HeaderDefinition headerDefinition1 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition2 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition3 = HeaderDefinition.of("B", "A", "C", "D"); // test ordering counts HeaderDefinition headerDefinition4 = HeaderDefinition.of("A", "B", "C", "D", "E"); HeaderDefinition headerDefinitionEmpty = HeaderDefinition.of(); HeaderDefinition headerDefinitionEmpty2 = HeaderDefinition.of(); Assert.assertEquals(headerDefinition1, headerDefinition1); Assert.assertEquals(headerDefinition1, headerDefinition2); Assert.assertEquals(headerDefinition1.hashCode(), headerDefinition2.hashCode()); Assert.assertNotEquals(headerDefinition2, headerDefinition3); Assert.assertNotEquals(headerDefinition2.hashCode(), headerDefinition3.hashCode()); Assert.assertNotEquals(headerDefinition1, headerDefinition4); Assert.assertNotEquals(headerDefinition1.hashCode(), headerDefinition4.hashCode()); Assert.assertNotEquals(null, headerDefinition1); Assert.assertNotEquals(null, headerDefinitionEmpty); Assert.assertEquals(headerDefinitionEmpty, headerDefinitionEmpty2); Assert.assertEquals(headerDefinitionEmpty.hashCode(), headerDefinitionEmpty2.hashCode()); }
/** * Returns an entry from a given column name as a LocalDate (possibly null). Throws an IllegalArgumentException if the name is not defined * in the header definition. Throws a DateTimeParseException if the underlying data is not a date in ISO local date format (YYYY-MM-DD). * * @param column the column name of the entry, not null * @return the entry, can be null if the value is null or empty */ public LocalDate getLocalDate(final String column) { int index = _headerDefinition.columnIndex(column); return getLocalDate(index); }
@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); } }
/** * Create row with new header. * * @param headerDefinition the header definition * @return a new Row with the new header definition */ public Row withPaddedHeader(final HeaderDefinition headerDefinition) { if (_headerDefinition != headerDefinition) { String[] values = new String[headerDefinition.size()]; System.arraycopy(_values, 0, values, 0, _values.length); return Row.of(headerDefinition, values); } else { return this; } }
sb.append(repeat(max - quandlCode.length(), ' ')); // indent sb.append(" => "); Iterator<String> iterator = headerDefinition.iterator(); while (iterator.hasNext()) { sb.append(iterator.next());
@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Row)) { return false; } Row other = (Row) obj; if (!_headerDefinition.equals(other._headerDefinition)) { return false; } if (!Arrays.equals(_values, other._values)) { return false; } return true; }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testHeaderDefinitionVarArgs() { HeaderDefinition.of((String[]) null); }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + _headerDefinition.hashCode(); result = prime * result + Arrays.hashCode(_values); return result; }
/** * Returns an entry from a given column name as a Double (possibly null). Throws an IllegalArgumentException if the name is not defined in * the header definition. Throws a NumberFormatException if the underlying data cannot be parsed as a double precision floating point * number. * * @param column the column name of the entry, not null * @return the entry, can be null if the value is null or empty */ public Double getDouble(final String column) { int index = _headerDefinition.columnIndex(column); return getDouble(index); }