-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: increase performance of Organizationrepository
- Loading branch information
Fredrik Marjoni
committed
Mar 3, 2026
1 parent
c334f4e
commit dcc32bf
Showing
3 changed files
with
37 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 35 additions & 30 deletions
65
src/main/java/edu/group5/app/model/organization/OrganizationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,63 @@ | ||
| package edu.group5.app.model.organization; | ||
|
|
||
| import edu.group5.app.model.Repository; | ||
| import tools.jackson.core.type.TypeReference; | ||
| import tools.jackson.databind.ObjectMapper; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Handles the business logic associated with organizations | ||
| */ | ||
| public class OrganizationRepository extends Repository<Integer, Organization> { | ||
| private final HashMap<Integer, Organization> grandMap; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public OrganizationRepository(Object[] input) { | ||
| grandMap = new HashMap<>(); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
|
|
||
| if (input == null) { | ||
| throw new NullPointerException("Input cannot be null"); | ||
| } | ||
| for (Object obj : input) { | ||
| Map<String, Object> map = (Map<String, Object>) obj; | ||
| HashMap<String, Object> contentMap = | ||
| mapper.convertValue(obj, new TypeReference<HashMap<String, Object>>() {}); | ||
|
|
||
| String orgNumberStr = ((String) contentMap.get("org_number")).replaceAll("\\s", ""); | ||
| int orgNumber = Integer.parseInt(orgNumberStr); | ||
|
|
||
| int orgNumber = Integer.parseInt((String) map.get("org_number")); | ||
| String name = (String) map.get("name"); | ||
| String name = (String) contentMap.get("name"); | ||
|
|
||
| boolean trusted = | ||
| "approved".equalsIgnoreCase((String) map.get("status")); | ||
| String url = (String) map.get("url"); | ||
|
|
||
| boolean isPreApproved = (Boolean) map.get("is_pre_approved"); | ||
| boolean trusted = "approved".equalsIgnoreCase((String) contentMap.get("status")); | ||
|
|
||
| Organization organization = new Organization( | ||
| orgNumber, name, trusted, url, | ||
| isPreApproved, | ||
| "" // description not included in API | ||
| ); | ||
| String websiteURL = (String) contentMap.get("url"); | ||
|
|
||
| boolean isPreApproved = true; | ||
| if (!contentMap.containsKey("is_pre_approved")) { | ||
| isPreApproved = false; | ||
| } else if ((boolean)contentMap.get("is_pre_approved") == false) { | ||
| isPreApproved = false; | ||
| } | ||
|
|
||
| content.put(orgNumber, organization); | ||
| String description = "Information about " + name; | ||
|
|
||
| Organization org = new Organization(orgNumber, name, trusted, websiteURL, isPreApproved, description); | ||
|
|
||
| grandMap.put(org.orgNumber(), org); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets all trusted organizations in the repository | ||
| * @return all organizations with trusted = true | ||
| */ | ||
| public Map<Integer, Organization> getTrustedOrganizations() { | ||
| /** | ||
| * Gets all trusted organizations in the repository | ||
| * @return all organizations with trusted = true | ||
| */ | ||
| public Map<Integer, Organization> getTrustedOrganizations() { | ||
| Map<Integer, Organization> trustedOrganizations = new HashMap<>(); | ||
|
|
||
| content.forEach((orgNr, org) -> { | ||
| if (org.trusted()) { | ||
| trustedOrganizations.put(orgNr, org); | ||
| } | ||
| grandMap.forEach((orgNr, org) -> { | ||
| if (org.trusted()) { | ||
| trustedOrganizations.put(orgNr, org); | ||
| } | ||
| }); | ||
|
|
||
| return trustedOrganizations; | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters