public final class AlphabeticIndex<V> extends Object implements Iterable<AlphabeticIndex.Bucket<V>>
... 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).
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 ...
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.
int bucketIndex = index.getBucketIndex(name); String label = immutableIndex.getBucket(bucketIndex).getLabel(); // optional RawCollationKey collationKey = collator.getRawCollationKey(name, null);
Modifier and Type | Class and Description |
---|---|
static class |
AlphabeticIndex.Bucket<V>
An index "bucket" with a label string and type.
|
static class |
AlphabeticIndex.ImmutableIndex<V>
Immutable, thread-safe version of
AlphabeticIndex . |
static class |
AlphabeticIndex.Record<V>
A (name, data) pair, to be sorted by name into one of the index buckets.
|
Constructor and Description |
---|
AlphabeticIndex(Locale locale)
Create the index object.
|
AlphabeticIndex(RuleBasedCollator collator)
Create an AlphabeticIndex that uses a specific collator.
|
AlphabeticIndex(ULocale locale)
Create the index object.
|
Modifier and Type | Method and Description |
---|---|
AlphabeticIndex<V> |
addLabels(Locale... additions)
Add more index characters (aside from what are in the locale)
|
AlphabeticIndex<V> |
addLabels(ULocale... additions)
Add more index characters (aside from what are in the locale)
|
AlphabeticIndex<V> |
addLabels(UnicodeSet additions)
Add more index characters (aside from what are in the locale)
|
AlphabeticIndex<V> |
addRecord(CharSequence name,
V data)
Add a record (name and data) to the index.
|
AlphabeticIndex.ImmutableIndex<V> |
buildImmutableIndex()
Builds an immutable, thread-safe version of this instance, without data records.
|
AlphabeticIndex<V> |
clearRecords()
Clear the index.
|
int |
getBucketCount()
Return the number of buckets in the index.
|
int |
getBucketIndex(CharSequence name)
Get the bucket number for the given name.
|
List<String> |
getBucketLabels()
Get the labels.
|
RuleBasedCollator |
getCollator()
Get a clone of the collator used internally.
|
List<String> |
getFirstCharactersInScripts()
Deprecated.
This API is ICU internal, only for testing.
|
String |
getInflowLabel()
Get the default label used for abbreviated buckets between other labels.
|
int |
getMaxLabelCount()
Get the limit on the number of labels in the index.
|
String |
getOverflowLabel()
Get the default label used in the IndexCharacters' locale for overflow, eg the first item in: ...
|
int |
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.
|
String |
getUnderflowLabel()
Get the default label used in the IndexCharacters' locale for underflow, eg the last item in: X Y Z ...
|
Iterator<AlphabeticIndex.Bucket<V>> |
iterator()
Return an iterator over the buckets.
|
AlphabeticIndex<V> |
setInflowLabel(String inflowLabel)
Set the inflowLabel label
|
AlphabeticIndex<V> |
setMaxLabelCount(int maxLabelCount)
Set a limit on the number of labels in the index.
|
AlphabeticIndex<V> |
setOverflowLabel(String overflowLabel)
Set the overflow label
|
AlphabeticIndex<V> |
setUnderflowLabel(String underflowLabel)
Set the underflowLabel label
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public AlphabeticIndex(ULocale locale)
locale
- The locale for the index.public AlphabeticIndex(Locale locale)
locale
- The locale for the index.public AlphabeticIndex(RuleBasedCollator collator)
The index will be created with no labels; the addLabels() function must be called after creation to add the desired labels to the index.
The index will work directly with the supplied collator. If the caller will need to continue working with the collator it should be cloned first, so that the collator provided to the AlphabeticIndex remains unchanged after creation of the index.
collator
- The collator to use to order the contents of this index.public AlphabeticIndex<V> addLabels(UnicodeSet additions)
additions
- additional characters to add to the index, such as A-Z.public AlphabeticIndex<V> addLabels(ULocale... additions)
additions
- additional characters to add to the index, such as those in Swedish.public AlphabeticIndex<V> addLabels(Locale... additions)
additions
- additional characters to add to the index, such as those in Swedish.public AlphabeticIndex<V> setOverflowLabel(String overflowLabel)
overflowLabel
- see class descriptionpublic String getUnderflowLabel()
public AlphabeticIndex<V> setUnderflowLabel(String underflowLabel)
underflowLabel
- see class descriptionpublic String getOverflowLabel()
public AlphabeticIndex<V> setInflowLabel(String inflowLabel)
inflowLabel
- see class descriptionpublic String getInflowLabel()
public int getMaxLabelCount()
public AlphabeticIndex<V> setMaxLabelCount(int maxLabelCount)
maxLabelCount
- 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.public AlphabeticIndex.ImmutableIndex<V> buildImmutableIndex()
public List<String> getBucketLabels()
public RuleBasedCollator getCollator()
Don't use this method across threads if you are changing the settings on the collator, at least not without synchronizing.
public AlphabeticIndex<V> addRecord(CharSequence name, V data)
name
- Name, such as a namedata
- Data, such as an address or linkpublic int getBucketIndex(CharSequence name)
Note that the bucket number (and sort key) are only valid for the settings of the current AlphabeticIndex; if those are changed, then the bucket number and sort key must be regenerated.
name
- Name, such as a namepublic AlphabeticIndex<V> clearRecords()
public int getBucketCount()
public int getRecordCount()
public Iterator<AlphabeticIndex.Bucket<V>> iterator()
iterator
in interface Iterable<AlphabeticIndex.Bucket<V>>
@Deprecated public List<String> getFirstCharactersInScripts()
Copyright © 2016 Unicode, Inc. and others.