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

public Class Segment

extends Object
implements Cloneable, CharacterIterator, CharSequence
Class Inheritance
All Implemented Interfaces
java.lang.CharSequence, java.text.CharacterIterator, java.lang.Cloneable
Known Direct Subclasses
javax.swing.text.SegmentCache.CachedSegment, javax.swing.text.TextLayoutStrategy.AttributedSegment, javax.swing.text.JTextComponent.AccessibleJTextComponent.IndexedSegment, javax.swing.text.html.AccessibleHTML.TextElementInfo.TextAccessibleContext.IndexedSegment
Imports
java.text.CharacterIterator

A segment of a character array representing a fragment of text. It should be treated as immutable even though the array is directly accessible. This gives fast access to fragments of text without the overhead of copying around characters. This is effectively an unprotected String.

The Segment implements the java.text.CharacterIterator interface to support use with the i18n support without copying text into a string.

Author
Timothy Prinzing

Field Summary

Modifier and TypeField and Description
public char[]
array

This is the array containing the text of interest.

pack-priv boolean
copy

Whether the array is a copy of data or not.

public int
count

This is the number of array elements that make up the text of interest.

public int
offset

This is the offset into the array that the desired text begins.

private boolean
private int

Constructor Summary

AccessConstructor and Description
public
Segment()

Creates a new segment.

public
Segment(char[]
the array to refer to
array
,
int
the offset into the array
offset
,
int
the number of characters
count
)

Creates a new segment referring to an existing array.

Method Summary

Modifier and TypeMethod and Description
public char
charAt(int index)

Implements java.lang.CharSequence.charAt.

Returns the char value at the specified index.

public Object

Returns:

the copy
clone
()

Overrides java.lang.Object.clone.

Implements java.text.CharacterIterator.clone.

Creates a shallow copy.

public char

Returns:

the character at the current position or DONE if the current position is off the end of the text.
current
()

Implements java.text.CharacterIterator.current.

Gets the character at the current position (as returned by getIndex()).

public char

Returns:

the first character in the text, or DONE if the text is empty
first
()

Implements java.text.CharacterIterator.first.

Sets the position to getBeginIndex() and returns the character at that position.

public int

Returns:

the index at which the text begins.
getBeginIndex
()

Implements java.text.CharacterIterator.getBeginIndex.

Returns the start index of the text.

public int

Returns:

the index after the last character in the text
getEndIndex
()

Implements java.text.CharacterIterator.getEndIndex.

Returns the end index of the text.

public int

Returns:

the current index.
getIndex
()

Implements java.text.CharacterIterator.getIndex.

Returns the current index.

public boolean

Returns:

whether or not partial returns are valid.
isPartialReturn
()

Flag to indicate that partial returns are valid.

public char

Returns:

the last character in the text, or DONE if the text is empty
last
()

Implements java.text.CharacterIterator.last.

Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position.

public int
length()

Implements java.lang.CharSequence.length.

Returns the length of this character sequence.

public char

Returns:

the character at the new position or DONE if the new position is off the end of the text range.
next
()

Implements java.text.CharacterIterator.next.

Increments the iterator's index by one and returns the character at the new index.

public char

Returns:

the character at the new position or DONE if the current position is equal to getBeginIndex().
previous
()

Implements java.text.CharacterIterator.previous.

Decrements the iterator's index by one and returns the character at the new index.

public char

Returns:

the character at the specified position or DONE if the specified position is equal to getEndIndex()
setIndex
(int
the position within the text. Valid values range from getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown if an invalid value is supplied.
position
)

Implements java.text.CharacterIterator.setIndex.

Sets the position to the specified position in the text and returns that character.

public void
setPartialReturn(boolean
whether or not partial returns are valid.
p
)

Flag to indicate that partial returns are valid.

public CharSequence
subSequence(int start, int end)

Implements java.lang.CharSequence.subSequence.

Returns a CharSequence that is a subsequence of this sequence.

public String

Returns:

the string
toString
()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Converts a segment into a String.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

arrayback to summary
public char[] array

This is the array containing the text of interest. This array should never be modified; it is available only for efficiency.

copyback to summary
pack-priv boolean copy

Whether the array is a copy of data or not.

