// SPDX-FileCopyrightText: 2020 Roberto Posenato // // SPDX-License-Identifier: LGPL-3.0-or-later package it.univr.di.labeledvalue; import com.google.common.base.Supplier; import java.lang.reflect.InvocationTargetException; import java.util.logging.Logger; /** * Basic factory of LabeledIntMap objects. An implementation C must be provided. * * @param implementation class of LabeledIntMap interface. * * @author posenato * @version $Rev$ */ public final class LabeledIntMapSupplier implements Supplier { /** * */ static final public Class DEFAULT_LABELEDINTMAP_CLASS = LabeledIntTreeMap.class; /** * */ @SuppressWarnings("StaticMethodOnlyUsedInOneClass") static final public Class SIMPLE_LABELEDINTMAP_CLASS = LabeledIntTreeSimpleMap.class; //LabeledIntTreeSimpleMap.class; /** * class logger */ @SuppressWarnings("unused") static private final Logger LOG = Logger.getLogger(LabeledIntMapSupplier.class.getCanonicalName()); /** * */ private final Class generatorClass; /** * */ private final C generator; /** * Constructor for LabeledIntMapSupplier. * * @param implementationClass a {@link java.lang.Class} object. */ public LabeledIntMapSupplier(Class implementationClass) { generatorClass = implementationClass; try { generator = implementationClass.getDeclaredConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { LOG.severe(e.getMessage()); throw new IllegalStateException(e.getMessage()); } } @Override public C get() { return generatorClass.cast(generator.newInstance()); } /** * @param optimize false if the labels have not to shorten. * * @return a new instance */ public C get(boolean optimize) { return generatorClass.cast(generator.newInstance(optimize)); } /** * @param lim a {@link it.univr.di.labeledvalue.LabeledIntMap} object. * * @return a new LabeledIntMap object that is an independent copy of 'lim'. */ public C get(LabeledIntMap lim) { return generatorClass.cast(generator.newInstance(lim)); } /** * @param lim a {@link it.univr.di.labeledvalue.LabeledIntMap} object. * @param optimize false if the labels have not to shorten. * * @return a new instance */ public C get(LabeledIntMap lim, boolean optimize) { return generatorClass.cast(generator.newInstance(lim, optimize)); } /** * @return a {@link java.lang.Class} object. */ public Class getReturnedObjectClass() { return generatorClass; } @Override public String toString() { return generatorClass.getSimpleName(); } }