View Javadoc
1   package civitas.common.ballotdesign;
2   
3   import org.springframework.beans.factory.annotation.Autowired;
4   import org.springframework.stereotype.Controller;
5   
6   import civitas.common.CommonConstants;
7   import civitas.common.VoteChoice;
8   import civitas.common.ballot.Ballot;
9   
10  @Controller
11  public class CheckBallotAgainstBallotDesign implements CommonConstants {
12  
13  	@Autowired
14  	CalculateBallotLength calculateBallotLength;
15  
16  	@Autowired
17  	CalculatePositionInBallot calculatePositionInBallot;
18  
19  	public void apply(final BallotDesign that, final Ballot b) {
20  
21  		if (b.k != that.candidates.length || b.matrix.length != calculateBallotLength.apply(b.k)) {
22  			throw new IllegalArgumentException("The ballot's matrix size is not correct.");
23  		}
24  		for (int i = 0; i < b.k; i++) {
25  			for (int j = i + 1; j < b.k; j++) {
26  				VoteChoice choice = b.matrix[calculatePositionInBallot.apply(i, j, b.k)];
27  				if (null == choice) {
28  					throw new IllegalArgumentException("Illegal choice for (" + i + "," + j + ")");
29  				}
30  			}
31  		}
32  	}
33  }