-
Notifications
You must be signed in to change notification settings - Fork 65
/
CustomStringBuilder.java
85 lines (74 loc) · 1.77 KB
/
CustomStringBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Data-Structures-In-Java
* CustomStringBuilder.java
*/
package com.deepak.data.structures.Strings;
import java.util.Arrays;
/**
* Custom implementation of StringBuilder
*
* @author Deepak
*/
public class CustomStringBuilder {
/**
* Character array to store our buffer
*/
private char[] characterBuffer;
/**
* Current index till where buffer is full
*/
private int curIndex;
/**
* Default capacity of the string builder
*/
private int DEFAULT_CAPACITY = 10;
/**
* Default Constructor
*/
public CustomStringBuilder() {
characterBuffer = new char[DEFAULT_CAPACITY];
curIndex = 0;
}
/**
* Method to append a given string to another string
*
* @param input
*/
public void append(String input) {
if (input != null) {
char[] charArray = input.toCharArray();
/* Using while loop here because of below use case,
* 1. Initial character buffer initialized with size 10
* 2. Input string came of size 25
* 3. We need to check for overflow 2 times now because size will be increased by 10 only in one time */
while (overflow(charArray.length)) {
ensureCapacity();
}
for (int i = 0; i < charArray.length; i++) {
characterBuffer[curIndex++] = charArray[i];
}
}
}
/**
* Method to print the content
*/
public String toString() {
return new String(characterBuffer, 0, curIndex);
}
/**
* Method to ensure capacity when we reach the threshold
*/
private void ensureCapacity() {
characterBuffer = Arrays.copyOf(characterBuffer, characterBuffer.length * 2);
}
/**
* Method to check if our character buffer will overflow
* when a new text is appended
*
* @param length
* @return {@link boolean}
*/
private boolean overflow(int length) {
return (length + curIndex) > characterBuffer.length;
}
}