APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.text

public Class PlainView

extends View
implements TabExpander
Class Inheritance
All Implemented Interfaces
javax.swing.text.TabExpander
Known Direct Subclasses
javax.swing.text.FieldView
Imports
java.awt.*, java.awt.font.FontRenderContext, java.awt.geom.Rectangle2D, java.util.Objects, .HashMap, javax.swing.event.*, java.lang.ref.SoftReference

Implements View interface for a simple multi-line text view that has text in one font and color. The view represents each child element as a line of text.
Author
Timothy Prinzing
See Also
View

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
private static class

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv final boolean
pack-priv final boolean
pack-priv int
firstLineOffset

Offset of where to draw the first character on the first line.

pack-priv Font
font

Font used to calculate the longest line...

pack-priv Segment
pack-priv Element
longLine

The current longest line.

private static SoftReference<HashMap<PlainView.FPMethodItem, Boolean>>
protected FontMetrics
metrics

Font metrics for the current font.

pack-priv int
pack-priv int
pack-priv Color
pack-priv int
pack-priv float
pack-priv Color
pack-priv final boolean
Inherited from javax.swing.text.View:
BadBreakWeightExcellentBreakWeightfirstUpdateIndexForcedBreakWeightGoodBreakWeightlastUpdateIndexsharedBiasReturnX_AXISY_AXIS

Constructor Summary

AccessConstructor and Description
public
PlainView(Element
the element
elem
)

Constructs a new PlainView wrapped on an element.

Method Summary

Modifier and TypeMethod and Description
pack-priv Shape
adjustPaintRegion(Shape a)

Should return a shape ideal for painting based on the passed in Shape a.

private void
calculateLongestLine()

Iterate over the lines represented by the child elements of the element this view represents, looking for the line that is the longest.

pack-priv float
callDrawSelectedText(Graphics g, float x, float y, int p0, int p1)

private float
callDrawUnselectedText(Graphics g, float x, float y, int p0, int p1)

