From 48f4ba06c08fbdb98ccaae81d82b60eb7329c375 Mon Sep 17 00:00:00 2001 From: Andrew Raines Date: Fri, 1 Nov 2013 16:07:51 -0500 Subject: [PATCH] Add TimestampedTable subclass. --- .../java/org/elasticsearch/common/Table.java | 15 +++-- .../common/table/TimestampedTable.java | 62 +++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/elasticsearch/common/table/TimestampedTable.java diff --git a/src/main/java/org/elasticsearch/common/Table.java b/src/main/java/org/elasticsearch/common/Table.java index 2c5cd4faa13..7addfc09944 100644 --- a/src/main/java/org/elasticsearch/common/Table.java +++ b/src/main/java/org/elasticsearch/common/Table.java @@ -31,12 +31,12 @@ import java.util.Map; */ public class Table { - private List headers = new ArrayList(); - private List> rows = new ArrayList>(); + protected List headers = new ArrayList(); + protected List> rows = new ArrayList>(); - private List currentCells; + protected List 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 attr; - Cell(Object value, Map attr) { + public Cell(Object value) { + this.value = value; + this.attr = new HashMap(); + } + + public Cell(Object value, Map attr) { this.value = value; this.attr = attr; } diff --git a/src/main/java/org/elasticsearch/common/table/TimestampedTable.java b/src/main/java/org/elasticsearch/common/table/TimestampedTable.java new file mode 100644 index 00000000000..9830b53a191 --- /dev/null +++ b/src/main/java/org/elasticsearch/common/table/TimestampedTable.java @@ -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(); + 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(headers.size()); + currentCells.add(new Cell(now.getTime() / 1000)); + currentCells.add(new Cell(dfHms.format(now))); + return this; + } + +}