Skip to contentMethod: build()
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-tictactoe-core.
5:  *
6:  * ipspiel24-tictactoe-core is free software: you can redistribute it and/or modify it under
7:  * the terms of the GNU General Public License as published by the Free Software
8:  * Foundation, either version 3 of the License, or (at your option) any later
9:  * version.
10:  *
11:  * ipspiel24-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13:  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14:  * details.
15:  *
16:  * You should have received a copy of the GNU General Public License along with
17:  * ipspiel24-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel24.tictactoe.core.domain.impl;
20: 
21: import java.util.Optional;
22: 
23: import de.fhdw.gaming.core.domain.GameException;
24: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToePlayer;
25: import de.fhdw.gaming.ipspiel24.tictactoe.core.domain.TicTacToePlayerBuilder;
26: 
27: /**
28:  * Implements {@link TicTacToePlayerBuilder}.
29:  */
30: final class TicTacToePlayerBuilderImpl implements TicTacToePlayerBuilder {
31: 
32:     /**
33:      * The name of the player.
34:      */
35:     private Optional<String> name;
36:     
37:     /**
38:      * {@code true} if the player uses crosses, or {@code false} if she uses noughts.
39:      */
40:     private boolean usingCrosses;
41: 
42:     /**
43:      * Creates an {@link TicTacToePlayerBuilderImpl}.
44:      */
45:     TicTacToePlayerBuilderImpl() {
46:         this.name = Optional.empty();
47:         this.usingCrosses = true;
48:     }
49: 
50:     @Override
51:     public TicTacToePlayerBuilderImpl changeName(final String newName) {
52:         this.name = Optional.of(newName);
53:         return this;
54:     }
55: 
56:     @Override
57:     public TicTacToePlayerBuilderImpl changeUsingCrosses(final boolean newUsingCrosses) {
58:         this.usingCrosses = newUsingCrosses;
59:         return this;
60:     }
61: 
62:     @Override
63:     public boolean isUsingCrosses() {
64:         return this.usingCrosses;
65:     }
66: 
67:     @Override
68:     public TicTacToePlayer build() throws GameException {
69:         return new TicTacToePlayerImpl(this.name.orElseThrow(), this.usingCrosses);
70:     }
71: }