From 8607b0a25ac7c06588d3a7e1537799c75be22c9c Mon Sep 17 00:00:00 2001 From: jorge Date: Mon, 1 Apr 2024 20:22:53 +0200 Subject: [PATCH] Optimized the usage of the map with the already processed entities --- .../questionGenerator/generator/AbstractGenerator.java | 9 +++++---- .../questionGenerator/generator/RightAnswerIsEntity.java | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java index 7cfa8d09..636d918e 100644 --- a/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java +++ b/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java @@ -24,12 +24,13 @@ public abstract class AbstractGenerator { private Locale localization = Locale.getDefault(); private ResourceBundle messages; - public static Map alreadyProcessedEntities = new HashMap<>(); + private static Map alreadyProcessedEntities = new HashMap<>(); private String propertyId = ""; private QuestionType type; private static final String MESSAGES_PATH = "messages"; + public AbstractGenerator(String propertyId, QuestionType type) { this.propertyId = propertyId; this.type = type; @@ -90,11 +91,11 @@ public String getPropertyId() { return propertyId; } - public static Map getAlreadyProcessedEntities() { - return new HashMap<>(alreadyProcessedEntities); + public static ItemDocumentImpl getAlreadyProcessedEntity(String id) { + return alreadyProcessedEntities.get(id); } - public static void addItem(String entity, ItemDocumentImpl item) { + public static void addProcessedEntity(String entity, ItemDocumentImpl item) { alreadyProcessedEntities.put(entity, item); } diff --git a/questionGenerator/src/main/java/questionGenerator/generator/RightAnswerIsEntity.java b/questionGenerator/src/main/java/questionGenerator/generator/RightAnswerIsEntity.java index 9ccddb6a..a65130b9 100644 --- a/questionGenerator/src/main/java/questionGenerator/generator/RightAnswerIsEntity.java +++ b/questionGenerator/src/main/java/questionGenerator/generator/RightAnswerIsEntity.java @@ -31,11 +31,11 @@ protected String processRightAnswer(Statement st) { String entity = getRightAnswerEntity(st.getValue().toString()); String answer = ""; try { - ItemDocumentImpl idi = getAlreadyProcessedEntities().get(entity); + ItemDocumentImpl idi = getAlreadyProcessedEntity(entity); if(idi==null) { idi = (ItemDocumentImpl) wbdf.getEntityDocument(entity); answer = getName(idi.getLabels()); - addItem(entity, idi); + addProcessedEntity(entity, idi); } else answer = getName(idi.getLabels()); @@ -46,12 +46,12 @@ protected String processRightAnswer(Statement st) { } protected String getAnswer(String id){ - ItemDocumentImpl idi = alreadyProcessedEntities.get(id); + ItemDocumentImpl idi = getAlreadyProcessedEntity(id); if(idi==null) { try { idi = (ItemDocumentImpl) wbdf.getEntityDocument(id); - alreadyProcessedEntities.put(id, idi); + addProcessedEntity(id, idi); } catch (MediaWikiApiErrorException | IOException e) { /* * * @throws MediaWikiApiErrorException