Add TimestampedTable subclass.

This commit is contained in:
Andrew Raines 2013-11-01 16:07:51 -05:00
parent 720edafa0d
commit 48f4ba06c0
2 changed files with 72 additions and 5 deletions

View File

@ -31,12 +31,12 @@ import java.util.Map;
*/
public class Table {
private List<Cell> headers = new ArrayList<Cell>();
private List<List<Cell>> rows = new ArrayList<List<Cell>>();
protected List<Cell> headers = new ArrayList<Cell>();
protected List<List<Cell>> rows = new ArrayList<List<Cell>>();
private List<Cell> currentCells;
protected List<Cell> currentCells;
private boolean inHeaders = false;
protected boolean inHeaders = false;
public Table startHeaders() {
inHeaders = true;
@ -117,7 +117,12 @@ public class Table {
public final Object value;
public final Map<String, String> attr;
Cell(Object value, Map<String, String> attr) {
public Cell(Object value) {
this.value = value;
this.attr = new HashMap<String, String>();
}
public Cell(Object value, Map<String, String> attr) {
this.value = value;
this.attr = attr;
}

View File

@ -0,0 +1,62 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.table;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Table;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class TimestampedTable extends Table {
private Date now;
public TimestampedTable () {
super();
this.now = new Date();
}
@Override
public Table startHeaders() {
inHeaders = true;
currentCells = new ArrayList<Cell>();
currentCells.add(new Cell("epoch"));
currentCells.add(new Cell("time"));
return this;
}
@Override
public Table startRow() {
SimpleDateFormat dfHms = new SimpleDateFormat("HH:mm:ss", Locale.ROOT);
if (headers.isEmpty()) {
throw new ElasticSearchIllegalArgumentException("no headers added...");
}
currentCells = new ArrayList<Cell>(headers.size());
currentCells.add(new Cell(now.getTime() / 1000));
currentCells.add(new Cell(dfHms.format(now)));
return this;
}
}