Skip to content

Commit

Permalink
fixing bug in progress which prevented the first count to be saved to…
Browse files Browse the repository at this point in the history
… a list
  • Loading branch information
AndreaSuckro committed Oct 4, 2016
1 parent 6b56ada commit 55030b5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* DTO to save the progress of an action
Expand Down Expand Up @@ -80,12 +83,16 @@ public Builder withActionName(Event.Name value) {
this.item.actionName = value;
return this;
}
public Builder withWinnersSoFarList(List<Bucket.Label> value) {
this.item.winnersSoFar = value;
public Builder withWinnersSoFarList(Collection<Bucket.Label> value) {
Set<Bucket.Label> winnerSoFar = new HashSet<>();
winnerSoFar.addAll(value);
this.item.winnersSoFar = winnerSoFar;
return this;
}
public Builder withLosersSoFarList(List<Bucket.Label> value) {
this.item.losersSoFar = value;
public Builder withLosersSoFarList(Collection<Bucket.Label> value) {
Set<Bucket.Label> loserSoFar = new HashSet<>();
loserSoFar.addAll(value);
this.item.losersSoFar = loserSoFar;
return this;
}
public Builder withSufficientData(Boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* DTO to save the progress of a bucket or an action. <br>
Expand All @@ -41,31 +44,29 @@
public class Progress implements Cloneable {

@ApiModelProperty(value = "list of winning buckets", required = true)
//todo: this should really be a set
protected List<Bucket.Label> winnersSoFar;
protected Set<Bucket.Label> winnersSoFar;
@ApiModelProperty(value = "list of losing buckets", required = true)
//todo: this should really be a set
protected List<Bucket.Label> losersSoFar;
protected Set<Bucket.Label> losersSoFar;
@ApiModelProperty(value = "if sufficient data has been collected to observe the effect size of interest",
required = true)
protected boolean hasSufficientData;
@ApiModelProperty(value = "fraction of data that has been collected to observe the effect size of interest",
required = true)
protected Double fractionDataCollected;

public List<Bucket.Label> getWinnersSoFar() {
public Set<Bucket.Label> getWinnersSoFar() {
return winnersSoFar;
}

public void setWinnersSoFar(List<Bucket.Label> value) {
public void setWinnersSoFar(Set<Bucket.Label> value) {
this.winnersSoFar = value;
}

public List<Bucket.Label> getLosersSoFar() {
public Set<Bucket.Label> getLosersSoFar() {
return losersSoFar;
}

public void setLosersSoFar(List<Bucket.Label> value) {
public void setLosersSoFar(Set<Bucket.Label> value) {
this.losersSoFar = value;
}

Expand All @@ -88,23 +89,21 @@ public void setFractionDataCollected(Double value) {
@JsonIgnore
public void addToWinnersSoFarList(Bucket.Label winner) {
if (this.winnersSoFar == null) {
this.winnersSoFar = new ArrayList<>();
this.winnersSoFar = new HashSet<>();
} else if (winner == null) {
throw new IllegalArgumentException();
} else {
this.winnersSoFar.add(winner);
}
this.winnersSoFar.add(winner);
}

@JsonIgnore
public void addToLosersSoFarList(Bucket.Label loser) {
if (this.losersSoFar == null) {
this.losersSoFar = new ArrayList<>();
this.losersSoFar = new HashSet<>();
} else if (loser == null) {
throw new IllegalArgumentException();
} else {
this.losersSoFar.add(loser);
}
this.losersSoFar.add(loser);
}

@Override
Expand Down Expand Up @@ -140,13 +139,17 @@ public Builder() {
this.item = new Progress();
}

public Builder withWinnersSoFar(List<Bucket.Label> value) {
this.item.winnersSoFar = value;
public Builder withWinnersSoFar(Collection<Bucket.Label> value) {
Set<Bucket.Label> winnerSoFar = new HashSet<>();
winnerSoFar.addAll(value);
this.item.winnersSoFar = winnerSoFar;
return this;
}

public Builder withLosersSoFar(List<Bucket.Label> value) {
this.item.losersSoFar = value;
public Builder withLosersSoFar(Collection<Bucket.Label> value) {
Set<Bucket.Label> losersSoFar = new HashSet<>();
losersSoFar.addAll(value);
this.item.losersSoFar = losersSoFar;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static junit.framework.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
Expand All @@ -33,8 +35,8 @@
public class ActionProgressTest {

private Event.Name actionName;
private List<Bucket.Label> winnersSoFar;
private List<Bucket.Label> losersSoFar;
private Set<Bucket.Label> winnersSoFar;
private Set<Bucket.Label> losersSoFar;
private boolean hasSufficientData;
private Double fractionDataCollected;
private Progress progress;
Expand All @@ -43,8 +45,8 @@ public class ActionProgressTest {
@Before
public void setup(){
actionName = Event.Name.valueOf("TestAction");
winnersSoFar = new ArrayList<>();
losersSoFar = new ArrayList<>();
winnersSoFar = new HashSet<>();
losersSoFar = new HashSet<>();
Bucket.Label winner = Bucket.Label.valueOf("TestWinner");
Bucket.Label loser = Bucket.Label.valueOf("TestLoser");
winnersSoFar.add(winner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static junit.framework.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -33,16 +35,16 @@
*/
public class ProgressTest {

private List<Bucket.Label> winnersSoFar;
private List<Bucket.Label> losersSoFar;
private Set<Bucket.Label> winnersSoFar;
private Set<Bucket.Label> losersSoFar;
private boolean hasSufficientData;
private Double fractionDataCollected;
private Progress progress;

@Before
public void setup(){
winnersSoFar = new ArrayList<>();
losersSoFar = new ArrayList<>();
winnersSoFar = new HashSet<>();
losersSoFar = new HashSet<>();
Bucket.Label winner = Bucket.Label.valueOf("TestWinner");
Bucket.Label loser = Bucket.Label.valueOf("TestLoser");
winnersSoFar.add(winner);
Expand Down Expand Up @@ -79,8 +81,9 @@ public void testBuilder(){
@Test
public void testAddWinners(){
progress.setWinnersSoFar(null);
progress.addToWinnersSoFarList(Bucket.Label.valueOf("TestWinner"));
assertNotNull(progress.getWinnersSoFar());
Bucket.Label bucketLabel = Bucket.Label.valueOf("TestWinner");
progress.addToWinnersSoFarList(bucketLabel);
assertTrue(progress.getWinnersSoFar().contains(bucketLabel));
try{
progress.addToWinnersSoFarList(null);
fail();
Expand All @@ -94,8 +97,9 @@ public void testAddWinners(){
@Test
public void testAddLosers() {
progress.setLosersSoFar(null);
progress.addToLosersSoFarList(Bucket.Label.valueOf("TestLoser"));
assertNotNull(progress.getLosersSoFar());
Bucket.Label bucketLabel = Bucket.Label.valueOf("TestLoser");
progress.addToLosersSoFarList(bucketLabel);
assertTrue(progress.getLosersSoFar().contains(bucketLabel));
try {
progress.addToLosersSoFarList(null);
fail();
Expand Down

0 comments on commit 55030b5

Please sign in to comment.