patternjavaModerate
Rotating strings in Java
Viewed 0 times
stringsrotatingjava
Problem
I have this Java class for producing rotations of an input string:
StringRotator.java
```
package net.coderodde.util;
import java.util.Objects;
/**
* This class implements a data type for rotating strings.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Mar 24, 2016)
*/
public class StringRotator {
/**
* The source string. All rotations will be produced from this string.
*/
private final String string;
/**
* Caches the length of the source string in order to by a little of
* efficiency.
*/
private final int length;
/**
* Caches the current rotation. This field is set to {@code null} after
* every effective rotation. Whenever the user requests the rotated string,
* if this field is not {@code null}, the cached string is returned.
* Otherwise, a rotated string is built and set to this field, and finally
* returned.
*/
private String cachedString;
/**
* Index of the first logical character component in the source string.
*/
private int finger;
public StringRotator(String string) {
this.string = Objects.requireNonNull(string,
"The input string is null.");
this.length = string.length();
}
public void rotate(int offset) {
int oldFinger = finger;
finger += offset;
finger %= string.length();
if (finger != oldFinger) {
cachedString = null;
}
}
@Override
public String toString() {
if (cachedString != null) {
return cachedString;
}
return cachedString = buildString();
}
private String buildString() {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; ++i) {
sb.append(string.charAt((finger + i) % length));
}
return sb.toString();
}
public static void main(final String... args) {
StringBuilder s
StringRotator.java
```
package net.coderodde.util;
import java.util.Objects;
/**
* This class implements a data type for rotating strings.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Mar 24, 2016)
*/
public class StringRotator {
/**
* The source string. All rotations will be produced from this string.
*/
private final String string;
/**
* Caches the length of the source string in order to by a little of
* efficiency.
*/
private final int length;
/**
* Caches the current rotation. This field is set to {@code null} after
* every effective rotation. Whenever the user requests the rotated string,
* if this field is not {@code null}, the cached string is returned.
* Otherwise, a rotated string is built and set to this field, and finally
* returned.
*/
private String cachedString;
/**
* Index of the first logical character component in the source string.
*/
private int finger;
public StringRotator(String string) {
this.string = Objects.requireNonNull(string,
"The input string is null.");
this.length = string.length();
}
public void rotate(int offset) {
int oldFinger = finger;
finger += offset;
finger %= string.length();
if (finger != oldFinger) {
cachedString = null;
}
}
@Override
public String toString() {
if (cachedString != null) {
return cachedString;
}
return cachedString = buildString();
}
private String buildString() {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; ++i) {
sb.append(string.charAt((finger + i) % length));
}
return sb.toString();
}
public static void main(final String... args) {
StringBuilder s
Solution
Simplicity
To rotate a string, only the string itself and the offset of rotation are needed.
A class is a way to store data in an orderly manner, but for the task of rotating a string, you do not need such storage.
In Java not needing a class is signalled by the use of static methods.
Also, using
The example usage is also simplified, as you do not need to build a new class:
The drawback of this approach is that it may be slower and/or use more memory, but unless you want to rotate a string tens of millions of times per second it should not matter.
To rotate a string, only the string itself and the offset of rotation are needed.
A class is a way to store data in an orderly manner, but for the task of rotating a string, you do not need such storage.
In Java not needing a class is signalled by the use of static methods.
Also, using
String.substring you can avoid the manual looping. In short all your program can be reduced to:public static String rotate(String s, int offset) {
int i = offset % s.length();
return s.substring(i) + s.substring(0, i);
}The example usage is also simplified, as you do not need to build a new class:
public static void main(final String... args) {
for (int i = 0; i < 6; i++) {
System.out.println(rotate("abcdefg", i));
}
}The drawback of this approach is that it may be slower and/or use more memory, but unless you want to rotate a string tens of millions of times per second it should not matter.
Code Snippets
public static String rotate(String s, int offset) {
int i = offset % s.length();
return s.substring(i) + s.substring(0, i);
}public static void main(final String... args) {
for (int i = 0; i < 6; i++) {
System.out.println(rotate("abcdefg", i));
}
}Context
StackExchange Code Review Q#123789, answer score: 10
Revisions (0)
No revisions yet.