it.unibo.alchemist.model.SupportedIncarnations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemist-implementationbase Show documentation
Show all versions of alchemist-implementationbase Show documentation
Abstract, incarnation independent implementations of the Alchemist's interfaces. Provides support for those who want to write incarnations.
/*
* Copyright (C) 2010-2023, Danilo Pianini and contributors
* listed, for each module, in the respective subproject's build.gradle.kts file.
*
* This file is part of Alchemist, and is distributed under the terms of the
* GNU General Public License, with a linking exception,
* as described in the file LICENSE in the Alchemist distribution's top directory.
*/
package it.unibo.alchemist.model;
import it.unibo.alchemist.util.ClassPathScanner;
import org.jooq.lambda.Unchecked;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* This enum interfaces the generic components of the graphical interface with
* the specific incarnation details.
*
*/
@SuppressWarnings("unchecked")
public final class SupportedIncarnations {
private static final Map>> INCARNATIONS;
static {
INCARNATIONS = ClassPathScanner.subTypesOf(
Incarnation.class,
"it.unibo.alchemist"
).stream()
.map(it -> (Class>) it)
.collect(Collectors.toMap(c -> preprocess(c.getSimpleName()), Function.identity()));
}
private SupportedIncarnations() {
}
/**
* @return The set of incarnations currently available.
*/
public static Set getAvailableIncarnations() {
return Collections.unmodifiableSet(INCARNATIONS.keySet());
}
/**
* Fetches an incarnation whose name matches the supplied string.
*
* @param s
* the name of the {@link Incarnation}
* @param
* {@link it.unibo.alchemist.model.Concentration} type
* @param
* {@link Position} type
*
* @return an {@link Optional} containing the incarnation, if one with a
* matching name exists
*/
public static > Optional> get(final String s) {
final String cmp = preprocess(s);
return Optional.ofNullable(INCARNATIONS.get(cmp))
.map(Unchecked.function(it -> (Incarnation) it.getDeclaredConstructor().newInstance()));
}
private static String preprocess(final String s) {
return s.toLowerCase(Locale.ENGLISH).replace("incarnation", "");
}
}