Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class AlphabeticIndex<V>

extends Object
implements Iterable<AlphabeticIndex.Bucket<V>>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Imports
java.util.ArrayList, .Collections, .Comparator, .Iterator, .List, .Locale, com.ibm.icu.lang.UCharacter, com.ibm.icu.text.AlphabeticIndex.Bucket, .AlphabeticIndex.Bucket.LabelType, com.ibm.icu.util.LocaleData, .ULocale

AlphabeticIndex supports the creation of a UI index appropriate for a given language. It can support either direct use, or use with a client that doesn't support localized collation. The following is an example of what an index might look like in a UI:
 ... A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  ...

 A
    Addison
    Albertson
    Azensky
 B
    Baecker
 ...
The class can generate a list of labels for use as a UI "index", that is, a list of clickable characters (or character sequences) that allow the user to see a segment (bucket) of a larger "target" list. That is, each label corresponds to a bucket in the target list, where everything in the bucket is greater than or equal to the character (according to the locale's collation). Strings can be added to the index; they will be in sorted order in the right bucket.

The class also supports having buckets for strings before the first (underflow), after the last (overflow), and between scripts (inflow). For example, if the index is constructed with labels for Russian and English, Greek characters would fall into an inflow bucket between the other two scripts.

Note

If you expect to have a lot of ASCII or Latin characters as well as characters from the user's language, then it is a good idea to call addLabels(ULocale.English).

Direct Use

The following shows an example of building an index directly. The "show..." methods below are just to illustrate usage.

// Create a simple index where the values for the strings are Integers, and add the strings

AlphabeticIndex<Integer> index = new AlphabeticIndex<Integer>(desiredLocale).addLabels(additionalLocale);
int counter = 0;
for (String item : test) {
    index.addRecord(item, counter++);
}
...
// Show index at top. We could skip or gray out empty buckets

for (AlphabeticIndex.Bucket<Integer> bucket : index) {
    if (showAll || bucket.size() != 0) {
        showLabelAtTop(UI, bucket.getLabel());
    }
}
 ...
// Show the buckets with their contents, skipping empty buckets

for (AlphabeticIndex.Bucket<Integer> bucket : index) {
    if (bucket.size() != 0) {
        showLabelInList(UI, bucket.getLabel());
        for (AlphabeticIndex.Record<Integer> item : bucket) {
            showIndexedItem(UI, item.getName(), item.getData());
        }
The caller can build different UIs using this class. For example, an index character could be omitted or grayed-out if its bucket is empty. Small buckets could also be combined based on size, such as:
... A-F G-N O-Z ...

Client Support

Callers can also use the AlphabeticIndex.ImmutableIndex, or the AlphabeticIndex itself, to support sorting on a client that doesn't support AlphabeticIndex functionality.

The ImmutableIndex is both immutable and thread-safe. The corresponding AlphabeticIndex methods are not thread-safe because they "lazily" build the index buckets.

Author
Mark Davis
ICU Status
Stable since ICU 4.8.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
AlphabeticIndex.Bucket<
Data type
V
>

An index "bucket" with a label string and type.

private static class
public static class
AlphabeticIndex.ImmutableIndex<
The Record value type is unused. It can be omitted for this class if it was omitted for the AlphabeticIndex that built it.
V
>

Immutable, thread-safe version of AlphabeticIndex.

public static class
AlphabeticIndex.Record<V>

A (name, data) pair, to be sorted by name into one of the index buckets.

Field Summary

Modifier and TypeField and Description
private static final String
BASE

Prefix string for Chinese index buckets.

private static final Comparator<String>
private AlphabeticIndex.BucketList<V>
private static final char
private RuleBasedCollator
private final RuleBasedCollator
private final RuleBasedCollator
private final List<String>
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private String
private final UnicodeSet
private List<AlphabeticIndex.Record<V>>
private int
private String
private final Comparator<AlphabeticIndex.Record<V>>
private String

Constructor Summary

AccessConstructor and Description
public
AlphabeticIndex(ULocale
The locale for the index.
locale
)

Create the index object.

public
AlphabeticIndex(Locale
The locale for the index.
locale
)

Create the index object.

public
AlphabeticIndex(RuleBasedCollator
The collator to use to order the contents of this index.
collator
)

Create an AlphabeticIndex that uses a specific collator.

private
AlphabeticIndex(ULocale locale, RuleBasedCollator collator)

Internal constructor containing implementation used by public constructors.

Method Summary

Modifier and TypeMethod and Description
private boolean
addChineseIndexCharacters()

Add Chinese index characters from the tailoring.

private void
addIndexExemplars(ULocale locale)

This method is called to get the index exemplars.

public AlphabeticIndex<V>

Returns:

this, for chaining
addLabels
(UnicodeSet
additional characters to add to the index, such as A-Z.
additions
)

Add more index characters (aside from what are in the locale)

public AlphabeticIndex<V>

Returns:

this, for chaining
addLabels
(ULocale...
additional characters to add to the index, such as those in Swedish.
additions
)

Add more index characters (aside from what are in the locale)

public AlphabeticIndex<V>

Returns:

this, for chaining
addLabels
(Locale...
additional characters to add to the index, such as those in Swedish.
additions
)

Add more index characters (aside from what are in the locale)

public AlphabeticIndex<V>

Returns:

this, for chaining
addRecord
(CharSequence
Name, such as a name
name
,
V
Data, such as an address or link
data
)

Add a record (name and data) to the index.

public AlphabeticIndex.ImmutableIndex<V>

Returns:

an immutable index instance
buildImmutableIndex
()

Builds an immutable, thread-safe version of this instance, without data records.

public AlphabeticIndex<V>

Returns:

this, for chaining
clearRecords
()

Clear the index.

private AlphabeticIndex.BucketList<V>
private static String
fixLabel(String current)

public int

Returns:

number of buckets
getBucketCount
()

Return the number of buckets in the index.

public int

Returns:

the bucket index for the name
getBucketIndex
(CharSequence
Name, such as a name
name
)

Get the bucket number for the given name.

public List<String>

Returns:

The list of bucket labels, after processing.
getBucketLabels
()

Get the labels.

public RuleBasedCollator

Returns:

a clone of the collator used internally
getCollator
()

Get a clone of the collator used internally.

public List<String>

Returns:

list of first characters in each script
getFirstCharactersInScripts
()

Deprecated This API is ICU internal, only for testing.
Return a list of the first character in each script.
public String

Returns:

inflow label
getInflowLabel
()

Get the default label used for abbreviated buckets between other labels.

public int

Returns:

maxLabelCount maximum number of labels.
getMaxLabelCount
()

Get the limit on the number of labels in the index.

public String

Returns:

overflow label
getOverflowLabel
()

Get the default label used in the IndexCharacters' locale for overflow, eg the first item in: ...

public int

Returns:

total number of records in buckets
getRecordCount
()

Return the number of records in the index: that is, the total number of distinct <name,data> pairs added with addRecord(...), over all the buckets.

public String

Returns:

underflow label
getUnderflowLabel
()

Get the default label used in the IndexCharacters' locale for underflow, eg the last item in: X Y Z ...

private static boolean
private void
initBuckets()

Creates an index, and buckets and sorts the list of records into the index.

private List<String>
initLabels()

Determine the best labels to use.

private static boolean
isOneLabelBetterThanOther(Normalizer2 nfkdNormalizer, String one, String other)

Returns true if one index character string is "better" than the other.

public Iterator<AlphabeticIndex.Bucket<V>>

Returns:

iterator over buckets.
iterator
()

Implements java.lang.Iterable.iterator.

Return an iterator over the buckets.
private String
separated(String item)

Return the string with interspersed CGJs.

public AlphabeticIndex<V>

Returns:

this, for chaining
setInflowLabel
(String
see class description
inflowLabel
)

Set the inflowLabel label

public AlphabeticIndex<V>

Returns:

this, for chaining
setMaxLabelCount
(int
Set the maximum number of labels. Currently, if the number is exceeded, then every nth item is removed to bring the count down. A more sophisticated mechanism may be available in the future.
maxLabelCount
)

Set a limit on the number of labels in the index.

public AlphabeticIndex<V>

Returns:

this, for chaining
setOverflowLabel
(String
see class description
overflowLabel
)

Set the overflow label

public AlphabeticIndex<V>

Returns:

this, for chaining
setUnderflowLabel
(String
see class description
underflowLabel
)

Set the underflowLabel label

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait