
net.alantea.viewml.internal.Referencer Maven / Gradle / Ivy
package net.alantea.viewml.internal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import net.alantea.viewml.VmlException;
import net.alantea.viewml.handlers.TransparentHandler;
/**
* The Class Referencer.
*/
public final class Referencer
{
/** The reference map. */
// Reference named objects
private static Map referenceMap = new HashMap<>();
/** The references. */
private static LinkedList references = new LinkedList<>();
/**
* Instantiates a new referencer.
*/
private Referencer()
{
}
/**
* Put a reference.
*
* @param name the name
* @param newReference the object
* @throws VmlException the TXML exception
*/
public static void putReference(String name, Object newReference) throws VmlException
{
Object object = null;
if (newReference != null)
{
object = newReference;
if (newReference instanceof TransparentHandler)
{
object = ((TransparentHandler)newReference).getComponent();
}
}
if (object != null)
{
referenceMap.put(name, object);
ActionsLoader.addActionsFor(name, object);
ChangesLoader.addChangesFor(name, object);
}
}
/**
* Get a reference.
*
* @param name the name
* @return the reference
* @throws VmlException the TXML exception
*/
public static Object getReference(String name) throws VmlException
{
return referenceMap.get(name);
}
/**
* Get a reference name.
*
* @param target the target
* @return the reference
* @throws VmlException the TXML exception
*/
public static String getName(Object target) throws VmlException
{
for (Entry entry : referenceMap.entrySet())
{
if (Objects.equals(target, entry.getValue()))
{
return entry.getKey();
}
}
throw new VmlException("Key not found");
}
/**
* Get all reference keys.
*
* @return the references
* @throws VmlException the TXML exception
*/
public static Set getReferences() throws VmlException
{
return referenceMap.keySet();
}
/**
* Push reference.
*
* @param reference the reference
*/
public static void pushReference(String reference)
{
if (reference == null)
{
references.push(references.peek());
}
else
{
references.push(reference);
}
}
/**
* Pop reference.
*/
public static void popReference()
{
references.pop();
}
/**
* Peek reference.
*
* @return the string
*/
public static String peekReference()
{
String ret = references.peek();
return (ret == null) ? "" : ret;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy