зеркало из https://github.com/microsoft/gctoolkit.git
Xy data set refactor (#97)
* Renamed max() to maxOfY(), changed return type to OptionalDouble, better tests * Moved XYDataSet into samples module * Reformatted XML to avoid hurting Martijn's eyes
This commit is contained in:
Родитель
eb4820f8eb
Коммит
943638b47d
|
@ -44,7 +44,6 @@ module com.microsoft.gctoolkit.api {
|
|||
exports com.microsoft.gctoolkit.io;
|
||||
exports com.microsoft.gctoolkit.jvm;
|
||||
exports com.microsoft.gctoolkit.time;
|
||||
exports com.microsoft.gctoolkit.collections;
|
||||
exports com.microsoft.gctoolkit.util.concurrent;
|
||||
requires java.logging;
|
||||
|
||||
|
|
|
@ -25,6 +25,14 @@
|
|||
<artifactId>gctoolkit-vertx</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.microsoft.gctoolkit.sample.aggregation;
|
||||
|
||||
import com.microsoft.gctoolkit.aggregator.Collates;
|
||||
import com.microsoft.gctoolkit.collections.XYDataSet;
|
||||
import com.microsoft.gctoolkit.sample.collections.XYDataSet;
|
||||
import com.microsoft.gctoolkit.event.GarbageCollectionTypes;
|
||||
import com.microsoft.gctoolkit.time.DateTimeStamp;
|
||||
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
package com.microsoft.gctoolkit.collections;
|
||||
package com.microsoft.gctoolkit.sample.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Collections.unmodifiableList;
|
||||
|
||||
public class XYDataSet {
|
||||
|
||||
private List<Point> dataSeries;
|
||||
private final List<Point> dataSeries;
|
||||
|
||||
public XYDataSet() {
|
||||
dataSeries = new ArrayList<>();
|
||||
|
@ -18,7 +16,6 @@ public class XYDataSet {
|
|||
|
||||
public XYDataSet(XYDataSet series) {
|
||||
dataSeries = new ArrayList<>(series.getItems());
|
||||
|
||||
}
|
||||
|
||||
public void add(Number x, Number y) {
|
||||
|
@ -37,8 +34,11 @@ public class XYDataSet {
|
|||
return dataSeries.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an immutable List of the items in this DataSet.
|
||||
*/
|
||||
public List<Point> getItems() {
|
||||
return unmodifiableList(dataSeries);
|
||||
return List.copyOf(dataSeries);
|
||||
}
|
||||
|
||||
public XYDataSet scaleSeries(double scaleFactor) {
|
||||
|
@ -49,8 +49,15 @@ public class XYDataSet {
|
|||
return scaled;
|
||||
}
|
||||
|
||||
public double max() {
|
||||
return dataSeries.stream().map(Point::getY).mapToDouble(Number::doubleValue).max().getAsDouble();
|
||||
/**
|
||||
* Returns the largest Y value in the XYDataSet as an OptionalDouble,
|
||||
* with an empty optional if the dataset is empty.
|
||||
*/
|
||||
public OptionalDouble maxOfY() {
|
||||
return dataSeries.stream()
|
||||
.map(Point::getY)
|
||||
.mapToDouble(Number::doubleValue)
|
||||
.max();
|
||||
}
|
||||
|
||||
public XYDataSet scaleAndTranslateXAxis(double scale, double offset) {
|
||||
|
@ -71,7 +78,6 @@ public class XYDataSet {
|
|||
}
|
||||
|
||||
public static class Point {
|
||||
|
||||
private final Number x;
|
||||
private final Number y;
|
||||
|
||||
|
@ -83,6 +89,7 @@ public class XYDataSet {
|
|||
public Number getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Number getY() {
|
||||
return y;
|
||||
}
|
|
@ -8,12 +8,12 @@ import com.microsoft.gctoolkit.sample.aggregation.HeapOccupancyAfterCollectionSu
|
|||
* Contains an Aggregator and an Aggregation
|
||||
*/
|
||||
module com.microsoft.gctoolkit.sample {
|
||||
|
||||
requires com.microsoft.gctoolkit.api;
|
||||
requires com.microsoft.gctoolkit.vertx;
|
||||
requires java.logging;
|
||||
|
||||
exports com.microsoft.gctoolkit.sample.aggregation to com.microsoft.gctoolkit.vertx;
|
||||
exports com.microsoft.gctoolkit.sample.collections to gctoolkit.sample.test;
|
||||
|
||||
provides Aggregation with HeapOccupancyAfterCollectionSummary;
|
||||
}
|
||||
|
|
|
@ -1,40 +1,46 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
package com.microsoft.gctoolkit.test.collections;
|
||||
package com.microsoft.gctoolkit.sample.test.collections;
|
||||
|
||||
import com.microsoft.gctoolkit.collections.XYDataSet;
|
||||
import com.microsoft.gctoolkit.collections.XYDataSet.Point;
|
||||
import com.microsoft.gctoolkit.sample.collections.XYDataSet;
|
||||
import com.microsoft.gctoolkit.sample.collections.XYDataSet.Point;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class XYDataSetTest {
|
||||
|
||||
@Test
|
||||
void shouldScaleOnlyY_AxisDataSet() {
|
||||
XYDataSet xyDataSet = new XYDataSet();
|
||||
var xyDataSet = new XYDataSet();
|
||||
xyDataSet.add(new Point(50, 100));
|
||||
XYDataSet scaledPoint = xyDataSet.scaleSeries(2);
|
||||
assertEquals(50, scaledPoint.getItems().get(0).getX());
|
||||
assertEquals(200.0, scaledPoint.getItems().get(0).getY());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldReturnMaximumValueOfY_Axis() {
|
||||
XYDataSet xyDataSet = new XYDataSet();
|
||||
var xyDataSet = new XYDataSet();
|
||||
xyDataSet.add(new Point(50, 100));
|
||||
xyDataSet.add(new Point(100, 140));
|
||||
xyDataSet.add(new Point(75, 230));
|
||||
assertEquals(230, xyDataSet.max());
|
||||
// added a bigger X than Y value
|
||||
xyDataSet.add(new Point(1075, 10));
|
||||
assertEquals(230, xyDataSet.maxOfY().getAsDouble());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnScaledAndTranslatedX_AxisDataSet() {
|
||||
XYDataSet xyDataSet = new XYDataSet();
|
||||
var xyDataSet = new XYDataSet();
|
||||
xyDataSet.add(new Point(50, 100));
|
||||
XYDataSet translated = xyDataSet.scaleAndTranslateXAxis(2, 20);
|
||||
var translated = xyDataSet.scaleAndTranslateXAxis(2, 20);
|
||||
assertEquals(120.0, translated.getItems().get(0).getX());
|
||||
assertEquals(100, translated.getItems().get(0).getY());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxOnEmptyDataSet() {
|
||||
assertTrue(new XYDataSet().maxOfY().isEmpty());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
open module gctoolkit.sample.test {
|
||||
requires com.microsoft.gctoolkit.sample;
|
||||
requires java.logging;
|
||||
requires org.junit.jupiter.api;
|
||||
requires org.junit.jupiter.engine;
|
||||
}
|
Загрузка…
Ссылка в новой задаче