Skip to content

Commit 8222de4

Browse files
committed
Identifiers utility class
1 parent 5838de1 commit 8222de4

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

src/main/java/scorekeep/GameFactory.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package scorekeep;
22
import java.util.*;
3-
import java.security.SecureRandom;
4-
import java.math.BigInteger;
53
import java.lang.Exception;
64

75
public class GameFactory {
8-
private final SecureRandom random = new SecureRandom();
96
private final HashMap<String, Game> allGames = new HashMap<String, Game>(1);
107
private final GameModel model = new GameModel();
118
private final SessionController sessionController = new SessionController();
@@ -14,7 +11,7 @@ public GameFactory(){
1411
}
1512

1613
public Game newGame(String sessionId) throws SessionNotFoundException, GameNotFoundException {
17-
String gameId = new BigInteger(40, random).toString(32).toUpperCase();
14+
String gameId = Identifiers.random();
1815
Game game = new Game(gameId, sessionId);
1916
model.saveGame(game);
2017
// Register game to session
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package scorekeep;
2+
3+
import java.security.SecureRandom;
4+
import java.math.BigInteger;
5+
6+
public class Identifiers {
7+
private static final SecureRandom secureRandom = new SecureRandom();
8+
9+
public static String random() {
10+
return new BigInteger(40, secureRandom).toString(32).toUpperCase();
11+
}
12+
13+
}

src/main/java/scorekeep/MoveFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package scorekeep;
22

3-
import java.math.BigInteger;
4-
import java.security.SecureRandom;
53
import java.util.HashMap;
64
import java.util.List;
75
import java.util.Set;
@@ -13,7 +11,6 @@
1311

1412
public class MoveFactory {
1513
private static final Logger logger = LoggerFactory.getLogger(MoveFactory.class);
16-
private final SecureRandom random = new SecureRandom();
1714
private final HashMap<String, Move> allMoves = new HashMap<String, Move>(1);
1815
private final MoveModel moveModel = new MoveModel();
1916
private final StateModel stateModel = new StateModel();
@@ -25,8 +22,8 @@ public MoveFactory(){
2522
}
2623

2724
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
28-
String moveId = new BigInteger(40, random).toString(32).toUpperCase();
29-
String stateId = new BigInteger(40, random).toString(32).toUpperCase();
25+
String moveId = Identifiers.random();
26+
String stateId = Identifiers.random();
3027
Move move = new Move(moveId, sessionId, gameId, userId, moveText);
3128
String newStateText = "";
3229
// load game state

src/main/java/scorekeep/SessionFactory.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package scorekeep;
22
import java.util.*;
3-
import java.security.SecureRandom;
4-
import java.math.BigInteger;
53
import java.lang.Exception;
64

75
public class SessionFactory {
8-
private final SecureRandom random = new SecureRandom();
96
private final SessionModel model = new SessionModel();
107

118
public SessionFactory(){
129
}
1310

1411
public Session newSession() {
15-
String id = new BigInteger(40, random).toString(32).toUpperCase();
12+
String id = Identifiers.random();
1613
Session session = new Session(id);
1714
model.saveSession(session);
1815
return session;

src/main/java/scorekeep/StateFactory.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package scorekeep;
22
import java.util.*;
3-
import java.security.SecureRandom;
4-
import java.math.BigInteger;
53
import java.lang.Exception;
64

75
public class StateFactory {
8-
private final SecureRandom random = new SecureRandom();
96
private final StateModel model = new StateModel();
107

118
public StateFactory(){
129
}
1310

1411
public State newState(String sessionId, String gameId, String stateText, Set<String> turn) throws SessionNotFoundException, GameNotFoundException {
15-
String id = new BigInteger(40, random).toString(32).toUpperCase();
12+
String id = Identifiers.random();
1613
State state = new State(id, sessionId, gameId, stateText, turn);
1714
model.saveState(state);
1815
return state;

src/main/java/scorekeep/UserFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@
1010

1111
import java.io.IOException;
1212
import java.io.InputStream;
13-
import java.math.BigInteger;
14-
import java.security.SecureRandom;
1513
import java.util.List;
1614
import java.util.Map;
1715

1816
public class UserFactory {
19-
private final SecureRandom random = new SecureRandom();
2017
private final UserModel model = new UserModel();
2118

2219
public UserFactory(){
2320
}
2421

2522
public User newUser() throws IOException {
26-
String id = new BigInteger(40, random).toString(32).toUpperCase();
23+
String id = Identifiers.random();
2724
User user = new User(id);
2825
String name = randomName();
2926
user.setName(name);
@@ -32,7 +29,7 @@ public User newUser() throws IOException {
3229
}
3330

3431
public User newUser(String name) throws IOException {
35-
String id = new BigInteger(40, random).toString(32).toUpperCase();
32+
String id = Identifiers.random();
3633
User user = new User(id);
3734
user.setName(name);
3835
model.saveUser(user);

0 commit comments

Comments
 (0)