diff --git a/chunked-string/src/main/java/edu/technopolis/advjava/CustomString.java b/chunked-string/src/main/java/edu/technopolis/advjava/CustomString.java index d6458fd..e3dfe7c 100644 --- a/chunked-string/src/main/java/edu/technopolis/advjava/CustomString.java +++ b/chunked-string/src/main/java/edu/technopolis/advjava/CustomString.java @@ -1,43 +1,86 @@ -package edu.technopolis.advjava; - +package edu.technopolis; import java.io.Serializable; -/** - * Реализованная специальным образом строка (аналог {@link java.lang.String}), - * хранящий содержимое строки кусочкам (chunks) для лучшего переиспользования памяти при активном - * создании подстрок. - */ public class CustomString implements CharSequence, Serializable { - private final char[][] chunks; - - /* - * todo add complimentary fields if required - */ + private char[][] chunks; + int offset; + int count; + private int length; + int chunkSize; + public CustomString(String str) { + this.count = str.length(); + this.length = str.length(); + this.chunkSize =(int)Math.sqrt(this.length)+1; + this.chunks = new char[chunkSize][chunkSize]; + int k = 0; + for (int i = 0; i < chunkSize; i++){ + for (int j = 0; j < chunkSize; j++) { + if (k < length) { + chunks[i][j] = str.charAt(k); + k++; + } + } + } + } - /* - * todo add constructor or group of constructors - */ + public CustomString(char[][] chunks, int offset, int count, int chunckSize) { + this.chunks = chunks; + this.offset = offset; + this.count = count; + this.chunkSize = chunckSize; + } @Override public int length() { - //todo implement length here + return count; } @Override public char charAt(int index) { - //todo implement charAt here + if (index < 0 || index >= count) { + throw new IndexOutOfBoundsException("Out of bounds"); + } + return chunks[(index+offset)/chunkSize][(index+offset)%chunkSize]; } @Override public CharSequence subSequence(int start, int end) { - //todo implement subSequence here + if (start < 0 || start > end || end > length) { + throw new StringIndexOutOfBoundsException("Out of bounds"); + } + edu.technopolis.CustomString nc = new edu.technopolis.CustomString(chunks, offset + start, end - start + 1, chunkSize); + return nc; } @Override public String toString() { - //todo fold chunks into single char array - return new String(/* place folded char array here */); + StringBuilder outString = new StringBuilder(); + for(int i = 0; i < count; i++){ + outString.append(chunks[(offset + i)/chunkSize][(offset + i)%chunkSize]); + } + return outString.toString(); + } + + public void printStr(edu.technopolis.CustomString str){ + for (int i = 0; i < count; i++) { + System.out.print(chunks[(offset + i)/chunkSize][(offset + i)%chunkSize]); + } + System.out.println(); + } + + public static void main (String[] args) { + edu.technopolis.CustomString s = new edu.technopolis.CustomString("Hello World!"); + System.out.print("Начальная строка: "); + s.printStr(s); + System.out.println("Количество символов: " + s.length()); + System.out.println("Шестой символ начальной строки: " + s.charAt(6)); + System.out.println("Приведение начальной к String: " + s.toString()); + edu.technopolis.CustomString subStr = (edu.technopolis.CustomString) s.subSequence(2,10); + System.out.print("Новая строка: "); + subStr.printStr(subStr); + System.out.println("Количество символов новой строки: "+subStr.length()); + System.out.println("Шестой символ новой строки: " + subStr.charAt(6)); } }