net.sf.rej
Class Imports

java.lang.Object
  extended by net.sf.rej.Imports

public class Imports
extends java.lang.Object

Handles creation of import statements (for improved readability) and the conversion between FQNs and short names of types, taking in consideration naming conflicts. This is a purely logical class used by the user interface classes to guide in the presentation of import statements and full/short names of types.

Author:
Sami Koivu

Constructor Summary
Imports(java.lang.String homePackage)
          Initializes this Imports object setting homePackage as the home of this object.
 
Method Summary
 void addType(java.lang.String fullTypeName)
          Adds the FQN given as parameter to this import set.
static boolean areInSamePackage(java.lang.String type1, java.lang.String type2)
          Return a boolean value indicating whether or not the two given fully qualified names are in th same package or not.
 java.util.Set<java.lang.String> getImports()
          Returns a list of import statements for this imports object.
static java.lang.String getPackage(java.lang.String type)
          Returns the package portion of the given FQN or an empty String if the FQN contains no package definition.
 java.lang.String getShortName(java.lang.String fqn)
          Returns the "short name" for the type.
static java.lang.String getType(java.lang.String type)
          Returns the short name of the given FQN.
static boolean isInDefaultPackage(java.lang.String type)
          Returns a boolean value indicating whether or not the given FQN of a type is in the default package (in other words, has no package definition).
static boolean isInJavaLang(java.lang.String type)
          Returns a boolean value indicating whether or not the given FQN of a type is in the java.lang package or not.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Imports

public Imports(java.lang.String homePackage)
Initializes this Imports object setting homePackage as the home of this object. No import statements are created for types in the home package.

Parameters:
homePackage - the home package, for example: "com.mycompany.myproject"
Method Detail

addType

public void addType(java.lang.String fullTypeName)
Adds the FQN given as parameter to this import set.

Parameters:
fullTypeName - the fully qualified name of the type to add to this import object.

getImports

public java.util.Set<java.lang.String> getImports()
Returns a list of import statements for this imports object. The required import statements are given based on the types added to this object and it's home package and keeping in mind that types in the java.lang package or the default package do not require import statements.

Returns:
a set of FQNs for types that require import statements.

getPackage

public static java.lang.String getPackage(java.lang.String type)
Returns the package portion of the given FQN or an empty String if the FQN contains no package definition.

Parameters:
type - a FQN of a class.
Returns:
the package of the given FQN.

getType

public static java.lang.String getType(java.lang.String type)
Returns the short name of the given FQN. If the FQN constains no package definition, the FQN is returned as-is.

Parameters:
type - a FQN of a type.
Returns:
the short name for the type given as parameter.

isInDefaultPackage

public static boolean isInDefaultPackage(java.lang.String type)
Returns a boolean value indicating whether or not the given FQN of a type is in the default package (in other words, has no package definition).

Parameters:
type - a fully qualified name of a type.
Returns:
true if the type is in the default package.

isInJavaLang

public static boolean isInJavaLang(java.lang.String type)
Returns a boolean value indicating whether or not the given FQN of a type is in the java.lang package or not.

Parameters:
type - a fully qualified name of a type.
Returns:
true if the type is in the java.lang package.

areInSamePackage

public static boolean areInSamePackage(java.lang.String type1,
                                       java.lang.String type2)
Return a boolean value indicating whether or not the two given fully qualified names are in th same package or not.

Parameters:
type1 - the first type to compare.
type2 - the second type to compare.
Returns:
true if the two FQNs are in the same package (for example, java.lang.Object and java.lang.String).

getShortName

public java.lang.String getShortName(java.lang.String fqn)
Returns the "short name" for the type. The concept of short name here means the FQN without the package part. In other words, if the FQN is java.lang.String the short name would be String. If the import object judges that shortening the name would be risking ambiguity, the full name is returned instead.

Parameters:
fqn - the FQN of the class to be shortened.
Returns:
the short name.