HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Implement Custom Google Guava Splitter Class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
googleimplementcustomguavaclasssplitter

Problem

Problem :
Create a thread safe class Spliiter that divides strings into substrings, by recognizing a separator which can be expressed as a single character, literal string, regular expression or by using a fixed substring length.
Basic Usage Example:
Splitter.on(',').split("foo,,bar, quux") will return ["foo", "", "bar", " quux"].

You can also add configuration for trimming result or omit empty strings.
For example:
Splitter.on(',').trimResults().omitEmptyStrings().split("foo, ,bar, quux,") returns ["foo", "bar", "quux"].

The below code works but I would like to know if there are any multi-threading issue or possibility of further optimizing the code.

Thanks in advance.

Solution:

```
package com.corejavaspace.testpackage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
* The Class Splitter.
*/
public final class Splitter {
/** The omit empty. */
private boolean omitEmpty;

/** The trim. */
private boolean trim;

/** The limit. */
private int limit;

/** The split type. */
private SplitType splitType;

/**
* The Interface SplitType.
*/
public interface SplitType {
Iterable iterator(Splitter splitter, CharSequence toSplit);
}

/**
* Instantiates a new splitter.
*
* @param split type
* @param omit empty
* @param trim
* @param limit
*/
private Splitter(SplitType splitType, boolean omitEmpty, boolean trim, int limit) {
this.splitType = splitType;
this.omitEmpty = omitEmpty;
this.trim = trim;
this.limit = limit;
}

/**
* Adds configuration on which splitting will be performed
*
* @param separator
* @return splitter
*/
public static Splitter on(char separator) {
return new Splitter(new SplitType() {

@Override
public Iterable iterator(Splitter splitter, CharSequence toSplit) {

final int len = toSplit.length();

List result = new ArrayList();
int i = 0, start = 0;

Solution

Following are some observations that I found in this code -

1) There is no need of taking limit as instance variable in this class. You are not using it and your code will work without it.

2) Your split() method requires CharSequence as instance variable, but in code you are converting it into String e.g. -

result.add(((String) toSplit).substring(start, i));


So, it will break your code, if input is other than String like StringBuffer and StringBuilder

3) Below method will break if input separator contains any dangling character like "*" -

public static Splitter on(final String separator)


This is because in below line, split() method of String class accepts a regex string -

String[] result = ((String) toSplit).split(separator);


4) As per current logic, String separator is also using regex, so you can call Pattern separator method from this method -

public static Splitter on(final String separator) {
     return on(Pattern.compile(separator);
}


5) Regarding multi-threading, code looks fine and it would be able to work in multi-threaded environment.

Code Snippets

result.add(((String) toSplit).substring(start, i));
public static Splitter on(final String separator)
String[] result = ((String) toSplit).split(separator);
public static Splitter on(final String separator) {
     return on(Pattern.compile(separator);
}

Context

StackExchange Code Review Q#162298, answer score: 2

Revisions (0)

No revisions yet.