/** * 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); }
while (rowIter.hasNext()) { final Row row = rowIter.next(); final LocalDate date = row.getLocalDate(0); final String dateStr = row.getString(0); if (date != null) { String[] bigRow; rows.put(date, bigRow); for (int i = 1; i < row.size(); i++) { bigRow[initialOffset.get(codeRequest) + (i - 1)] = row.getString(i); // (i-1 is becuase initialOffset index already includes final HeaderDefinition headerDefinition = HeaderDefinition.of(columnNames); for (final Entry<LocalDate, String[]> entry : rows.entrySet()) { final Row row = Row.of(headerDefinition, entry.getValue()); combinedRows.add(row);
/** * Create a Row. Items in the values array can be null although the array cannot. If the headerDefinition has a different number of * columns than the length of the values array then an IllegalArgumentException will be thrown. * * @param headerDefinition the row definition, not null * @param values the actual values in the row, not null * @return a row instance */ public static Row of(final HeaderDefinition headerDefinition, final String[] values) { ArgumentChecker.notNull(headerDefinition, "headerDefinition"); ArgumentChecker.notNull(values, "values"); return new Row(headerDefinition, values); }
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; } }
@Test public void testRow() { HeaderDefinition headerDefinition1 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition2 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition3 = HeaderDefinition.of("B", "A", "C", "D"); Row row1 = Row.of(headerDefinition1, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Row row2 = Row.of(headerDefinition1, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Row row3 = Row.of(headerDefinition2, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Row row4 = Row.of(headerDefinition3, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Assert.assertEquals(row1, row1); Assert.assertEquals(row1.hashCode(), row1.hashCode()); Assert.assertEquals(row2, row3); Assert.assertEquals(row2.hashCode(), row3.hashCode()); Assert.assertNotEquals(row3, row4); Assert.assertNotEquals(row3.hashCode(), row4.hashCode()); }
@Test public void runAccessorTests() { HeaderDefinition headerDefinition1 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition2 = HeaderDefinition.of("A", "B", "C", "D"); Row row1 = Row.of(headerDefinition1, new String[] {"Jim", "Miranda", "1.2", "2009-09-13" }); Row row3 = Row.of(headerDefinition2, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); TabularResult tr1 = TabularResult.of(headerDefinition1, Arrays.asList(row1, row3)); Assert.assertEquals(tr1.get(0).getString(0), "Jim"); Assert.assertEquals(tr1.get(0).getString(1), "Miranda"); Assert.assertEquals(tr1.get(0).getString(2), "1.2"); Assert.assertEquals(tr1.get(0).getDouble(2), 1.2d, 1e-14); Assert.assertEquals(tr1.get(0).getString(3), "2009-09-13"); Assert.assertEquals(tr1.get(0).getLocalDate(3), LocalDate.of(2009, 9, 13)); Assert.assertEquals(tr1.get(0).getString("A"), "Jim"); Assert.assertEquals(tr1.get(0).getString("B"), "Miranda"); Assert.assertEquals(tr1.get(0).getString("C"), "1.2"); Assert.assertEquals(tr1.get(0).getDouble("C"), 1.2d, 1e-14); Assert.assertEquals(tr1.get(0).getString("D"), "2009-09-13"); Assert.assertEquals(tr1.get(0).getLocalDate("D"), LocalDate.of(2009, 9, 13)); }
/** * 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; } }
/** * The main body of the code. */ private void run() { ClassicQuandlSession session = ClassicQuandlSession.create(); TabularResult tabularResult = session.getDataSet( DataSetRequest.Builder .of("SSE/VROS") // VERIANOS REAL ESTATE AG on Boerse Stuttgart .withColumn(3) // Last (looked up previously) .withStartDate(RECENTISH_DATE) .withFrequency(Frequency.MONTHLY) .build()); System.out.println("Header definition: " + tabularResult.getHeaderDefinition()); for (final Row row : tabularResult) { LocalDate date = row.getLocalDate("Date"); Double value = row.getDouble("Last"); System.out.println("Value on date " + date + " was " + value); } }
/** * 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); }
private static void row(final StringBuilder sb, final int[] maxWidths, final Row row) { int i = 0; for (int width : maxWidths) { String value = row.getString(i++); 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"); }
@Test public void runAccessorTests() { HeaderDefinition headerDefinition1 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition2 = HeaderDefinition.of("A", "B", "C", "D"); Row row1 = Row.of(headerDefinition1, new String[] {"Jim", "Miranda", "1.2", "2009-09-13" }); Row row3 = Row.of(headerDefinition2, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); TabularResult tr1 = TabularResult.of(headerDefinition1, Arrays.asList(row1, row3)); Assert.assertEquals(tr1.get(0).getString(0), "Jim"); Assert.assertEquals(tr1.get(0).getString(1), "Miranda"); Assert.assertEquals(tr1.get(0).getString(2), "1.2"); Assert.assertEquals(tr1.get(0).getDouble(2), 1.2d, 1e-14); Assert.assertEquals(tr1.get(0).getString(3), "2009-09-13"); Assert.assertEquals(tr1.get(0).getLocalDate(3), LocalDate.of(2009, 9, 13)); Assert.assertEquals(tr1.get(0).getString("A"), "Jim"); Assert.assertEquals(tr1.get(0).getString("B"), "Miranda"); Assert.assertEquals(tr1.get(0).getString("C"), "1.2"); Assert.assertEquals(tr1.get(0).getDouble("C"), 1.2d, 1e-14); Assert.assertEquals(tr1.get(0).getString("D"), "2009-09-13"); Assert.assertEquals(tr1.get(0).getLocalDate("D"), LocalDate.of(2009, 9, 13)); }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testRowBothNull() { Row.of(null, null); }
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); TabularResult tabularResultMulti = session.getDataSets( MultiDataSetRequest.Builder .of( QuandlCodeRequest.singleColumn("WIKI/AAPL", CLOSE_COLUMN), QuandlCodeRequest.allColumns("DOE/RWTC") ) .withStartDate(RECENTISH_DATE) .withFrequency(Frequency.MONTHLY) .build()); System.out.println("Header definition: " + tabularResultMulti.getHeaderDefinition()); Iterator<Row> iter = tabularResultMulti.iterator(); while (iter.hasNext()) { Row row = iter.next(); LocalDate date = row.getLocalDate("Date"); Double value = row.getDouble("DOE/RWTC - Value"); System.out.println("Value on date " + date + " was " + value); } }
@Test public void testRow() { HeaderDefinition headerDefinition1 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition2 = HeaderDefinition.of("A", "B", "C", "D"); HeaderDefinition headerDefinition3 = HeaderDefinition.of("B", "A", "C", "D"); Row row1 = Row.of(headerDefinition1, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Row row2 = Row.of(headerDefinition1, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Row row3 = Row.of(headerDefinition2, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Row row4 = Row.of(headerDefinition3, new String[] {"Jim", "Miranda", "Elaine", "Kostas" }); Assert.assertEquals(row1, row1); Assert.assertEquals(row1.hashCode(), row1.hashCode()); Assert.assertEquals(row2, row3); Assert.assertEquals(row2.hashCode(), row3.hashCode()); Assert.assertNotEquals(row3, row4); Assert.assertNotEquals(row3.hashCode(), row4.hashCode()); }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testRowBothNull() { Row.of(null, null); }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testRowHeaderNull() { Row.of(null, new String[] { "one", "two"}); }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testRowHeaderNull() { Row.of(null, new String[] { "one", "two"}); }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testRowValuesNull() { Row.of(HeaderDefinition.of("one", "two"), null); }
@Test(expectedExceptions = QuandlRuntimeException.class) public final void testRowValuesNull() { Row.of(HeaderDefinition.of("one", "two"), null); }
Row row = Row.of(headerDef, next); rows.add(row); next = reader.readNext();