<commit_before>package info.gehrels.voting.web;

import com.google.common.collect.ImmutableSet;
import info.gehrels.voting.Ballot;
import info.gehrels.voting.Ballot.ElectionCandidatePreference;
import info.gehrels.voting.genderedElections.GenderedCandidate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;


@Controller
public class CastVoteController {
	private final BallotLayoutState ballotLayoutState;
	private final CastBallotsState castBallotsState;

	public CastVoteController(BallotLayoutState ballotLayoutState, CastBallotsState castBallotsState) {
		this.ballotLayoutState = ballotLayoutState;
		this.castBallotsState = castBallotsState;
	}

	@RequestMapping(value = {"/castVote"}, method = {POST, PUT})
	public String doCastVote(@ModelAttribute CastVoteForm castVoteForm) {
		castBallotsState.castBallotsById.put(castVoteForm.getBallotId(), new Ballot<>(
			ImmutableSet.of(
				new ElectionCandidatePreference<>(ballotLayoutState.ballotLayout.getElections().get(0),
				                                  ImmutableSet.<GenderedCandidate>of()))));

		return "redirect:/castVote";
	}

	@RequestMapping("/castVote")
	public ModelAndView doGet() {
		if (ballotLayoutState.ballotLayout == null) {
			return new ModelAndView("redirect:/");
		}

		return new ModelAndView("castVote", "ballotLayout", ballotLayoutState.ballotLayout);
	}

}
<commit_msg>Generating and storing complete Ballots on submission of the castVote form<commit_after>package info.gehrels.voting.web;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
import info.gehrels.voting.Ballot;
import info.gehrels.voting.Ballot.ElectionCandidatePreference;
import info.gehrels.voting.genderedElections.GenderedCandidate;
import info.gehrels.voting.genderedElections.GenderedElection;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;


@Controller
public class CastVoteController {
	private final BallotLayoutState ballotLayoutState;
	private final CastBallotsState castBallotsState;

	public CastVoteController(BallotLayoutState ballotLayoutState, CastBallotsState castBallotsState) {
		this.ballotLayoutState = ballotLayoutState;
		this.castBallotsState = castBallotsState;
	}

	@RequestMapping(value = "/castVote", method = {POST, PUT})
	public String doCastVote(@ModelAttribute CastVoteForm castVoteForm) {
		castBallotsState.castBallotsById.put(castVoteForm.getBallotId(), createBallotFromForm(castVoteForm));

		return "redirect:/castVote";
	}

	@RequestMapping(value = "/castVote", method = {HEAD, GET})
	public ModelAndView doGet() {
		if (ballotLayoutState.ballotLayout == null) {
			return new ModelAndView("redirect:/");
		}

		return new ModelAndView("castVote", "ballotLayout", ballotLayoutState.ballotLayout);
	}

	private Ballot<GenderedCandidate> createBallotFromForm(CastVoteForm castVoteForm) {
		int i = 0;
		Builder<ElectionCandidatePreference<GenderedCandidate>> preferenceSetBuilder = ImmutableSet.builder();
		for (GenderedElection genderedElection : ballotLayoutState.ballotLayout.getElections()) {
			ElectionCandidatePreference<GenderedCandidate> preference = createPreference(genderedElection, castVoteForm
				.getVotesByElectionId().get(i));
			preferenceSetBuilder.add(preference);
			i++;
		}

		return new Ballot<>(preferenceSetBuilder.build());
	}

	private ElectionCandidatePreference<GenderedCandidate> createPreference(GenderedElection genderedElection,
	                                                                        String preferenceString) {
		Builder<GenderedCandidate> preferenceBuilder = ImmutableSet.builder();
		for (char c : preferenceString.toUpperCase().toCharArray()) {
			int candidateIndex = c-'A';
			preferenceBuilder.add(Iterables.get(genderedElection.getCandidates(), candidateIndex));
		}
		return new ElectionCandidatePreference<>(genderedElection, preferenceBuilder.build());
	}

}

{'fraction_non_alphanumeric': 0.09586426299045599, 'fraction_numerical': 0.0004241781548250265, 'mean_word_length': 6.038805970149253, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7588531', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1352, 'n_words': 225}