Bat/src/main/java/cc/fascinated/bat/common/ProfileHolder.java
Liam 73e4b58695
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 37s
add premium to guilds and re-enable the global commands that are supposed to be global
2024-06-27 21:05:54 +01:00

42 lines
1.0 KiB
Java

package cc.fascinated.bat.common;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
/**
* @author Fascinated (fascinated7)
*/
@Getter
public class ProfileHolder {
/**
* The profiles for the holder
*/
private Map<String, Profile> profiles;
/**
* Gets a profile for the holder
*
* @param clazz The class of the profile
* @param <T> The type of the profile
* @return The profile
*/
public <T extends Profile> T getProfile(Class<T> clazz) {
if (profiles == null) {
profiles = new HashMap<>();
}
Profile profile = profiles.values().stream().filter(p -> p.getClass().equals(clazz)).findFirst().orElse(null);
if (profile == null) {
try {
profile = (Profile) clazz.newInstance();
profiles.put(profile.getProfileKey(), profile);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
return clazz.cast(profile);
}
}