Skip to content

Commit

Permalink
#15: On the way to a more functional design
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geisselmeier committed Sep 15, 2015
1 parent 56a7056 commit 048c64b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javafx.beans.property.SimpleLongProperty;

public class AverageExecutionTimeRule extends NumericRule<Long> {

private final LongProperty limit;

public AverageExecutionTimeRule(long limit) {
Expand All @@ -31,7 +31,7 @@ public Property<Number> limitProperty() {
@Override
public EvaluationResult<Long> evaluate(MBeanServerConnection mbsc) {
try {
final long execTime = JmxHelper.getAverageExecutionTime(mbsc);
final long execTime = jmxFunction().apply(mbsc);
if (execTime <= limit.get()) {
return new EvaluationResult<Long>(HealthStatus.GOOD, execTime);
} else {
Expand All @@ -56,4 +56,9 @@ public boolean equals(Object other) {
public String toString() {
return String.format("Average Excecution Time of %d ms", getLimit());
}

@Override
protected ExceptionalFunction<MBeanServerConnection, Long, JMException> jmxFunction() {
return JmxHelper::getAverageExecutionTime;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package org.levigo.jadice.server.converterclient.gui.clusterhealth.rule;

import javax.management.JMException;
import javax.management.MBeanServerConnection;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;

public abstract class NumericRule<T extends Number> implements Rule<T> {

interface ExceptionalFunction<T, R, E extends Exception> {
R apply(T t) throws E;
}

private BooleanProperty enabledProperty = new SimpleBooleanProperty(true);

abstract public EvaluationResult<T> evaluate(MBeanServerConnection mbsc);

abstract Property<Number> limitProperty();

@Override
Expand All @@ -23,4 +32,6 @@ public void setLimit(Number value) {
limitProperty().setValue(value);
}

abstract protected ExceptionalFunction<MBeanServerConnection, T, JMException> jmxFunction();

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Property<Number> limitProperty() {
@Override
public EvaluationResult<Long> evaluate(MBeanServerConnection mbsc) {
try {
final long execTime = JmxHelper.getRecentAverageExecutionTime(mbsc);
final long execTime = jmxFunction().apply(mbsc);
if (execTime <= limit.get()) {
return new EvaluationResult<Long>(HealthStatus.GOOD, execTime);
} else {
Expand All @@ -57,4 +57,9 @@ public boolean equals(Object other) {
public String toString() {
return String.format("Recent Average Excecution Time of %d ms", getLimit());
}

@Override
protected ExceptionalFunction<MBeanServerConnection, Long, JMException> jmxFunction() {
return JmxHelper::getRecentAverageExecutionTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Property<Number> limitProperty() {
@Override
public EvaluationResult<Float> evaluate(MBeanServerConnection mbsc) {
try {
final float eff = JmxHelper.getEfficiency10Min(mbsc);
final float eff = jmxFunction().apply(mbsc);
if (eff <= limit.get()) {
return new EvaluationResult<Float>(HealthStatus.GOOD, eff);
} else {
Expand All @@ -56,4 +56,9 @@ public boolean equals(Object other) {
public String toString() {
return String.format("Recent Efficiency of %f", getLimit());
}

@Override
protected ExceptionalFunction<MBeanServerConnection, Float, JMException> jmxFunction() {
return JmxHelper::getEfficiency10Min;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Property<Number> limitProperty() {
@Override
public EvaluationResult<Float> evaluate(MBeanServerConnection mbsc) {
try {
final float rate = JmxHelper.getRecentFailureRate(mbsc);
final float rate = jmxFunction().apply(mbsc);
if (rate <= limit.get()) {
return new EvaluationResult<Float>(HealthStatus.GOOD, rate);
} else {
Expand All @@ -56,4 +56,9 @@ public boolean equals(Object other) {
public String toString() {
return String.format("Recent Failure Rate of %f", getLimit());
}

@Override
protected ExceptionalFunction<MBeanServerConnection, Float, JMException> jmxFunction() {
return JmxHelper::getRecentFailureRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public boolean equals(Object other) {
public String toString() {
return String.format("Failure Rate of %f", getLimit());
}

@Override
protected ExceptionalFunction<MBeanServerConnection, Float, JMException> jmxFunction() {
return JmxHelper::getTotalFailureRate;
}

}

0 comments on commit 048c64b

Please sign in to comment.