Package VASSAL.tools
Class UniqueIdManager
java.lang.Object
VASSAL.tools.UniqueIdManager
- All Implemented Interfaces:
ValidityChecker
public class UniqueIdManager extends Object implements ValidityChecker
A class for assigning unique identifiers to objects. Identifiers will be
of the form prefix#, where prefix is specified at initialization and the #
is an increasing digit. Components will have the same ID provided they
are loaded in the same order.
Unfortunately, this approach is flawed. If a module is edited, saved games
from previous versions can become broken. Worse, two players with different
extensions loaded could have incompatible behavior.
The preferred way to have unique identifiers is to allow the user to provide
names and use a
ValidityChecker
to ensure that the
names are unique. This class provides some support for using this approach
while providing backward compatibility with old saved games and modules.
Usage: an UniqueIdManager.Identifyable
instance invokes add(VASSAL.tools.UniqueIdManager.Identifyable)
, typically
during the Buildable.build(org.w3c.dom.Element)
method. Classes can use the
getIdentifier(VASSAL.tools.UniqueIdManager.Identifyable)
method to look up an identifier for that instance,
and can use findInstance(java.lang.String)
to look up a component by id.-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
UniqueIdManager.Identifyable
An object with an identifier that can be manipulated by aUniqueIdManager
-
Constructor Summary
Constructors Constructor Description UniqueIdManager(String prefix)
-
Method Summary
Modifier and Type Method Description void
add(UniqueIdManager.Identifyable i)
UniqueIdManager.Identifyable
findInstance(String id)
Return the first instance whose name or id matches the argumentIterator<UniqueIdManager.Identifyable>
getAllInstances()
static String
getIdentifier(UniqueIdManager.Identifyable target)
Make a best guess for a unique identifier for the target.void
remove(UniqueIdManager.Identifyable i)
void
validate(Buildable target, ValidationReport report)
Ensures that no other instance of the same class has the same name
-
Constructor Details
-
UniqueIdManager
-
-
Method Details
-
add
-
remove
-
getIdentifier
Make a best guess for a unique identifier for the target. Useif non-null, otherwise use {@link Identifyable#getId
- Parameters:
target
-- Returns:
-
getAllInstances
-
findInstance
Return the first instance whose name or id matches the argument- Parameters:
id
-- Returns:
-
validate
Ensures that no other instance of the same class has the same name- Specified by:
validate
in interfaceValidityChecker
-