Skip to content
Snippets Groups Projects
Commit 6ce53468 authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Wrap feedback average calculation in Optional type

parent 881dce2b
Branches
No related merge requests found
Pipeline #23630 passed with warnings with stages
in 1 minute and 46 seconds
......@@ -22,12 +22,12 @@ import de.thm.arsnova.model.serialization.View;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* The feedback values of a single session.
*/
public class Feedback {
private static final double Z_THRESHOLD = 0.1;
public static final int MIN_FEEDBACK_TYPE = 0;
public static final int MAX_FEEDBACK_TYPE = 3;
......@@ -52,16 +52,16 @@ public class Feedback {
return values;
}
public double getAverage() {
final double count = this.getCount();
public Optional<Double> getAverage() {
final int count = this.getCount();
if (count == 0) {
return Optional.empty();
}
final double sum = values.get(FEEDBACK_OK) + values.get(FEEDBACK_SLOWER) * 2
+ values.get(FEEDBACK_AWAY) * 3;
if (Math.abs(count) < Z_THRESHOLD) {
return 0;
}
return sum / count;
return Optional.of(sum / count);
}
public int getCount() {
......
......@@ -29,11 +29,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* Performs all feedback related operations.
......@@ -130,11 +126,7 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub
@Override
public double calculateAverageFeedback(final String roomId) {
final Feedback feedback = this.getByRoomId(roomId);
if (feedback.getCount() == 0) {
throw new NoContentException();
}
return feedback.getAverage();
return feedback.getAverage().orElseThrow(NoContentException::new);
}
@Override
......
......@@ -19,6 +19,8 @@ package de.thm.arsnova.model;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.*;
public class FeedbackTest {
......@@ -69,7 +71,7 @@ public class FeedbackTest {
Feedback f = new Feedback(1, 0, 0, 1);
double expected = 1.5;
double actual = f.getAverage();
double actual = f.getAverage().get();
assertEquals(expected, actual, 0.01);
}
......@@ -78,10 +80,9 @@ public class FeedbackTest {
public void averageCalculationShouldAvoidDivisionByZero() {
Feedback f = new Feedback(0, 0, 0, 0);
double expected = 0;
double actual = f.getAverage();
Optional<Double> actual = f.getAverage();
assertEquals(expected, actual, 0.01);
assertFalse(actual.isPresent());
}
@Test
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment