TimeService extends AbstractService { public void putAttributeValues(String column) { Date latest = null; Date earliest = null; Date secondLatest = null; Date secondEarliest = null; for (Attribute attr : dataset.getAttributesByColumn(column)) { if (attr.isAlive()) { Date value = (Date) attr.getValue(); if (value == null) { continue; } if (latest == null || earliest == null || secondLatest == null || secondEarliest == null) { latest = value; earliest = value; secondLatest = value; secondEarliest = value; } else { if (value.after(latest)) { secondLatest = latest; latest = value; if (secondLatest.equals(earliest)) { secondEarliest = latest; } } else if (value.before(earliest)) { secondEarliest = earliest; earliest = value; if (secondEarliest.equals(latest)) { secondLatest = earliest; } } if (value.before(latest) && value.after(secondLatest)) { secondLatest = value; } if (value.after(earliest) && value.before(secondEarliest)) { secondEarliest = value; } } } } latestValueMap.put(column, latest); earliestValueMap.put(column, earliest); secondLatestValueMap.put(column, secondLatest); secondEarliestValueMap.put(column, secondEarliest); } TimeService(DataSet dataset); void putAttributeValues(String column); boolean isLatestValue(Object var, String column); boolean isSecondEarliestValue(Object var, String column); boolean isSecondLatestValue(Object var, String column); boolean isEarliestValue(Object var, String column); @Override void init();  }
@Test public void testPutAttributeValues() { List<Column> columnList = generateColumnList(); DataSet dataSet = new DataSet(columnList); dataSet.initData(generateInputData()); TimeService ts = new TimeService(dataSet); ts.putAttributeValues("date1"); ts.putAttributeValues("date2"); ts.putAttributeValues("date3"); ts.putAttributeValues("date4"); Assert.assertEquals("longestValueMap size should be 4", 4, ts.earliestValueMap.size()); Assert.assertEquals("secondLongestValueMap size should be 4", 4, ts.latestValueMap.size()); Assert.assertEquals("secondShortestValueMap size should be 4", 4, ts.secondEarliestValueMap.size()); Assert.assertEquals("shortestValueMap size should be 4", 4, ts.secondLatestValueMap.size()); Assert.assertEquals("The result of firstName column should be 2000-06-06", "2000-06-06", SampleData.dateToString(ts.latestValueMap.get("date1"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-01-01", "2000-01-01", SampleData.dateToString(ts.latestValueMap.get("date2"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-06-06", "2000-06-06", SampleData.dateToString(ts.latestValueMap.get("date3"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-04-04", "2000-04-04", SampleData.dateToString(ts.latestValueMap.get("date4"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-05-05", "2000-05-05", SampleData.dateToString(ts.secondLatestValueMap.get("date1"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-01-01", "2000-01-01", SampleData.dateToString(ts.secondLatestValueMap.get("date2"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-05-05", "2000-05-05", SampleData.dateToString(ts.secondLatestValueMap.get("date3"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-03-03", "2000-03-03", SampleData.dateToString(ts.secondLatestValueMap.get("date4"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-01-01", "2000-01-01", SampleData.dateToString(ts.earliestValueMap.get("date1"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-01-01", "2000-01-01", SampleData.dateToString(ts.earliestValueMap.get("date2"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-01-01", "2000-01-01", SampleData.dateToString(ts.earliestValueMap.get("date3"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-03-03", "2000-03-03", SampleData.dateToString(ts.earliestValueMap.get("date4"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-02-02", "2000-02-02", SampleData.dateToString(ts.secondEarliestValueMap.get("date1"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-01-01", "2000-01-01", SampleData.dateToString(ts.secondEarliestValueMap.get("date2"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-02-02", "2000-02-02", SampleData.dateToString(ts.secondEarliestValueMap.get("date3"), "yyyy-MM-dd")); Assert.assertEquals("The result of firstName column should be 2000-04-04", "2000-04-04", SampleData.dateToString(ts.secondEarliestValueMap.get("date4"), "yyyy-MM-dd")); ts.init(); }