/// <summary>
        /// Convert the text represented by this High Level Encoder into a BitArray.
        /// </summary>
        /// <returns>text represented by this encoder encoded as a <see cref="BitArray"/></returns>
        public BitArray encode()
        {
            State initialState = State.INITIAL_STATE;

            if (encoding != null && !disableEci)
            {
                CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(encoding);
                if (null == eci)
                {
                    throw new ArgumentException("No ECI code for character set " + encoding.WebName);
                }
                initialState = initialState.appendFLGn(eci.Value);
            }
            ICollection <State> states = new Collection <State>();

            states.Add(initialState);
            for (int index = 0; index < text.Length; index++)
            {
                int pairCode;
                // don't remove the (int) type cast, mono compiler needs it
                int nextChar = (index + 1 < text.Length) ? (int)text[index + 1] : 0;
                switch (text[index])
                {
                case (byte)'\r':
                    pairCode = nextChar == '\n' ? 2 : 0;
                    break;

                case (byte)'.':
                    pairCode = nextChar == ' ' ? 3 : 0;
                    break;

                case (byte)',':
                    pairCode = nextChar == ' ' ? 4 : 0;
                    break;

                case (byte)':':
                    pairCode = nextChar == ' ' ? 5 : 0;
                    break;

                default:
                    pairCode = 0;
                    break;
                }
                if (pairCode > 0)
                {
                    // We have one of the four special PUNCT pairs.  Treat them specially.
                    // Get a new set of states for the two new characters.
                    states = updateStateListForPair(states, index, pairCode);
                    index++;
                }
                else
                {
                    // Get a new set of states for the new character.
                    states = updateStateListForChar(states, index);
                }
            }
            // We are left with a set of states.  Find the shortest one.
            State minState = null;

            foreach (var state in states)
            {
                if (minState == null)
                {
                    minState = state;
                }
                else
                {
                    if (state.BitCount < minState.BitCount)
                    {
                        minState = state;
                    }
                }
            }

            /*
             * State minState = Collections.min(states, new Comparator<State>() {
             * @Override
             * public int compare(State a, State b) {
             *    return a.getBitCount() - b.getBitCount();
             * }
             * });
             */
            // Convert it to a bit array, and return.
            return(minState.toBitArray(text));
        }