Skip to content

Commit

Permalink
Add a discarding MetricsObserver
Browse files Browse the repository at this point in the history
This is useful for test code where we don't care about the metrics
gathered.
  • Loading branch information
SquidDev committed Oct 19, 2023
1 parent 0ff58cd commit ae5a661
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0

package dan200.computercraft.core.metrics;

/**
* A {@link MetricsObserver} implementation which discards all metrics it receives.
*/
final class DiscardingMetricsObserver implements MetricsObserver {
static final MetricsObserver INSTANCE = new DiscardingMetricsObserver();

private DiscardingMetricsObserver() {
}

@Override
public void observe(Metric.Counter counter) {
}

@Override
public void observe(Metric.Event event, long value) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ public interface MetricsObserver {
* @param value The value corresponding to this event.
*/
void observe(Metric.Event event, long value);

/**
* Get a {@link MetricsObserver} which discards all metrics.
*
* @return An observer which discards all metrics.
*/
static MetricsObserver discard() {
return DiscardingMetricsObserver.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import dan200.computercraft.core.filesystem.FileMount;
import dan200.computercraft.core.filesystem.JarMount;
import dan200.computercraft.core.filesystem.MemoryMount;
import dan200.computercraft.core.metrics.Metric;
import dan200.computercraft.core.metrics.MetricsObserver;

import java.io.File;
Expand All @@ -27,7 +26,7 @@
* A basic implementation of {@link ComputerEnvironment} and {@link GlobalEnvironment}, suitable for a context which
* will only run a single computer.
*/
public class BasicEnvironment implements ComputerEnvironment, GlobalEnvironment, MetricsObserver {
public class BasicEnvironment implements ComputerEnvironment, GlobalEnvironment {
private final WritableMount mount;

public BasicEnvironment() {
Expand Down Expand Up @@ -55,7 +54,7 @@ public double getTimeOfDay() {

@Override
public MetricsObserver getMetrics() {
return this;
return MetricsObserver.discard();
}

@Override
Expand Down Expand Up @@ -124,12 +123,4 @@ private static File getContainingFile(Class<?> klass) {
return new File(url.getPath());
}
}

@Override
public void observe(Metric.Counter counter) {
}

@Override
public void observe(Metric.Event event, long value) {
}
}
13 changes: 2 additions & 11 deletions projects/web/src/main/java/cc/tweaked/web/EmulatedComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import dan200.computercraft.core.computer.ComputerEnvironment;
import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.core.filesystem.MemoryMount;
import dan200.computercraft.core.metrics.Metric;
import dan200.computercraft.core.metrics.MetricsObserver;
import dan200.computercraft.core.terminal.Terminal;
import org.slf4j.Logger;
Expand All @@ -37,7 +36,7 @@
* <p>
* This is exposed to Javascript via the {@link ComputerHandle} interface.
*/
class EmulatedComputer implements ComputerEnvironment, ComputerHandle, MetricsObserver {
class EmulatedComputer implements ComputerEnvironment, ComputerHandle {
private static final Logger LOG = LoggerFactory.getLogger(EmulatedComputer.class);

private static final ComputerSide[] SIDES = ComputerSide.values();
Expand Down Expand Up @@ -124,15 +123,7 @@ public WritableMount createRootMount() {

@Override
public MetricsObserver getMetrics() {
return this;
}

@Override
public void observe(Metric.Counter counter) {
}

@Override
public void observe(Metric.Event event, long value) {
return MetricsObserver.discard();
}

@Override
Expand Down

0 comments on commit ae5a661

Please sign in to comment.