1/*
2 *  RepeatedCharacterSequence.java
3 *  Copyright (C) 2007 Amin Ahmad.
4 *
5 *  This file is part of Java Ropes.
6 *
7 *  Java Ropes is free software: you can redistribute it and/or modify
8 *  it under the terms of the GNU General Public License as published by
9 *  the Free Software Foundation, either version 3 of the License, or
10 *  (at your option) any later version.
11 *
12 *  Java Ropes is distributed in the hope that it will be useful,
13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *  GNU General Public License for more details.
16 *
17 *  You should have received a copy of the GNU General Public License
18 *  along with Java Ropes.  If not, see <http://www.gnu.org/licenses/>.
19 *
20 *  Amin Ahmad can be contacted at amin.ahmad@gmail.com or on the web at
21 *  www.ahmadsoft.org.
22 */
23package org.ahmadsoft.ropes.impl;
24
25import java.util.Arrays;
26
27/**
28 * A character sequence defined by a character
29 * and a repeat count.
30 * @author Amin Ahmad
31 */
32public class RepeatedCharacterSequence implements CharSequence {
33
34	private char character;
35	private int repeat;
36
37	public RepeatedCharacterSequence(char character, int repeat) {
38		super();
39		this.character = character;
40		this.repeat = repeat;
41	}
42
43	@Override
44	public char charAt(int index) {
45		return getCharacter();
46	}
47
48	@Override
49	public int length() {
50		return repeat;
51	}
52
53	@Override
54	public CharSequence subSequence(int start, int end) {
55		return new RepeatedCharacterSequence(getCharacter(), end - start);
56	}
57
58	@Override
59	public String toString() {
60		char[] result = new char[repeat];
61		Arrays.fill(result, character);
62		return new String(result);
63	}
64
65	/**
66	 * Returns the character used to construct this sequence.
67	 * @return the character used to construct this sequence.
68	 */
69	public char getCharacter() {
70		return character;
71	}
72
73}
74