1
1
package org .rulez .demokracia .pdengine ;
2
2
3
+ import java .util .List ;
4
+
3
5
import org .rulez .demokracia .pdengine .BeatTable .Direction ;
4
6
import org .rulez .demokracia .pdengine .dataobjects .Pair ;
5
7
import org .rulez .demokracia .pdengine .exception .ReportedException ;
6
8
import org .rulez .demokracia .types .Matrix ;
7
9
8
- public interface ContainingBeats extends Matrix <Choice , Pair >{
9
-
10
+ public interface ContainingBeats extends Matrix <String , Pair > {
11
+
10
12
long serialVersionUID = 1L ;
13
+
11
14
default void checkPair (final Pair pair ) {
12
15
if (pair == null )
13
16
throw new ReportedException ("Invalid Pair key" );
14
17
}
15
-
16
- default int beatInformation (final Choice choice1 , final Choice choice2 , final Direction direction ) {
18
+
19
+ default int beatInformation (final String choice1 , final String choice2 , final Direction direction ) {
17
20
if (direction == null )
18
21
throw new ReportedException ("Invalid direction" );
19
-
22
+
20
23
int result = 0 ;
21
24
Pair pair = getElement (choice1 , choice2 );
22
-
23
25
24
26
if (direction .equals (Direction .DIRECTION_FORWARD ))
25
27
result = pair .winning ;
@@ -28,23 +30,60 @@ default int beatInformation(final Choice choice1, final Choice choice2, final Di
28
30
29
31
return result ;
30
32
}
31
-
33
+
32
34
default Pair compareBeats (final Pair beat1 , final Pair beat2 ) {
33
35
checkPair (beat1 );
34
36
checkPair (beat2 );
35
-
37
+
36
38
if (beat1 .winning > beat2 .winning )
37
39
return beat1 ;
38
40
else if (beat1 .winning < beat2 .winning )
39
41
return beat2 ;
42
+ else if (beat1 .losing < beat2 .losing )
43
+ return beat1 ;
44
+ else if (beat2 .losing < beat1 .losing )
45
+ return beat2 ;
40
46
else
41
- if (beat1 .losing < beat2 .losing )
42
- return beat1 ;
43
- else if (beat2 .losing < beat1 .losing )
44
- return beat2 ;
45
- else
46
- throw new UnsupportedOperationException ();
47
+ throw new UnsupportedOperationException ();
48
+ }
49
+
50
+ default void initialize (List <CastVote > castVotes ) {
51
+ if (castVotes == null )
52
+ throw new ReportedException ("Invalid castVotes" );
53
+
54
+
55
+ for (CastVote castVote : castVotes ) {
56
+ List <RankedChoice > preferences = castVote .getPreferences ();
57
+
58
+ for (int columnIndex = 0 ; columnIndex < preferences .size (); columnIndex ++) {
59
+ RankedChoice column = preferences .get (columnIndex );
60
+ for (int rowIndex = columnIndex + 1 ; rowIndex < preferences .size (); rowIndex ++) {
61
+ RankedChoice row = preferences .get (rowIndex );
62
+ if (column .rank > row .rank ) {
63
+ increasePairValue (preferences , columnIndex , rowIndex );
64
+ } else if (column .rank < row .rank ){
65
+ increasePairValue (preferences , rowIndex , columnIndex );
66
+ }
67
+ }
68
+ }
69
+ }
47
70
}
48
71
49
-
72
+
73
+ default Pair getPair (String beats1 , String beats2 ) {
74
+ Pair result = getElement (beats1 , beats2 );
75
+ if (result == null )
76
+ return new Pair (0 , 0 );
77
+ return result ;
78
+ }
79
+
80
+ default void increasePairValue (List <RankedChoice > preferences , int column , int row ) {
81
+ Pair value1 = getPair (preferences .get (column ).choiceId , preferences .get (row ).choiceId );
82
+ setElement (preferences .get (column ).choiceId , preferences .get (row ).choiceId ,
83
+ new Pair (value1 .winning + 1 , value1 .losing ));
84
+
85
+ Pair value2 = getPair (preferences .get (row ).choiceId , preferences .get (column ).choiceId );
86
+ setElement (preferences .get (row ).choiceId , preferences .get (column ).choiceId ,
87
+ new Pair (value2 .winning , value2 .losing + 1 ));
88
+ }
50
89
}
0 commit comments