All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.util.StringBand Maven / Gradle / Ivy

Go to download

Jodd Core tools and utilities, including type converters, JDateTime, cache etc.

There is a newer version: 5.3.0
Show newest version
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.util;

/**
 * StringBand is a faster alternative to StringBuilder.
 * Instead of adding strings, they are stored in an internal array. Only at the
 * end of concatenation, when toString() is invoked, strings are
 * joined together in a very fast manner.
 * 

* To make StringBand even faster, predict the number of joined * strings (and not the final string size)! */ public class StringBand { private static final int DEFAULT_ARRAY_CAPACITY = 16; private String[] array; private int index; private int length; /** * Creates an empty StringBand. */ public StringBand() { array = new String[DEFAULT_ARRAY_CAPACITY]; } /** * Creates an empty StringBand with provided capacity. * Capacity refers to internal string array (i.e. number of * joins) and not the total string size. */ public StringBand(int initialCapacity) { if (initialCapacity <= 0) { throw new IllegalArgumentException("Invalid initial capacity"); } array = new String[initialCapacity]; } /** * Creates StringBand with provided content. */ public StringBand(String s) { this(); array[0] = s; index = 1; length = s.length(); } public StringBand(Object o) { this(String.valueOf(o)); } // ---------------------------------------------------------------- append /** * Appends boolean value. */ public StringBand append(boolean b) { return append(b ? StringPool.TRUE : StringPool.FALSE); } /** * Appends double value. */ public StringBand append(double d) { return append(Double.toString(d)); } /** * Appends float value. */ public StringBand append(float f) { return append(Float.toString(f)); } /** * Appends int value. */ public StringBand append(int i) { return append(Integer.toString(i)); } /** * Appends long value. */ public StringBand append(long l) { return append(Long.toString(l)); } /** * Appends short value. */ public StringBand append(short s) { return append(Short.toString(s)); } /** * Appends a character. This is not efficient * as in StringBuilder, since new string is created. */ public StringBand append(char c) { return append(String.valueOf(c)); } /** * Appends byte value. */ public StringBand append(byte b) { return append(Byte.toString(b)); } /** * Appends string representation of an object. * If null, the 'null' string * will be appended. */ public StringBand append(Object obj) { return append(String.valueOf(obj)); } /** * Appends a string. */ public StringBand append(String s) { if (s == null) { s = StringPool.NULL; } if (index >= array.length) { expandCapacity(); } array[index++] = s; length += s.length(); return this; } // ---------------------------------------------------------------- size /** * Returns array capacity. */ public int capacity() { return array.length; } /** * Returns total string length. */ public int length() { return length; } /** * Returns current index of string array. */ public int index() { return index; } /** * Specifies the new index. */ public void setIndex(int newIndex) { if (newIndex < 0) { throw new ArrayIndexOutOfBoundsException(newIndex); } if (newIndex > array.length) { String[] newArray = new String[newIndex]; System.arraycopy(array, 0, newArray, 0, index); array = newArray; } if (newIndex > index) { for (int i = index; i < newIndex; i++) { array[i] = StringPool.EMPTY; } } else if (newIndex < index) { for (int i = newIndex; i < index; i++) { array[i] = null; } } index = newIndex; length = calculateLength(); } // ---------------------------------------------------------------- values /** * Returns char at given position. * This method is not fast as it calculates * the right string array element and the offset! */ public char charAt(int pos) { int len = 0; for (int i = 0; i < index; i++) { int newlen = len + array[i].length(); if (pos < newlen) { return array[i].charAt(pos - len); } len = newlen; } throw new IllegalArgumentException("Invalid char index"); } /** * Returns string at given position. */ public String stringAt(int index) { if (index >= this.index) { throw new ArrayIndexOutOfBoundsException(); } return array[index]; } /** * Joins together all strings into one. */ public String toString() { // special cases if (index == 0) { return StringPool.EMPTY; } // join strings char[] destination = new char[length]; int start = 0; for (int i = 0; i < index; i++) { String s = array[i]; int len = s.length(); s.getChars(0, len, destination, start); start += len; } return new String(destination); } // ---------------------------------------------------------------- utils /** * Expands internal string array by multiplying its size by 2. */ protected void expandCapacity() { String[] newArray = new String[array.length << 1]; System.arraycopy(array, 0, newArray, 0, index); array = newArray; } /** * Calculates string length. */ protected int calculateLength() { int len = 0; for (int i = 0; i < index; i++) { len += array[i].length(); } return len; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy