com.threerings.presents.tools
Class ImportSet

java.lang.Object
  extended by com.threerings.presents.tools.ImportSet

public class ImportSet
extends Object

Manages a set of strings to be used as a set of imports. Provides useful functions for manipulating the set and sorts results.

Some methods in this class use a variable length String parameter 'replace'. This is a convenience for easily specifying multiple find/replace pairs. For example, to replace "Foo" with "Bar" and "123" with "ABC", a function can be called with the 4 arguments "Foo", "Bar", "123", "ABC" for the String... replace argument.

A few methods also use a "pattern" string parameter that is used to match a class name. This is a dumbed down regular expression (to avoid many \.) where "*" means .* and no other characters have special meaning. The pattern is also implicitly enclosed with ^$ so that the pattern must match the class name in its entirety. Callers will mostly use this to specify a prefix like "something*" or a suffix like "*something".


Field Summary
protected  HashSet<String> _imports
           
protected  List<String> _pushed
           
protected static Pattern _splitter
           
protected static List<String> IMPORT_GROUPS
           
 
Constructor Summary
ImportSet()
           
 
Method Summary
 void add(Class<?> clazz)
          Adds the given class' name to the set of imports.
 void add(String name)
          Adds the given name to the set of imports.
 void addAll(ImportSet other)
          Adds all the imports from another import set into this one.
 void addMunged(Class<?> clazz, String... replace)
          Adds a class' name to the imports but first performs the given list of search/replaces as described above.
 ImportSet clone()
           
 void duplicateAndMunge(String pattern, String... replace)
          Adds a new munged import for each existing import that matches a pattern.
protected static int findImportGroup(String imp)
           
protected static Pattern makePattern(String input)
          Create a real regular expression from the dumbed down input.
 void popIn()
          Re-adds the most recently popped import to the set.
 void pushOut(String pattern)
          Temporarily remove one import matching the given pattern.
 void remove(Class<?> clazz)
          Removes the name of a class from the imports.
 void remove(String name)
           
 int removeAll(String pattern)
          Remove all imports matching the given pattern.
 int removeArrays()
          Gets rid of array imports.
 void removeGlobals()
          Gets rid of primitive and java.lang imports.
 void removeSamePackage(String pkg)
          Remove all classes that are in the same package.
 void replace(Class<?> pattern, String replacement)
          Replace the import matchings a class name.
 void replace(String... replace)
          Replaces any import exactly each find string with the corresponding replace string.
 void swapInnerClassesForParents()
          Replaces inner class imports (those with a '$') with an import of the parent class.
 List<List<String>> toGroups()
          Converts the set of imports to groups of class names, according to conventional package ordering and spacing.
 List<String> toList()
          Convert the set of imports to a sorted list, ready to be output to a generated file.
 String toString()
           
 void translateClassArrays()
          Inserts imports for the non-primitive classes contained in all array imports.
 void translateInnerClasses()
          Replace all inner classes' separator characters ('$') with an underscore ('_') for use when generating ActionScript.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_imports

protected HashSet<String> _imports

_pushed

protected List<String> _pushed

_splitter

protected static Pattern _splitter

IMPORT_GROUPS

protected static List<String> IMPORT_GROUPS
Constructor Detail

ImportSet

public ImportSet()
Method Detail

add

public void add(Class<?> clazz)
Adds the given class' name to the set of imports.

Parameters:
clazz - the class to add

add

public void add(String name)
Adds the given name to the set of imports.

Parameters:
name - the name to add

addAll

public void addAll(ImportSet other)
Adds all the imports from another import set into this one.

Parameters:
other - the import set whose imports should be added

addMunged

public void addMunged(Class<?> clazz,
                      String... replace)
Adds a class' name to the imports but first performs the given list of search/replaces as described above.

Parameters:
clazz - the class whose name is munged and added
replace - array of pairs to search/replace on the name before adding

clone

public ImportSet clone()
Overrides:
clone in class Object

removeGlobals

public void removeGlobals()
Gets rid of primitive and java.lang imports.


removeArrays

public int removeArrays()
Gets rid of array imports.


removeSamePackage

public void removeSamePackage(String pkg)
Remove all classes that are in the same package.

Parameters:
pkg - package to remove

swapInnerClassesForParents

public void swapInnerClassesForParents()
Replaces inner class imports (those with a '$') with an import of the parent class.


translateInnerClasses

public void translateInnerClasses()
Replace all inner classes' separator characters ('$') with an underscore ('_') for use when generating ActionScript.


translateClassArrays

public void translateClassArrays()
Inserts imports for the non-primitive classes contained in all array imports.


pushOut

public void pushOut(String pattern)
Temporarily remove one import matching the given pattern. The most recently pushed pattern can be re-added using popIn. If there is no match, a null value is pushed so that popIn can still be called.

Parameters:
pattern - to match

popIn

public void popIn()
Re-adds the most recently popped import to the set. If a null value was pushed, does nothing.

Throws:
IndexOutOfBoundsException - if there is nothing to pop

remove

public void remove(String name)

remove

public void remove(Class<?> clazz)
Removes the name of a class from the imports.

Parameters:
clazz - the class whose name should be removed

replace

public void replace(String... replace)
Replaces any import exactly each find string with the corresponding replace string. See the description above.

Parameters:
replace - array of pairs for search/replace

replace

public void replace(Class<?> pattern,
                    String replacement)
Replace the import matchings a class name.

Parameters:
pattern - the class whose name to match
replacement - string to use instead

removeAll

public int removeAll(String pattern)
Remove all imports matching the given pattern.

Parameters:
pattern - the dumbed down regex to match (see description above)
Returns:
the number of imports removed

duplicateAndMunge

public void duplicateAndMunge(String pattern,
                              String... replace)
Adds a new munged import for each existing import that matches a pattern. The new entry is a copy of the old entry but modified according to the given find/replace pairs (see description above).

Parameters:
pattern - to qualify imports to duplicate
replace - pairs to find/replace on the new import

toGroups

public List<List<String>> toGroups()
Converts the set of imports to groups of class names, according to conventional package ordering and spacing. Within each group, sorting is alphabetical.


toList

public List<String> toList()
Convert the set of imports to a sorted list, ready to be output to a generated file.

Returns:
the sorted list of imports

toString

public String toString()
Overrides:
toString in class Object

makePattern

protected static Pattern makePattern(String input)
Create a real regular expression from the dumbed down input.

Parameters:
input - the dumbed down wildcard expression
Returns:
the calculated regular expression

findImportGroup

protected static int findImportGroup(String imp)