001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020package org.crsh.util;
021
022/**
023 * An immutable sequence of white spaces.
024 */
025public class BlankSequence implements CharSequence {
026
027  /** . */
028  private static final BlankSequence[] CACHE = new BlankSequence[64];
029
030  static {
031    for (int i = 0;i < CACHE.length;i++) {
032      CACHE[i] = new BlankSequence(i);
033    }
034  }
035
036  public static BlankSequence create(int length) {
037    if (length < 0) {
038      throw new IllegalArgumentException("No negative length accepted");
039    }
040    if (length < CACHE.length) {
041      return CACHE[length];
042    } else {
043      return new BlankSequence(length);
044    }
045  }
046
047  /** . */
048  private final int length;
049
050  /** . */
051  private String value;
052
053  /**
054   * Build a new blank sequence.
055   *
056   * @param length the length
057   * @throws IllegalArgumentException when length is negative
058   */
059  private BlankSequence(int length) throws IllegalArgumentException {
060    if (length < 0) {
061      throw new IllegalArgumentException();
062    }
063
064    //
065    this.length = length;
066    this.value = null;
067  }
068
069  public int length() {
070    return length;
071  }
072
073  public char charAt(int index) {
074    checkIndex("index", index);
075    return ' ';
076  }
077
078  public CharSequence subSequence(int start, int end) {
079    checkIndex("start", start);
080    checkIndex("end", end);
081    if (start > end) {
082      throw new IndexOutOfBoundsException("Start " + start + " cannot greater than end " + end);
083    }
084    return new BlankSequence(end - start);
085  }
086
087  @Override
088  public String toString() {
089    if (value == null) {
090      if (length == 0) {
091        value = "";
092      } else {
093        char[] chars = new char[length];
094        for (int i = 0;i < length;i++) {
095          chars[i] = ' ';
096        }
097        value = new String(chars, 0, chars.length);
098      }
099    }
100    return value;
101  }
102
103  private void checkIndex(String name, int index) {
104    if (index < 0) {
105      throw new IndexOutOfBoundsException("No negative " + name + " value " + index);
106    }
107    if (index >= length) {
108      throw new IndexOutOfBoundsException("The " + name + " value " + index + " cannot greater than length " + length);
109    }
110  }
111}