Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2024 Berner Fachhochschule https://e-voting.bfh.ch
*
* - This program is free software: you can redistribute it and/or modify -
* - it under the terms of the GNU Affero General Public License as published by -
* - the Free Software Foundation, either version 3 of the License, or -
* - (at your option) any later version. -
* - -
* - This program is distributed in the hope that it will be useful, -
* - but WITHOUT ANY WARRANTY; without even the implied warranty of -
* - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -
* - GNU General Public License for more details. -
* - -
* - You should have received a copy of the GNU Affero General Public License -
* - along with this program. If not, see . -
*/
package ch.openchvote.framework.party;
import ch.openchvote.framework.annotations.state.Notify;
import ch.openchvote.framework.communication.*;
import ch.openchvote.framework.exceptions.CommunicationException;
import ch.openchvote.framework.exceptions.FrameworkException;
import ch.openchvote.framework.exceptions.StateException;
import ch.openchvote.framework.interfaces.Identifiable;
import ch.openchvote.framework.interfaces.UserInterface;
import ch.openchvote.framework.protocol.Phase;
import ch.openchvote.framework.services.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static ch.openchvote.framework.exceptions.FrameworkException.Type.MISSING_CONSTRUCTOR;
import static ch.openchvote.framework.exceptions.FrameworkException.Type.STATE_CLASS_NOT_FOUND;
import static ch.openchvote.framework.exceptions.StateException.Type.*;
/**
* This is a generic base class for a party's states in a protocol. The generic type parameter {@code P} represents the
* type of the party that deals with a particular state object. State classes inheriting from {@link State} need to be
* defined for every protocol and every party in that protocol. State transitions are implemented by registering message
* handlers, request handlers, response handler, input handlers, and output handlers.
*
* @param
The type of the state's party
* @param The type of the state's event context
*/
public abstract class State
> implements Identifiable {
// protected fields for making them accessible in subclasses
protected final P party;
protected final E eventContext;
// several maps for storing the registered handlers
private final Map> messageHandlers;
private final Map> mailHandlers;
private final Map> requestHandlers;
private final Map> responseHandlers;
private final Map> inputHandlers;
private final Map> outputHandlers;
private final Map> publicationHandlers;
// protected constructor for internal use in subclasses
protected State(P party, E eventContext) {
this.party = party;
this.eventContext = eventContext;
this.messageHandlers = new HashMap<>();
this.mailHandlers = new HashMap<>();
this.requestHandlers = new HashMap<>();
this.responseHandlers = new HashMap<>();
this.inputHandlers = new HashMap<>();
this.outputHandlers = new HashMap<>();
this.publicationHandlers = new HashMap<>();
}
// protected method to register a message handler
protected void registerMessageHandler(Class extends Content> contentClass, Consumer messageHandler) {
var contentId = Content.getId(contentClass);
if (this.messageHandlers.containsKey(contentId)) {
throw new StateException(DUPLICATE_MESSAGE_HANDLER, this);
}
this.messageHandlers.put(contentId, messageHandler);
}
protected void registerMailHandler(Class extends Content> contentClass, Consumer mailHandler) {
var contentId = Content.getId(contentClass);
if (this.mailHandlers.containsKey(contentId)) {
throw new StateException(DUPLICATE_MAIL_HANDLER, this);
}
this.mailHandlers.put(contentId, mailHandler);
}
protected void registerRequestHandler(Class extends Content> contentClass, Consumer requestHandler) {
var contentId = Content.getId(contentClass);
if (this.requestHandlers.containsKey(contentId)) {
throw new StateException(DUPLICATE_REQUEST_HANDLER, this);
}
this.requestHandlers.put(contentId, requestHandler);
}
protected void registerResponseHandler(Class extends Content> contentClass, Consumer responseHandler) {
var contentId = Content.getId(contentClass);
if (this.responseHandlers.containsKey(contentId)) {
throw new StateException(DUPLICATE_RESPONSE_HANDLER, this);
}
this.responseHandlers.put(contentId, responseHandler);
}
protected void registerInputHandler(Class contentClass, Consumer inputHandler) {
var contentId = Content.getId(contentClass);
if (this.inputHandlers.containsKey(contentId)) {
throw new StateException(DUPLICATE_INPUT_HANDLER, this);
}
this.inputHandlers.put(contentId, inputHandler);
}
protected void registerOutputHandler(Class contentClass, Consumer