public void
changedUpdate(DocumentEvent
the change information from the associated document
changes
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.changedUpdate.

Gives notification from the document that attributes were changed in a location that this view is responsible for.

private static boolean
checkFPMethodOverridden(final Class<?> className, final String methodName, final PlainView.FPMethodArgs methodArgs)

protected void
damageLineRange(int
the starting line number to repaint. This must be a valid line number in the model.
line0
,
int
the ending line number to repaint. This must be a valid line number in the model.
line1
,
Shape
the region allocated for the view to render into
a
,
Component
the component hosting the view (used to call repaint)
host
)

Repaint the given line range.

private float
drawElement(int lineIndex, Element elem, Graphics g, float x, float y)

protected void
drawLine(int
the line to draw >= 0
lineIndex
,
Graphics
the Graphics context
g
,
int
the starting X position >= 0
x
,
int
the starting Y position >= 0
y
)
Deprecated since 9. replaced by drawLine(int, Graphics2D, float, float)

Renders a line of text, suppressing whitespace at the end and expanding any tabs.

protected void
drawLine(int
the line to draw >= 0
lineIndex
,
Graphics2D
the Graphics context
g
,
float
the starting X position >= 0
x
,
float
the starting Y position >= 0
y
)

Renders a line of text, suppressing whitespace at the end and expanding any tabs.

private void
drawLineImpl(int lineIndex, Graphics g, float x, float y)

protected int

Returns:

the location of the end of the range
drawSelectedText
(Graphics
the graphics context
g
,
int
the starting X coordinate >= 0
x
,
int
the starting Y coordinate >= 0
y
,
int
the beginning position in the model >= 0
p0
,
int
the ending position in the model >= 0
p1
)

Renders the given range in the model as selected text.

protected float

Returns:

the location of the end of the range
drawSelectedText
(Graphics2D
the graphics context
g
,
float
the starting X coordinate >= 0
x
,
float
the starting Y coordinate >= 0
y
,
int
the beginning position in the model >= 0
p0
,
int
the ending position in the model >= 0
p1
)

Renders the given range in the model as selected text.

private float
drawSelectedTextImpl(Graphics g, float x, float y, int p0, int p1, boolean useFPAPI)

protected int

Returns:

the X location of the end of the range >= 0
drawUnselectedText
(Graphics
the graphics context
g
,
int
the starting X coordinate >= 0
x
,
int
the starting Y coordinate >= 0
y
,
int
the beginning position in the model >= 0
p0
,
int
the ending position in the model >= 0
p1
)

Renders the given range in the model as normal unselected text.

protected float

Returns:

the X location of the end of the range >= 0
drawUnselectedText
(Graphics2D
the graphics context
g
,
float
the starting X coordinate >= 0
x
,
float
the starting Y coordinate >= 0
y
,
int
the beginning position in the model >= 0
p0
,
int
the ending position in the model >= 0
p1
)

Renders the given range in the model as normal unselected text.

private float
drawUnselectedTextImpl(Graphics g, float x, float y, int p0, int p1, boolean useFPAPI)

pack-priv static boolean
protected final Segment

Returns:

the buffer
getLineBuffer
()

Gives access to a buffer that can be used to fetch text from the associated document.

private int
getLineWidth(Element line)

Calculate the width of the line represented by the given element.

public float

Returns:

the span the view would like to be rendered into >= 0. Typically the view is told to render into the span that is returned, although there is no guarantee. The parent may choose to resize or break the view.
getPreferredSpan
(int
may be either View.X_AXIS or View.Y_AXIS
axis
)

Implements abstract javax.swing.text.View.getPreferredSpan.

Determines the preferred span for this view along an axis.

protected int

Returns:

the tab size
getTabSize
()

Returns the tab size set for the document, defaulting to 8.

public void
insertUpdate(DocumentEvent
the change information from the associated document
changes
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.insertUpdate.

Gives notification that something was inserted into the document in a location that this view is responsible for.

private static boolean
isFPMethodOverridden(String method, Class<?> cls, Class<?>[] intTypes, Class<?>[] fpTypes)

protected Rectangle

Returns:

the rectangle that represents the given line
lineToRect
(Shape
the region allocated for the view to render into
a
,
int
the line number to find the region of. This must be a valid line number in the model.
line
)

Determine the rectangle that represents the given line.

public Shape

Returns:

the bounding box of the given position
modelToView
(int
the position to convert >= 0
pos
,
Shape
the allocated region to render into
a
,
Position.Bias b)

Implements abstract javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

public float

Returns:

the tab stop, measured in points >= 0
nextTabStop
(float
the current position >= 0
x
,
int
the position within the text stream that the tab occurred at >= 0.
tabOffset
)

Implements javax.swing.text.TabExpander.nextTabStop.

Returns the next tab stop position after a given reference position.

public void
paint(Graphics
the rendering surface to use
g
,
Shape
the allocated region to render into
a
)

Implements abstract javax.swing.text.View.paint.

Renders using the given rendering surface and area on that surface.

public void
removeUpdate(DocumentEvent
the change information from the associated document
changes
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.removeUpdate.

Gives notification that something was removed from the document in a location that this view is responsible for.

public void
setSize(float
the width >= 0
width
,
float
the height >= 0
height
)

Overrides javax.swing.text.View.setSize.

Sets the size of the view.

protected void
updateDamage(DocumentEvent
the change information from the associated document
changes
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Repaint the region of change covered by the given document event.

protected void
updateMetrics()

Checks to see if the font metrics and longest line are up-to-date.

public int

Returns:

the location within the model that best represents the given point in the view >= 0
viewToModel
(float
the X coordinate >= 0
x
,
float
the Y coordinate >= 0
y
,
Shape
the allocated region to render into
a
,
Position.Bias[] bias)

Implements abstract javax.swing.text.View.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Inherited from javax.swing.text.View:
appendbreakViewcalculateUpdateIndexescreateFragmentforwardUpdateforwardUpdateToViewgetAlignmentgetAttributesgetBreakWeightgetChildAllocationgetContainergetDocumentgetElementgetEndOffsetgetGraphicsgetMaximumSpangetMinimumSpangetNextVisualPositionFromgetParentgetResizeWeightgetStartOffsetgetToolTipTextgetViewgetViewCountgetViewFactorygetViewIndexgetViewIndexinsertisVisiblemodelToViewmodelToViewpreferenceChangedremoveremoveAllreplacesetParentupdateAfterChangeupdateChildrenupdateLayoutviewToModel

Field Detail

drawLineOverriddenback to summary
pack-priv final boolean drawLineOverridden
drawSelectedTextOverriddenback to summary
pack-priv final boolean drawSelectedTextOverridden
drawUnselectedTextOverriddenback to summary
pack-priv final boolean drawUnselectedTextOverridden
firstLineOffsetback to summary
pack-priv int firstLineOffset

Offset of where to draw the first character on the first line. This is a hack and temporary until we can better address the problem of text measuring. This field is actually never set directly in PlainView, but by FieldView.

fontback to summary
pack-priv Font font

Font used to calculate the longest line... if this changes we need to recalculate the longest line

lineBufferback to summary
pack-priv Segment lineBuffer
longLineback to summary
pack-priv Element longLine

The current longest line. This is used to calculate the preferred width of the view. Since the calculation is potentially expensive we try to avoid it by stashing which line is currently the longest.

methodsOverriddenMapRefback to summary
private static SoftReference<HashMap<PlainView.FPMethodItem, Boolean>> methodsOverriddenMapRef
metricsback to summary
protected FontMetrics metrics

Font metrics for the current font.

sel0back to summary
pack-priv int sel0
sel1back to summary
pack-priv int sel1
selectedback to summary
pack-priv Color selected
tabBaseback to summary
pack-priv int tabBase
tabSizeback to summary
pack-priv float tabSize
unselectedback to summary
pack-priv Color unselected
useFloatingPointAPIback to summary
pack-priv final boolean useFloatingPointAPI

Constructor Detail

PlainViewback to summary
public PlainView(Element elem)

Constructs a new PlainView wrapped on an element.

Parameters
elem:Element

the element

Method Detail

adjustPaintRegionback to summary
pack-priv Shape adjustPaintRegion(Shape a)

Should return a shape ideal for painting based on the passed in Shape a. This is useful if painting in a different region. The default implementation returns a.

calculateLongestLineback to summary
private void calculateLongestLine()

Iterate over the lines represented by the child elements of the element this view represents, looking for the line that is the longest. The longLine variable is updated to represent the longest line contained. The font variable is updated to indicate the font used to calculate the longest line.

callDrawSelectedTextback to summary
pack-priv float callDrawSelectedText(Graphics g, float x, float y, int p0, int p1) throws BadLocationException
callDrawUnselectedTextback to summary
private float callDrawUnselectedText(Graphics g, float x, float y, int p0, int p1) throws BadLocationException
changedUpdateback to summary
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f)

Overrides javax.swing.text.View.changedUpdate.

Gives notification from the document that attributes were changed in a location that this view is responsible for.

Parameters
changes:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#changedUpdate
checkFPMethodOverriddenback to summary
private static boolean checkFPMethodOverridden(final Class<?> className, final String methodName, final PlainView.FPMethodArgs methodArgs)
damageLineRangeback to summary
protected void damageLineRange(int line0, int line1, Shape a, Component host)

Repaint the given line range.

Parameters
line0:int

the starting line number to repaint. This must be a valid line number in the model.

line1:int

the ending line number to repaint. This must be a valid line number in the model.

a:Shape

the region allocated for the view to render into

host:Component

the component hosting the view (used to call repaint)

Since
1.4
drawElementback to summary
private float drawElement(int lineIndex, Element elem, Graphics g, float x, float y) throws BadLocationException
drawLineback to summary
protected void drawLine(int lineIndex, Graphics g, int x, int y)

Deprecated

since 9.

replaced by drawLine(int, Graphics2D, float, float)

Renders a line of text, suppressing whitespace at the end and expanding any tabs. This is implemented to make calls to the methods drawUnselectedText and drawSelectedText so that the way selected and unselected text are rendered can be customized.

Parameters
lineIndex:int

the line to draw >= 0

g:Graphics

the Graphics context

x:int

the starting X position >= 0

y:int

the starting Y position >= 0

Annotations
@Deprecated
since:9
See Also
drawUnselectedText, drawSelectedText
drawLineback to summary
protected void drawLine(int lineIndex, Graphics2D g, float x, float y)

Renders a line of text, suppressing whitespace at the end and expanding any tabs. This is implemented to make calls to the methods drawUnselectedText and drawSelectedText so that the way selected and unselected text are rendered can be customized.

Parameters
lineIndex:int

the line to draw >= 0

g:Graphics2D

the Graphics context

x:float

the starting X position >= 0

y:float

the starting Y position >= 0

Since
9
See Also
drawUnselectedText, drawSelectedText
drawLineImplback to summary
private void drawLineImpl(int lineIndex, Graphics g, float x, float y)
drawSelectedTextback to summary
protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException

Renders the given range in the model as selected text. This is implemented to render the text in the color specified in the hosting component. It assumes the highlighter will render the selected background.

Parameters
g:Graphics

the graphics context

x:int

the starting X coordinate >= 0

y:int

the starting Y coordinate >= 0

p0:int

the beginning position in the model >= 0

p1:int

the ending position in the model >= 0

Returns:int

the location of the end of the range

Annotations
@Deprecated
since:9
Exceptions
BadLocationException:
if the range is invalid
drawSelectedTextback to summary
protected float drawSelectedText(Graphics2D g, float x, float y, int p0, int p1) throws BadLocationException

Renders the given range in the model as selected text. This is implemented to render the text in the color specified in the hosting component. It assumes the highlighter will render the selected background.

Parameters
g:Graphics2D

the graphics context

x:float

the starting X coordinate >= 0

y:float

the starting Y coordinate >= 0

p0:int

the beginning position in the model >= 0

p1:int

the ending position in the model >= 0

Returns:float

the location of the end of the range

Exceptions
BadLocationException:
if the range is invalid
Since
9
drawSelectedTextImplback to summary
private float drawSelectedTextImpl(Graphics g, float x, float y, int p0, int p1, boolean useFPAPI) throws BadLocationException
drawUnselectedTextback to summary
protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException

Renders the given range in the model as normal unselected text. Uses the foreground or disabled color to render the text.

Parameters
g:Graphics

the graphics context

x:int

the starting X coordinate >= 0

y:int

the starting Y coordinate >= 0

p0:int

the beginning position in the model >= 0

p1:int

the ending position in the model >= 0

Returns:int

the X location of the end of the range >= 0

Annotations
@Deprecated
since:9
Exceptions
BadLocationException:
if the range is invalid
drawUnselectedTextback to summary
protected float drawUnselectedText(Graphics2D g, float x, float y, int p0, int p1) throws BadLocationException

Renders the given range in the model as normal unselected text. Uses the foreground or disabled color to render the text.

Parameters
g:Graphics2D

the graphics context

x:float

the starting X coordinate >= 0

y:float

the starting Y coordinate >= 0

p0:int

the beginning position in the model >= 0

p1:int

the ending position in the model >= 0

Returns:float

the X location of the end of the range >= 0

Exceptions
BadLocationException:
if the range is invalid
Since
9
drawUnselectedTextImplback to summary
private float drawUnselectedTextImpl(Graphics g, float x, float y, int p0, int p1, boolean useFPAPI) throws BadLocationException
getFPMethodOverriddenback to summary
pack-priv static boolean getFPMethodOverridden(Class<?> cls, String method, PlainView.FPMethodArgs methodArgs)
getLineBufferback to summary
protected final Segment getLineBuffer()

Gives access to a buffer that can be used to fetch text from the associated document.

Returns:Segment

the buffer

getLineWidthback to summary
private int getLineWidth(Element line)

Calculate the width of the line represented by the given element. It is assumed that the font and font metrics are up-to-date.

Annotations
@SuppressWarnings:deprecation
getPreferredSpanback to summary
public float getPreferredSpan(int axis)

Implements abstract javax.swing.text.View.getPreferredSpan.

Determines the preferred span for this view along an axis.

Parameters
axis:int

may be either View.X_AXIS or View.Y_AXIS

Returns:float

the span the view would like to be rendered into >= 0. Typically the view is told to render into the span that is returned, although there is no guarantee. The parent may choose to resize or break the view.

Exceptions
IllegalArgumentException:
for an invalid axis
getTabSizeback to summary
protected int getTabSize()

Returns the tab size set for the document, defaulting to 8.

Returns:int

the tab size

insertUpdateback to summary
public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f)

Overrides javax.swing.text.View.insertUpdate.

Gives notification that something was inserted into the document in a location that this view is responsible for.

Parameters
changes:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#insertUpdate
isFPMethodOverriddenback to summary
private static boolean isFPMethodOverridden(String method, Class<?> cls, Class<?>[] intTypes, Class<?>[] fpTypes)
lineToRectback to summary
protected Rectangle lineToRect(Shape a, int line)

Determine the rectangle that represents the given line.

Parameters
a:Shape

the region allocated for the view to render into

line:int

the line number to find the region of. This must be a valid line number in the model.

Returns:Rectangle

the rectangle that represents the given line

Since
1.4
modelToViewback to summary
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException

Implements abstract javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

Parameters
pos:int

the position to convert >= 0

a:Shape

the allocated region to render into

Returns:Shape

the bounding box of the given position

Annotations
@SuppressWarnings:deprecation
Exceptions
BadLocationException:
if the given position does not represent a valid location in the associated document
See Also
View#modelToView
nextTabStopback to summary
public float nextTabStop(float x, int tabOffset)

Implements javax.swing.text.TabExpander.nextTabStop.

Returns the next tab stop position after a given reference position. This implementation does not support things like centering so it ignores the tabOffset argument.

Parameters
x:float

the current position >= 0

tabOffset:int

the position within the text stream that the tab occurred at >= 0.

Returns:float

the tab stop, measured in points >= 0

paintback to summary
public void paint(Graphics g, Shape a)

Implements abstract javax.swing.text.View.paint.

Renders using the given rendering surface and area on that surface. The view may need to do layout and create child views to enable itself to render into the given allocation.

Parameters
g:Graphics

the rendering surface to use

a:Shape

the allocated region to render into

See Also
View#paint
removeUpdateback to summary
public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f)

Overrides javax.swing.text.View.removeUpdate.

Gives notification that something was removed from the document in a location that this view is responsible for.

Parameters
changes:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#removeUpdate
setSizeback to summary
public void setSize(float width, float height)

Overrides javax.swing.text.View.setSize.

Sets the size of the view. This should cause layout of the view along the given axis, if it has any layout duties.

Parameters
width:float

the width >= 0

height:float

the height >= 0

updateDamageback to summary
protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f)

