aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/spellcheck/Word.java
blob: b2e2b40dc11d716c8e1912f90dca2aa5237040c9 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * 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;
    
    private final int end;

    /**
     * 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, end, text.substring(start, end));
    }

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

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

    public int getEnd()
    {
        return this.end;
    }

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

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