use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.
the class FactoryGraphicAwt method saveImage.
@Override
public void saveImage(ImageBuffer image, Media media) {
Check.notNull(media);
final BufferedImage surface = image.getSurface();
try (OutputStream output = media.getOutputStream()) {
ToolsAwt.saveImage(surface, output);
} catch (final IOException exception) {
throw new LionEngineException(exception, media, ERROR_IMAGE_SAVE);
}
}
use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.
the class Factory method create.
/**
* Create a {@link Featurable} from its {@link Media} using a generic way. The concerned class to instantiate and
* its constructor must be public, and can have the following parameter: ({@link Setup}).
* <p>
* Automatically add {@link IdentifiableModel} if feature does not have {@link Identifiable} feature.
* </p>
* <p>
* Destroyed {@link Featurable} can be cached to avoid {@link Featurable} creation if has {@link Recycler} and
* {@link Recyclable} {@link Feature}s. If cache associated to media is available, it is
* {@link Recyclable#recycle()} and then returned.
* </p>
*
* @param <O> The featurable type.
* @param media The featurable media.
* @return The featurable instance.
* @throws LionEngineException If {@link Media} is <code>null</code> or {@link Setup} not found.
*/
@SuppressWarnings("unchecked")
public <O extends Featurable> O create(Media media) {
if (cache.containsKey(media) && !cache.get(media).isEmpty()) {
final Featurable featurable = cache.get(media).poll();
featurable.getFeature(Recycler.class).recycle();
return (O) featurable;
}
final Setup setup = getSetup(media);
final Class<O> type = setup.getConfigClass(classLoader);
try {
return createFeaturable(type, setup);
} catch (final NoSuchMethodException exception) {
throw new LionEngineException(exception, ERROR_CONSTRUCTOR_MISSING + media);
}
}
use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.
the class Factory method create.
/**
* Create a featurable from its {@link Media} using a generic way. The concerned class to instantiate and its
* constructor must be public, and can have the following parameter: ({@link Setup}).
* <p>
* Automatically add {@link IdentifiableModel} if feature does not have {@link Identifiable} feature.
* </p>
* <p>
* Destroyed {@link Featurable} can be cached to avoid {@link Featurable} creation if has {@link Recycler} and
* {@link Recyclable} {@link Feature}s. If cache associated to media is available, it is
* {@link Recyclable#recycle()} and then returned.
* </p>
*
* @param <O> The featurable type.
* @param media The featurable media.
* @param type The specific class to use (override the one in the media).
* @return The featurable instance.
* @throws LionEngineException If {@link Media} is <code>null</code> or {@link Setup} not found.
*/
@SuppressWarnings("unchecked")
public <O extends Featurable> O create(Media media, Class<O> type) {
if (cache.containsKey(media) && !cache.get(media).isEmpty()) {
final Featurable featurable = cache.get(media).poll();
featurable.getFeature(Recycler.class).recycle();
return (O) featurable;
}
final Setup setup = getSetup(media, true);
try {
return createFeaturable(type, setup);
} catch (final NoSuchMethodException exception) {
throw new LionEngineException(exception, ERROR_CONSTRUCTOR_MISSING + media);
}
}
use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.
the class FeaturableConfig method getClass.
/**
* Get the class reference from its name using cache.
*
* @param <T> The class type.
* @param loader The class loader reference.
* @param className The class name.
* @return The typed class instance.
* @throws LionEngineException If invalid class.
*/
@SuppressWarnings("unchecked")
private static <T> Class<T> getClass(ClassLoader loader, String className) {
if (CLASS_CACHE.containsKey(className)) {
return (Class<T>) CLASS_CACHE.get(className);
}
try {
final Class<?> clazz = loader.loadClass(className);
CLASS_CACHE.put(className, clazz);
return (Class<T>) clazz;
} catch (final ClassNotFoundException exception) {
throw new LionEngineException(exception, ERROR_CLASS_PRESENCE + className);
}
}
use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.
the class FeaturableConfig method getFeatures.
/**
* Get all available features.
* Default constructor of each feature must be available or with {@link Setup} as single parameter.
*
* @param loader The class loader reference.
* @param services The services reference.
* @param setup The setup reference.
* @param filter The type filter (can be <code>null</code> if none).
* @return The available features.
* @throws LionEngineException If invalid class.
*/
public static List<Feature> getFeatures(ClassLoader loader, Services services, Setup setup, Class<?> filter) {
final Collection<XmlReader> children;
final XmlReader root = setup.getRoot();
if (root.hasNode(NODE_FEATURES)) {
children = setup.getRoot().getChild(FeaturableConfig.NODE_FEATURES).getChildren(FeaturableConfig.NODE_FEATURE);
} else {
children = Collections.emptyList();
}
final List<Feature> features = new ArrayList<>(children.size());
for (final XmlReader featureNode : children) {
final String className = featureNode.getText();
try {
final Class<? extends Feature> clazz = getClass(loader, className);
if (filter == null || filter.isAssignableFrom(clazz)) {
final Feature feature = UtilReflection.createReduce(clazz, services, setup);
features.add(feature);
}
} catch (final NoSuchMethodException | LionEngineException exception) {
throw new LionEngineException(exception, setup.getMedia());
}
}
children.clear();
return features;
}
Aggregations