countback to summary
public int count

This is the number of array elements that make up the text of interest.

offsetback to summary
public int offset

This is the offset into the array that the desired text begins.

partialReturnback to summary
private boolean partialReturn
posback to summary
private int pos

Constructor Detail

Segmentback to summary
public Segment()

Creates a new segment.

Segmentback to summary
public Segment(char[] array, int offset, int count)

Creates a new segment referring to an existing array.

Parameters
array:char[]

the array to refer to

offset:int

the offset into the array

count:int

the number of characters

Method Detail

charAtback to summary
public char charAt(int index)

Implements java.lang.CharSequence.charAt.

Doc from java.lang.CharSequence.charAt.

Returns the char value at the specified index. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Since
1.6
cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

Implements java.text.CharacterIterator.clone.

Creates a shallow copy.

Returns:Object

the copy

currentback to summary
public char current()

Implements java.text.CharacterIterator.current.

Gets the character at the current position (as returned by getIndex()).

Returns:char

the character at the current position or DONE if the current position is off the end of the text.

Since
1.3
See Also
getIndex
firstback to summary
public char first()

Implements java.text.CharacterIterator.first.

Sets the position to getBeginIndex() and returns the character at that position.

Returns:char

the first character in the text, or DONE if the text is empty

Since
1.3
See Also
getBeginIndex
getBeginIndexback to summary
public int getBeginIndex()

Implements java.text.CharacterIterator.getBeginIndex.

Returns the start index of the text.

Returns:int

the index at which the text begins.

Since
1.3
getEndIndexback to summary
public int getEndIndex()

Implements java.text.CharacterIterator.getEndIndex.

Returns the end index of the text. This index is the index of the first character following the end of the text.

Returns:int

the index after the last character in the text

Since
1.3
getIndexback to summary
public int getIndex()

Implements java.text.CharacterIterator.getIndex.

Returns the current index.

Returns:int

the current index.

Since
1.3
isPartialReturnback to summary
public boolean isPartialReturn()

Flag to indicate that partial returns are valid.

Returns:boolean

whether or not partial returns are valid.

Since
1.4
lastback to summary
public char last()

Implements java.text.CharacterIterator.last.

Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position.

Returns:char

the last character in the text, or DONE if the text is empty

Since
1.3
See Also
getEndIndex
lengthback to summary
public int length()

Implements java.lang.CharSequence.length.

Doc from java.lang.CharSequence.length.

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Since
1.6
nextback to summary
public char next()

Implements java.text.CharacterIterator.next.

Increments the iterator's index by one and returns the character at the new index. If the resulting index is greater or equal to getEndIndex(), the current index is reset to getEndIndex() and a value of DONE is returned.

Returns:char

the character at the new position or DONE if the new position is off the end of the text range.

Since
1.3
previousback to summary
public char previous()

Implements java.text.CharacterIterator.previous.

Decrements the iterator's index by one and returns the character at the new index. If the current index is getBeginIndex(), the index remains at getBeginIndex() and a value of DONE is returned.

Returns:char

the character at the new position or DONE if the current position is equal to getBeginIndex().

Since
1.3
setIndexback to summary
public char setIndex(int position)

Implements java.text.CharacterIterator.setIndex.

Sets the position to the specified position in the text and returns that character.

Parameters
position:int

the position within the text. Valid values range from getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown if an invalid value is supplied.

Returns:char

the character at the specified position or DONE if the specified position is equal to getEndIndex()

Since
1.3
setPartialReturnback to summary
public void setPartialReturn(boolean p)

Flag to indicate that partial returns are valid. If the flag is true, an implementation of the interface method Document.getText(position,length,Segment) should return as much text as possible without making a copy. The default state of the flag is false which will cause Document.getText(position,length,Segment) to provide the same return behavior it always had, which may or may not make a copy of the text depending upon the request.

Parameters
p:boolean

whether or not partial returns are valid.

Since
1.4
subSequenceback to summary
public CharSequence subSequence(int start, int end)

Implements java.lang.CharSequence.subSequence.

Doc from java.lang.CharSequence.subSequence.

Returns a CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

Since
1.6
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Converts a segment into a String.

Returns:String

the string