· 5 years ago · May 12, 2020, 08:26 PM
1package tech.shitzuu.tickets.data.handlers;
2
3import org.slf4j.Logger;
4import org.slf4j.LoggerFactory;
5import tech.shitzuu.tickets.ActionType;
6import tech.shitzuu.tickets.utilities.Configuration;
7
8import java.sql.ResultSet;
9import java.sql.SQLException;
10import java.util.HashSet;
11import java.util.Set;
12import java.util.concurrent.atomic.AtomicInteger;
13
14public class IdentifierHandler {
15
16 private static final AtomicInteger identifierCounter = new AtomicInteger();
17
18 private static final Set<Integer> identifiers = new HashSet<>();
19
20 private static final Logger logger = LoggerFactory.getLogger(IdentifierHandler.class);
21
22 // "generateIdentifier" method is used to generate new Identifier, or get actually not used.
23 public static Integer generateIdentifier() {
24 if (identifiers.isEmpty()) { // If list with unused identifiers is empty, creating a new Identifier, then returning it.
25 return identifierCounter.getAndIncrement();
26 }
27
28 // Getting a first value from "identifiers" list, then returning it.
29 final Integer selectedIdentifier = identifiers.stream().findFirst().orElse(null); // Searching a free identifier in `identifiers` set.
30 manageIdentifier(selectedIdentifier, ActionType.DELETE); // Deleting searched identifier from database.
31
32 return selectedIdentifier; // Returning selected identifier from `identifiers` set
33 }
34
35 // "manageIdentifier" method is used to manipulate value in database. Should be provided ActionType and Identifier
36 // where this should be executed.
37 public static void manageIdentifier(final Integer identifier, final ActionType actionType) { // "identifier" value should be Integer, "actionType" value should be ActionType.
38 switch (actionType) { // Switching provided action type as ActionType value.
39 case CREATE: // Creating a new unused identifier.
40 Configuration.getStorage().executeUpdate("INSERT INTO `Identifiers` (`id`, `identifier`) VALUES (NULL, '" + identifier + "');"); // adding "identifier" value to database.
41 identifiers.add(identifier); // adding "identifier" value to "identifiers" list, then break code.
42 break;
43 case DELETE: // Deleting an exists identifier from unused list.
44 Configuration.getStorage().executeUpdate("DELETE FROM `Identifiers` WHERE `id` = '" + identifier + "';"); // removing "identifier" value from database.
45 identifiers.remove(identifier); // removing "identifier" value from "identifiers" list, then break code.
46 break;
47 case LOAD: // Loading values from database. (Table: "identifiers")
48 final ResultSet resultSet = Configuration.getStorage().executeQuery("SELECT * from `Identifiers`;");
49
50 try {
51 while (resultSet.next()) { // If ResultSet has next, adding to `identifiers` list.
52 identifiers.add( resultSet.getInt("identifier") );
53 } // then closing resultSet, and printing info
54 resultSet.close();
55 logger.info("Successfully loaded data from `Identifiers` table. (Objects size: " + identifiers.size() + ")");
56 } catch (SQLException exception) { // If an unexpected error occurred while loading errors.
57 logger.error("An unexpected error occurred when loading identifiers.", exception);
58 }
59
60 break;
61 }
62 }