Package com.humaorie.dollar

Source Code of com.humaorie.dollar.CharSequenceWrapper

package com.humaorie.dollar;

/*
* Dollar, http://bitbucket.org/dfa/dollar
* (c) 2010, 2011 Davide Angelocola <davide.angelocola@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import com.humaorie.dollar.ArrayWrapper.CharArrayWrapper;
import com.humaorie.dollar.Dollar.Wrapper;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;

// TODO: try to avoid toString()
public class CharSequenceWrapper extends AbstractWrapper<Character> {

    private final CharSequence charSequence;

    public CharSequenceWrapper(CharSequence charSequence) {
        Preconditions.require(charSequence != null, "charSequence must be non-null");
        this.charSequence = charSequence;
    }

    @Override
    public Wrapper<Character> repeat(int n) {
        Preconditions.require(n > 0, "n must be positive");
        return new RepeatCharSequenceWrapper(charSequence, n);
    }

    @Override
    public Wrapper<Character> copy() {
        return new CharSequenceWrapper(charSequence);
    }

    @Override
    public Wrapper<Character> reverse() {
        return new CharSequenceWrapper(new StringBuilder(charSequence).reverse().toString());
    }

    @Override
    public Wrapper<Character> shuffle(Random random) {
        Preconditions.require(random != null, "random must be non-null");
        StringBuilder stringBuilder = new StringBuilder(charSequence);
        for (int i = 0; i < stringBuilder.length(); i++) {
            int j = random.nextInt(stringBuilder.length());
            char c = stringBuilder.charAt(i);
            stringBuilder.setCharAt(i, stringBuilder.charAt(j));
            stringBuilder.setCharAt(j, c);
        }
        return new CharSequenceWrapper(stringBuilder.toString());
    }

    @Override
    public int size() {
        return charSequence.length();
    }

    @Override
    public Wrapper<Character> slice(int i, int j) {
        if (i >= 0 && j < charSequence.length()) {
            return new CharSequenceWrapper(charSequence.subSequence(i, j));
        } else {
            return this;
        }
    }

    @Override
    public Iterator<Character> iterator() {
        return new CharSequenceIterator(charSequence);
    }

    public static class CharSequenceIterator implements Iterator<Character> {

        private final CharSequence charSequence;
        private int i;

        public CharSequenceIterator(CharSequence charSequence) {
            this.charSequence = charSequence;
            this.i = 0;
        }

        @Override
        public boolean hasNext() {
            return i < charSequence.length();
        }

        @Override
        public Character next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }

            return charSequence.charAt(i++);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove() not supported for CharSequenceWrapper");
        }
    }

    @Override
    public Wrapper<Character> sort() {
        return new CharArrayWrapper(charSequenceToArray(charSequence)).sort();
    }

    @Override
    public Character[] toArray() {
        return new CharArrayWrapper(charSequenceToArray(charSequence)).toArray();
    }

    @Override
    public boolean equals(Object object) {
        if (object instanceof CharSequenceWrapper) {
            CharSequenceWrapper charSequenceWrapper = (CharSequenceWrapper) object;
            return charSequence.equals(charSequenceWrapper.charSequence);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return charSequence.hashCode();
    }

    @Override
    public String toString() {
        return charSequence.toString();
    }

    private static char[] charSequenceToArray(CharSequence charSequence) {
        char[] array = new char[charSequence.length()];

        for (int i = 0; i < charSequence.length(); i++) {
            array[i] = charSequence.charAt(i);
        }

        return array;
    }
}
TOP

Related Classes of com.humaorie.dollar.CharSequenceWrapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.