aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/spellcheck/Word.java
blob: e7a882aeaeeea756a40c9846e0b845c018e7fc19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.spellcheck;

import java.text.BreakIterator;

/**
 * Immutable representation of a word in the context of a document, bundling
 * the bounds with the text.
 * @author Damian Johnson
 */
class Word
{
    private static final BreakIterator WORD_ITR
        = BreakIterator.getWordInstance();
    private final int start;
    private final String text;

    /**
     * Provides the word before or after a given index. No bounds checking is
     * performed.
     * @param text text to be checked
     * @param index index in which to begin search (inclusive)
     * @param before search is before index if true, after otherwise
     * @return index of word boundary
     */
    public static synchronized Word getWord(String text, int index,
            boolean before)
    {
        int start, end;
        WORD_ITR.setText(text);

        if (before)
        {
            start = WORD_ITR.preceding(index);
            end = WORD_ITR.next();
            if (start == BreakIterator.DONE) start = 0;
        }
        else
        {
            end = WORD_ITR.following(index);
            start = WORD_ITR.previous();
            if (end == BreakIterator.DONE) end = text.length() - 1;
        }

        return new Word(start, text.substring(start, end));
    }

    private Word(int start, String text)
    {
        this.start = start;
        this.text = text;
    }

    public int getStart()
    {
        return this.start;
    }

    public String getText()
    {
        return this.text;
    }

    public String toString()
    {
        return this.text;
    }
}