Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add registry
Browse files Browse the repository at this point in the history
OliverSchlueter committed Dec 27, 2024
1 parent b671951 commit c1a35fb
Showing 2 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package de.oliver.fancyholograms.api;

import de.oliver.fancyholograms.api.hologram.Hologram;

import java.util.Collection;
import java.util.Optional;

/**
* An interface for managing the registration and retrieval of holograms.
* Provides methods to register, unregister, and query holograms by their name.
*/
public interface HologramRegistry {

/**
* Registers a hologram in the registry.
*
* @param hologram the hologram to be registered
* @return {@code true} if the registration was successful, otherwise {@code false}
*/
boolean register(Hologram hologram);

/**
* Unregisters the specified hologram from the registry.
*
* @param hologram the hologram to be unregistered
* @return {@code true} if the hologram was successfully unregistered, otherwise {@code false}
*/
boolean unregister(Hologram hologram);

/**
* Checks if a hologram with the specified name exists in the registry.
*
* @param name the name of the hologram to check for existence
* @return {@code true} if a hologram with the specified name exists, otherwise {@code false}
*/
boolean contains(String name);

/**
* Retrieves a hologram by its name from the registry.
*
* @param name the name of the hologram to retrieve
* @return an {@code Optional} containing the hologram if found, or an empty {@code Optional} if no hologram exists with the specified name
*/
Optional<Hologram> get(String name);

/**
* Retrieves a hologram by its name from the registry, ensuring that the hologram exists.
* If no hologram exists with the specified name, this method will throw an exception.
*
* @param name the name of the hologram to retrieve
* @return the hologram associated with the specified name
* @throws IllegalArgumentException if no hologram exists with the given name
*/
Hologram mustGet(String name);

/**
* Retrieves all holograms currently registered in the registry.
*
* @return a collection containing all registered holograms
*/
Collection<Hologram> getAll();

/**
* Retrieves all persistent holograms currently registered in the registry.
*
* @return a collection containing all persistent holograms
*/
Collection<Hologram> getAllPersistent();

/**
* Removes all holograms from the registry, effectively clearing its contents.
*/
void clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package de.oliver.fancyholograms.registry;

import de.oliver.fancyholograms.api.HologramRegistry;
import de.oliver.fancyholograms.api.hologram.Hologram;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class HologramRegistryImpl implements HologramRegistry {

private final Map<String, Hologram> holograms;

public HologramRegistryImpl() {
this.holograms = new ConcurrentHashMap<>();
}

@Override
public boolean register(Hologram hologram) {
return holograms.putIfAbsent(hologram.getName(), hologram) != null;
}

@Override
public boolean unregister(Hologram hologram) {
return holograms.remove(hologram.getName()) != null;
}

@Override
public boolean contains(String name) {
return holograms.containsKey(name);
}

@Override
public Optional<Hologram> get(String name) {
return Optional.ofNullable(holograms.get(name));
}

@Override
public Hologram mustGet(String name) {
if (!contains(name)) {
throw new IllegalArgumentException("Hologram with name " + name + " does not exist!");
}

return holograms.get(name);
}

@Override
public Collection<Hologram> getAll() {
return Collections.unmodifiableCollection(holograms.values());
}

@Override
public Collection<Hologram> getAllPersistent() {
return getAll()
.stream()
.filter(hologram -> hologram.getData().isPersistent())
.toList();
}

@Override
public void clear() {
holograms.clear();
}
}

0 comments on commit c1a35fb

Please sign in to comment.