summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/experimental/theories/extendingwithstubs/GuesserQueue.java
blob: f3eb550eaed4b4b82162ac66897477d6dc0dc187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.junit.tests.experimental.theories.extendingwithstubs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.experimental.theories.PotentialAssignment;
import org.junit.internal.AssumptionViolatedException;

public class GuesserQueue extends ArrayList<ReguessableValue> {
	static class ReguessableDecorator extends ReguessableValue {
		private final PotentialAssignment delegate;
	
		public ReguessableDecorator(PotentialAssignment delegate) {
			this.delegate= delegate;
		}
	
		@Override
		public List<ReguessableValue> reguesses(AssumptionViolatedException e) {
			return Collections.emptyList();
		}
	
		@Override
		public Object getValue() throws CouldNotGenerateValueException {
			return delegate.getValue();
		}

		@Override
		public String getDescription() throws CouldNotGenerateValueException {
			return delegate.getDescription();
		}
	}

	static GuesserQueue forSingleValues(
			List<PotentialAssignment> potentials) {
		GuesserQueue returnThis= new GuesserQueue();
		for (PotentialAssignment potentialParameterValue : potentials) {
			returnThis
					.add(new GuesserQueue.ReguessableDecorator(potentialParameterValue));
		}
		return returnThis;
	}

	private static final long serialVersionUID = 1L;
	private ReguessableValue lastRemoved;

	public void update(AssumptionViolatedException e) {
		if (lastRemoved != null)
			addAll(lastRemoved.reguesses(e));
	}
	
	@Override
	public ReguessableValue remove(int index) {
		lastRemoved = super.remove(index);
		return lastRemoved;
	}
}