summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica/replica2/Resources.java
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-28 17:32:28 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-28 17:32:28 -0500
commitd267dd1dda606f0c56d8afaa7187485e60ebfd86 (patch)
treee1bca5933aa7e5e9793773057fd5616ff65a9eb8 /src/main/java/derms/replica/replica2/Resources.java
parent6654546671eea9f9becd32b3160a134802659cbc (diff)
downloadsoen423-d267dd1dda606f0c56d8afaa7187485e60ebfd86.zip
move replica2 to top level
Diffstat (limited to 'src/main/java/derms/replica/replica2/Resources.java')
-rw-r--r--src/main/java/derms/replica/replica2/Resources.java74
1 files changed, 0 insertions, 74 deletions
diff --git a/src/main/java/derms/replica/replica2/Resources.java b/src/main/java/derms/replica/replica2/Resources.java
deleted file mode 100644
index 03bf33f..0000000
--- a/src/main/java/derms/replica/replica2/Resources.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package derms.replica.replica2;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.concurrent.ConcurrentHashMap;
-
-class Resources {
- private Map<ResourceType, Map<ResourceID, Resource>> resources;
-
- Resources() {
- this.resources = new ConcurrentHashMap<ResourceType, Map<ResourceID, Resource>>();
- }
-
- List<Resource> borrowed(CoordinatorID borrower, ResourceType name) {
- List<Resource> borrowed = new ArrayList<Resource>();
- Resource[] namedResources = getByName(name);
- for (Resource r : namedResources) {
- if (r.isBorrowed && r.borrower.equals(borrower)) {
- borrowed.add(r);
- }
- }
- return borrowed;
- }
-
- Resource getByID(ResourceID id) throws NoSuchElementException {
- for (Map<ResourceID, Resource> rids : resources.values()) {
- Resource resource = rids.get(id);
- if (resource != null) {
- return resource;
- }
- }
- throw new NoSuchElementException("No such resource "+id);
- }
-
- Resource[] getByName(ResourceType name) {
- Map<ResourceID, Resource> rids = resources.get(name);
- if (rids == null) {
- return new Resource[0];
- }
- Resource[] r = new Resource[0];
- return rids.values().toArray(r);
- }
-
- void add(Resource r) {
- Map<ResourceID, Resource> rids;
- synchronized (resources) {
- rids = resources.get(r.type);
- if (rids == null) {
- rids = new ConcurrentHashMap<ResourceID, Resource>();
- resources.put(r.type, rids);
- }
- }
- synchronized (rids) {
- Resource existing = rids.get(r.id);
- if (existing != null) {
- existing.duration += r.duration;
- } else {
- rids.put(r.id, r);
- }
- }
- }
-
- void removeByID(ResourceID id) throws NoSuchElementException {
- for (Map<ResourceID, Resource> rids : resources.values()) {
- if (rids.containsKey(id)) {
- rids.remove(id);
- return;
- }
- }
- throw new NoSuchElementException("No such resource "+id);
- }
-}