1/*
2*******************************************************************************
3*   Copyright (C) 2001-2016, International Business Machines
4*   Corporation and others.  All Rights Reserved.
5*******************************************************************************
6*/
7/* Written by Simon Montagu, Matitiahu Allouche
8 * (ported from C code written by Markus W. Scherer)
9 */
10
11package com.ibm.icu.text;
12
13/**
14 * A BidiRun represents a sequence of characters at the same embedding level.
15 * The Bidi algorithm decomposes a piece of text into sequences of characters
16 * at the same embedding level, each such sequence is called a "run".
17 *
18 * <p>A BidiRun represents such a run by storing its essential properties,
19 * but does not duplicate the characters which form the run.
20 *
21 * <p>The &quot;limit&quot; of the run is the position just after the
22 * last character, i.e., one more than that position.
23 *
24 * <p>This class has no public constructor, and its members cannot be
25 * modified by users.
26 *
27 * @see com.ibm.icu.text.Bidi
28 * @stable ICU 3.8
29 */
30public class BidiRun {
31
32    int start;              /* first logical position of the run */
33    int limit;              /* last visual position of the run +1 */
34    int insertRemove;       /* if >0, flags for inserting LRM/RLM before/after run,
35                               if <0, count of bidi controls within run            */
36    byte level;
37
38    /*
39     * Default constructor
40     *
41     * Note that members start and limit of a run instance have different
42     * meanings depending whether the run is part of the runs array of a Bidi
43     * object, or if it is a reference returned by getVisualRun() or
44     * getLogicalRun().
45     * For a member of the runs array of a Bidi object,
46     *   - start is the first logical position of the run in the source text.
47     *   - limit is one after the last visual position of the run.
48     * For a reference returned by getLogicalRun() or getVisualRun(),
49     *   - start is the first logical position of the run in the source text.
50     *   - limit is one after the last logical position of the run.
51     */
52    BidiRun()
53    {
54        this(0, 0, (byte)0);
55    }
56
57    /*
58     * Constructor
59     */
60    BidiRun(int start, int limit, byte embeddingLevel)
61    {
62        this.start = start;
63        this.limit = limit;
64        this.level = embeddingLevel;
65    }
66
67    /*
68     * Copy the content of a BidiRun instance
69     */
70    void copyFrom(BidiRun run)
71    {
72        this.start = run.start;
73        this.limit = run.limit;
74        this.level = run.level;
75        this.insertRemove = run.insertRemove;
76    }
77
78    /**
79     * Get the first logical position of the run in the source text
80     * @stable ICU 3.8
81     */
82    public int getStart()
83    {
84        return start;
85    }
86
87    /**
88     * Get position of one character after the end of the run in the source text
89     * @stable ICU 3.8
90     */
91    public int getLimit()
92    {
93        return limit;
94    }
95
96    /**
97     * Get length of run
98     * @stable ICU 3.8
99     */
100    public int getLength()
101    {
102        return limit - start;
103    }
104
105    /**
106     * Get level of run
107     * @stable ICU 3.8
108     */
109    public byte getEmbeddingLevel()
110    {
111        return level;
112    }
113
114    /**
115     * Check if run level is odd
116     * @return true if the embedding level of this run is odd, i.e. it is a
117     *  right-to-left run.
118     * @stable ICU 3.8
119     */
120    public boolean isOddRun()
121    {
122        return (level & 1) == 1;
123    }
124
125    /**
126     * Check if run level is even
127     * @return true if the embedding level of this run is even, i.e. it is a
128     *  left-to-right run.
129     * @stable ICU 3.8
130     */
131    public boolean isEvenRun()
132    {
133        return (level & 1) == 0;
134    }
135
136    /**
137     * Get direction of run
138     * @stable ICU 3.8
139     */
140    public byte getDirection()
141    {
142        return (byte)(level & 1);
143    }
144
145    /**
146     * String to display run
147     * @stable ICU 3.8
148     */
149    public String toString()
150    {
151        return "BidiRun " + start + " - " + limit + " @ " + level;
152    }
153}
154