Class DotName
- All Implemented Interfaces:
Comparable<DotName>
DotName represents a dot separated name, typically a Java package or a Java class.
It has two possible variants. A simple wrapper based variant allows for fast construction
(it simply wraps the specified name string). Whereas, a componentized variant represents
one or more String components that, when combined with a dot character, assemble the
full name. The intention of the componentized variant is that the String components
can be reused to offer memory efficiency. This reuse is common in Java where packages
and classes follow a tree structure.
Both the simple and componentized variants are considered semantically equivalent if they
refer to the same logical name. More specifically, the equals and hashCode
methods return the same values for the same semantic name regardless of the variant used.
Which variant to use when depends on the specific performance and overhead objectives
of the specific use pattern.
Simple names are cheap to construct (just an additional wrapper object), so are ideal for
temporary use, like looking for an entry in a Map. Componentized names however require
that they be split in advance, and so require some additional time to construct. However, the memory
benefits of reusing component strings make them desirable when stored in a longer term area
such as in a Java data structure.
- Author:
- Jason T. Greene
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionintCompares aDotNameto anotherDotNameand returns whether thisDotNameis lesser than, greater than, or equal to the specified DotName.static DotNamecreateComponentized(DotName prefix, String localName) Constructs a componentizedDotName.static DotNamecreateComponentized(DotName prefix, String localName, boolean innerClass) Constructs a componentizedDotName.static DotNamecreateSimple(Class<?> clazz) Constructs a simpleDotNamewhich stores the name of given class in its entirety.static DotNamecreateSimple(String name) Constructs a simpleDotNamewhich stores the string in its entirety.booleanCompares aDotNameto anotherDotNameand returnstrueif they represent the same underlying semantic name.inthashCode()Returns a hash code which is based on the semantic representation of thisDotName.booleanReturns whether thisDotNameis a componentized variant.booleanisInner()Returns whether the local portion of a componentizedDotNameis separated by an inner class style delimiter ('$').local()Returns the local portion of thisDotName.Returns the package portion of thisDotName.Returns the package portion of thisDotName.prefix()Returns the parent prefix for thisDotNameornullif there is none.toString()Returns the regular binary class name.toString(char delim) Returns the regular binary class name wheredelimis used as a package separator.Returns the portion of thisDotNamethat does not contain a package prefix.
-
Field Details
-
OBJECT_NAME
-
ENUM_NAME
-
RECORD_NAME
-
STRING_NAME
-
-
Method Details
-
createSimple
Constructs a simpleDotNamewhich stores the string in its entirety. This variant is ideal for temporary usage, such as looking up an entry in aMapor an index.- Parameters:
name- a fully qualified name (with dots); must not benull- Returns:
- a simple
DotNamethat wraps givenname; nevernull
-
createSimple
Constructs a simpleDotNamewhich stores the name of given class in its entirety. This variant is ideal for temporary usage, such as looking up an entry in aMapor an index.This method is a shortcut for
DotName.createSimple(clazz.getName()).- Parameters:
clazz- a class whose fully qualified name is returned; must not benull- Returns:
- a simple
DotNamethat wraps the name of givenclazz; nevernull
-
createComponentized
Constructs a componentizedDotName. SuchDotNamerefers to a parent prefix (ornullif there is no further prefix) in addition to a local name that has no dot separator. The fully qualified name thisDotNamerepresents is constructed by recursing all parent prefixes and joining all local names with the'.'character.- Parameters:
prefix- anotherDotNamethat is the portion of the final name to the left oflocalName; may benullif there is no prefixlocalName- the local portion of this name; must not benulland must not contain'.'- Returns:
- a componentized
DotName; nevernull
-
createComponentized
Constructs a componentizedDotName. SuchDotNamerefers to a parent prefix (ornullif there is no further prefix) in addition to a local name that has no dot separator. The fully qualified name thisDotNamerepresents is constructed by recursing all parent prefixes and joining all local names with the'.'character.- Parameters:
prefix- anotherDotNamethat is the portion of the final name to the left oflocalName; may benullif there is no prefixlocalName- the local portion of this name; must not benulland must not contain'.'innerClass- whether thelocalNameis an inner class style name, which is joined to the prefix using'$'instead of'.'- Returns:
- a componentized
DotName; nevernull
-
prefix
Returns the parent prefix for thisDotNameornullif there is none. SimpleDotNamevariants never have a prefix.- Returns:
- the parent prefix for this
DotName; may benull
-
local
Returns the local portion of thisDotName. In simple variants, the entire fully qualified string is returned. In componentized variants, just the rightmost portion not including a separator (either'.'or'$') is returned.Use
withoutPackagePrefix()instead of this method if the desired value is the part of the string (including'$'signs if present) after the rightmost'.'delimiter.- Returns:
- the local portion of this
DotName; nevernull
-
withoutPackagePrefix
Returns the portion of thisDotNamethat does not contain a package prefix. In the case of an inner class syntax name, the'$'portion is included in the return value.- Returns:
- the portion of the fully qualified name that does not include a package name
- Since:
- 2.1.1
-
packagePrefix
Returns the package portion of thisDotName.- Returns:
- the package name or
nullif thisDotNamehas no package prefix - Since:
- 2.4
-
packagePrefixName
Returns the package portion of thisDotName. This is aDotName-returning variant ofpackagePrefix().- Returns:
- the package name or
nullif thisDotNamehas no package prefix - Since:
- 3.0
-
isComponentized
public boolean isComponentized()Returns whether thisDotNameis a componentized variant.- Returns:
trueif it is componentized,falseif it is a simpleDotName
-
isInner
public boolean isInner()Returns whether the local portion of a componentizedDotNameis separated by an inner class style delimiter ('$'). The result is undefined when thisDotNameis not componentized.This should not be used to test whether the name truly refers to an inner class, only that the dollar sign delimits the value. Java class names are allowed to contain
'$'signs, so the local value could simply be a fragment of a class name, and not an actual inner class. The correct way to determine whether a name refers to an actual inner class is to look up aClassInfoin the index and examine the nesting type like so:index.getClassByName(name).nestingType() != TOP_LEVEL;
- Returns:
trueif local is an inner class style delimited name,falseotherwise
-
toString
Returns the regular binary class name. -
toString
Returns the regular binary class name wheredelimis used as a package separator.- Parameters:
delim- the package separator; typically., but may be e.g./to construct a bytecode descriptor- Returns:
- the binary class name with given character used as a package separator
-
hashCode
public int hashCode()Returns a hash code which is based on the semantic representation of thisDotName.Whether a
DotNameis componentized has no impact on the calculated hash code. In other words, a componentizedDotNameand a simpleDotNamethat represent the same fully qualified name have the same hash code. -
equals
Compares aDotNameto anotherDotNameand returnstrueif they represent the same underlying semantic name. In other words, whether a name is componentized or simple has no bearing on the comparison. -
compareTo
Compares aDotNameto anotherDotNameand returns whether thisDotNameis lesser than, greater than, or equal to the specified DotName. If thisDotNameis lesser, a negative value is returned. If greater, a positive value is returned. If equal, zero is returned.- Specified by:
compareToin interfaceComparable<DotName>- Parameters:
other- theDotNameto compare to- Returns:
- a negative number if this is less than the specified object, a positive if greater, and zero if equal
- See Also:
-