2024-06-25 12:36:40 +01:00
|
|
|
package cc.fascinated.bat.common;
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
2024-06-26 16:17:57 +01:00
|
|
|
import java.io.Serializable;
|
2024-06-25 12:36:40 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
|
|
|
@Getter
|
2024-06-26 16:17:57 +01:00
|
|
|
public class ProfileHolder implements Serializable {
|
2024-06-25 12:36:40 +01:00
|
|
|
/**
|
|
|
|
* 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<?> 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 (T) profile;
|
|
|
|
}
|
|
|
|
}
|