Repaint the region of change covered by the given document event. Damages the line that begins the range to cover the case when the insert/remove is only on one line. If lines are added or removed, damages the whole view. The longest line is checked to see if it has changed.

Parameters
changes:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

Since
1.4
updateMetricsback to summary
protected void updateMetrics()

Checks to see if the font metrics and longest line are up-to-date.

Since
1.4
viewToModelback to summary
public int viewToModel(float x, float y, Shape a, Position.Bias[] bias)

Implements abstract javax.swing.text.View.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Parameters
x:float

the X coordinate >= 0

y:float

the Y coordinate >= 0

a:Shape

the allocated region to render into

Returns:int

the location within the model that best represents the given point in the view >= 0

See Also
View#viewToModel
javax.swing.text back to summary

pack-priv final Enum PlainView.FPMethodArgs

extends Enum<PlainView.FPMethodArgs>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final PlainView.FPMethodArgs
public static final PlainView.FPMethodArgs
public static final PlainView.FPMethodArgs
public static final PlainView.FPMethodArgs

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Class<?>[]
getMethodArguments(boolean isFPType)

public static PlainView.FPMethodArgs
public static PlainView.FPMethodArgs[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

GNNCback to summary
public static final PlainView.FPMethodArgs GNNC
GNNIIback to summary
public static final PlainView.FPMethodArgs GNNII
IGNNback to summary
public static final PlainView.FPMethodArgs IGNN
IIGNNback to summary
public static final PlainView.FPMethodArgs IIGNN

Constructor Detail

FPMethodArgsback to summary
private FPMethodArgs()

Method Detail

getMethodArgumentsback to summary
public Class<?>[] getMethodArguments(boolean isFPType)
valueOfback to summary
public static PlainView.FPMethodArgs valueOf(String name)
valuesback to summary
public static PlainView.FPMethodArgs[] values()
javax.swing.text back to summary

private Class PlainView.FPMethodItem

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final Class<?>
pack-priv final String

Constructor Summary

AccessConstructor and Description
public
FPMethodItem(Class<?> className, String methodName)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

classNameback to summary
pack-priv final Class<?> className
methodNameback to summary
pack-priv final String methodName

Constructor Detail

FPMethodItemback to summary
public FPMethodItem(Class<?> className, String methodName)

Method Detail

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override