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
022import java.io.Serializable;
023
024/**
025 * An immutable sequence of white spaces.
026 */
027public class BlankSequence implements CharSequence, Serializable {
028
029  /** . */
030  private static final BlankSequence[] CACHE = new BlankSequence[64];
031
032  static {
033    for (int i = 0;i < CACHE.length;i++) {
034      CACHE[i] = new BlankSequence(i);
035    }
036  }
037
038  public static BlankSequence create(int length) {
039    if (length < 0) {
040      throw new IllegalArgumentException("No negative length accepted");
041    }
042    if (length < CACHE.length) {
043      return CACHE[length];
044    } else {
045      return new BlankSequence(length);
046    }
047  }
048
049  /** . */
050  private final int length;
051
052  /** . */
053  private String value;
054
055  /**
056   * Build a new blank sequence.
057   *
058   * @param length the length
059   * @throws IllegalArgumentException when length is negative
060   */
061  private BlankSequence(int length) throws IllegalArgumentException {
062    if (length < 0) {
063      throw new IllegalArgumentException();
064    }
065
066    //
067    this.length = length;
068    this.value = null;
069  }
070
071  public int length() {
072    return length;
073  }
074
075  public char charAt(int index) {
076    checkIndex("index", index);
077    return ' ';
078  }
079
080  public CharSequence subSequence(int start, int end) {
081    checkIndex("start", start);
082    checkIndex("end", end);
083    if (start > end) {
084      throw new IndexOutOfBoundsException("Start " + start + " cannot greater than end " + end);
085    }
086    return new BlankSequence(end - start);
087  }
088
089  @Override
090  public String toString() {
091    if (value == null) {
092      if (length == 0) {
093        value = "";
094      } else {
095        char[] chars = new char[length];
096        for (int i = 0;i < length;i++) {
097          chars[i] = ' ';
098        }
099        value = new String(chars, 0, chars.length);
100      }
101    }
102    return value;
103  }
104
105  private void checkIndex(String name, int index) {
106    if (index < 0) {
107      throw new IndexOutOfBoundsException("No negative " + name + " value " + index);
108    }
109    if (index > length) {
110      throw new IndexOutOfBoundsException("The " + name + " value " + index + " cannot greater than length " + length);
111    }
112  }
113}