From 18057d43696d661be05c1a161260d844e3dc0d1c Mon Sep 17 00:00:00 2001 From: Ashvin Agrawal Date: Thu, 8 Feb 2018 14:58:10 -0800 Subject: [PATCH] Add builder methods for Array Types --- .../dhalion/core/MeasurementsArray.java | 51 +++++++++++-------- .../microsoft/dhalion/core/SymptomsArray.java | 28 ++++++++-- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/microsoft/dhalion/core/MeasurementsArray.java b/src/main/java/com/microsoft/dhalion/core/MeasurementsArray.java index 19a5c5c..b26c723 100644 --- a/src/main/java/com/microsoft/dhalion/core/MeasurementsArray.java +++ b/src/main/java/com/microsoft/dhalion/core/MeasurementsArray.java @@ -84,6 +84,12 @@ public class MeasurementsArray { }); } + public MeasurementsArray of(Collection measurements) { + MeasurementsArray array = new MeasurementsArray(); + array.addAll(measurements); + return array; + } + /** * Retains all {@link Measurement}s with given component names. * @@ -262,7 +268,8 @@ public class MeasurementsArray { /** * Sorts the {@link Measurement}s in this collection in the order of the specified keys * - * @param sortKeys one or more sort keys, e.g. {@link SortKey#COMPONENT} + * @param descending false for ascending order, true for descending + * @param sortKeys one or more sort keys, e.g. {@link SortKey#COMPONENT} * @return ordered {@link Measurement}s */ public MeasurementsArray sort(boolean descending, SortKey... sortKeys) { @@ -297,26 +304,14 @@ public class MeasurementsArray { * @return the first {@link Measurement}, if present */ public Measurement first() { - if (measurements.isEmpty()) { - return null; - } - - Table result = measurements.first(1); - Collection measurementCollection = new MeasurementsArray(result).get(); - return measurementCollection.iterator().next(); + return get(0); } /** * @return the last {@link Measurement}, if present */ public Measurement last() { - if (measurements.isEmpty()) { - return null; - } - - Table result = measurements.last(1); - Collection measurementCollection = new MeasurementsArray(result).get(); - return measurementCollection.iterator().next(); + return get(measurements.rowCount() - 1); } /** @@ -325,15 +320,31 @@ public class MeasurementsArray { public Collection get() { ArrayList result = new ArrayList<>(); for (int i = 0; i < measurements.rowCount(); i++) { - result.add(new Measurement(component.get(i), - instance.get(i), - type.get(i), - Instant.ofEpochMilli(timeStamps.get(i)), - value.get(i))); + result.add(row2Obj(i)); } return result; } + /** + * @param index position in the table + * @return {@link Measurement} at the requested position + */ + public Measurement get(int index) { + if (index < 0 || index >= measurements.rowCount() || measurements.isEmpty()) { + return null; + } + + return row2Obj(index); + } + + private Measurement row2Obj(int index) { + return new Measurement(component.get(index), + instance.get(index), + type.get(index), + Instant.ofEpochMilli(timeStamps.get(index)), + value.get(index)); + } + public String toStringForDebugging() { return measurements.print(measurements.rowCount()); } diff --git a/src/main/java/com/microsoft/dhalion/core/SymptomsArray.java b/src/main/java/com/microsoft/dhalion/core/SymptomsArray.java index 37208ba..8eb86a8 100644 --- a/src/main/java/com/microsoft/dhalion/core/SymptomsArray.java +++ b/src/main/java/com/microsoft/dhalion/core/SymptomsArray.java @@ -192,7 +192,8 @@ public class SymptomsArray { /** * Sorts the {@link Symptom}s in this collection in the order of the specified keys * - * @param sortKeys one or more sort keys, e.g. {@link SortKey#ID} + * @param descending false for ascending order, true for descending + * @param sortKeys one or more sort keys, e.g. {@link SortKey#ID} * @return ordered {@link Symptom}s */ public SymptomsArray sort(boolean descending, SortKey... sortKeys) { @@ -255,14 +256,31 @@ public class SymptomsArray { public Collection get() { ArrayList result = new ArrayList<>(); for (int i = 0; i < symptoms.rowCount(); i++) { - result.add(new Symptom(id.get(i), - type.get(i), - Instant.ofEpochMilli(timeStamp.get(i)), - Collections.singletonList(assignment.get(i)))); + result.add(row2Obj(i)); } return result; } + /** + * @param index position in the table + * @return {@link Symptom} at the requested position + */ + public Symptom get(int index) { + if (index < 0 || index >= symptoms.rowCount() || symptoms.isEmpty()) { + return null; + } + + return row2Obj(index); + } + + private Symptom row2Obj(int index) { + return new Symptom(id.get(index), + type.get(index), + Instant.ofEpochMilli(timeStamp.get(index)), + Collections.singletonList(assignment.get(index))); + } + + public String toStringForDebugging() { return symptoms.print(symptoms.rowCount()); }