NameManager

public abstract class NameManager extends DataSetObservable
Known Direct Subclasses

Utility functions for handling variable and procedure names.

Nested Class Summary

class NameManager.ProcedureNameManager The NameManager for procedure names. 
class NameManager.VariableNameManager The NameManager for variable names. 

Field Summary

protected final mCanonicalMap
protected final SortedSet<String> mDisplayNamesSorted

Inherited Field Summary

Public Constructor Summary

Public Method Summary

void
addName(String name)
Adds the name to the list of used names.
void
clear()
Clear the list of used names.
boolean
contains(String name)
abstract String
generateExternalName(Set<String> reservedWords, String baseName)
Convert a Blockly entity name to a legal exportable entity name.
String
generateUniqueName(String name, boolean addName)
Generates a name that is unique within the scope of the current NameManager, based on the input name.
String
getExisting(String name)
Returns the existing name, if the name will map to the same canonical form.
SortedSet<String>
boolean
isValidName(String name)
String
makeValidName(String name, String fallbackName)
boolean
remove(String toRemove)
Remove a single name from the list of used names.
int
size()

Protected Method Summary

String
makeCanonical(String name)

Inherited Method Summary

Fields

protected final mCanonicalMap

protected final SortedSet<String> mDisplayNamesSorted

Public Constructors

public NameManager ()

Public Methods

public void addName (String name)

Adds the name to the list of used names. Does not check if the name is already there.

Parameters
name The name to add.
Throws
IllegalArgumentException If the name is not valid.

public void clear ()

Clear the list of used names.

public boolean contains (String name)

Parameters
name The string to look up.
Returns
  • True if name's lowercase equivalent is in the list.

public abstract String generateExternalName (Set<String> reservedWords, String baseName)

Convert a Blockly entity name to a legal exportable entity name. Ensure that this is a new name not overlapping any previously defined name. Also check against list of reserved words for the current language and ensure name doesn't collide. The new name will conform to the [_A-Za-z][_A-Za-z0-9]* format that most languages consider legal for variables.

Parameters
reservedWords Reserved words in the target language.
baseName The name to convert.
Returns
  • A legal variable or procedure name in the target language.

public String generateUniqueName (String name, boolean addName)

Generates a name that is unique within the scope of the current NameManager, based on the input name. If the base name was unique, returns it directly.

Parameters
name The name upon which to base the unique name.
addName Whether to add the generated name to the used names list.
Returns
  • A unique name.

public String getExisting (String name)

Returns the existing name, if the name will map to the same canonical form. Otherwise, return the proposedName.

Parameters
name The proposed name.
Returns
  • A previous added name that shares the same canonical form. Otherwise return null.

public SortedSet<String> getUsedNames ()

Returns
  • An alphabetically sorted list of all of the names that have already been used. This list is not modifiable, but is backed by the real list and will stay updated.

public boolean isValidName (String name)

Parameters
name

public String makeValidName (String name, String fallbackName)

Parameters
name
fallbackName

public boolean remove (String toRemove)

Remove a single name from the list of used names.

Parameters
toRemove The name to remove.

public int size ()

Returns
  • The number of names that have been used.

Protected Methods

protected String makeCanonical (String name)

Parameters
name The proposed name
Returns
  • The canonical string for the provided name, as used by mCanonicalMap.