mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Combine Joiner/Splitter #1631

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In many instances that the Splitter class is used a programmer may want to 
rejoin it or vice versa. However, instances of Splitter are completely 
separated from instances of Joiner even though many of the features overlap 
because they are inverse functions.

It would perhaps be useful to have a method in Splitter called joiner and a 
method in Joiner called splitter which would create the inverse function if 
possible, or through an exception.

Alternatively, a third class could be used that would only allow methods that 
are shared by both Joiner and Splitter such as skipNulls/omitEmptyStrings 
on(String) and on(char), but not on(Pattern), etc.

This class could be named Cleaver since this word is an auto-antonym tha means 
join/split.

Original issue reported on code.google.com by jos.k.st...@gmail.com on 7 Jan 2014 at 8:58

GoogleCodeExporter commented 9 years ago
As of Guava 16, you can make a Converter<String, Iterable<String>> that 
encapsulates your Joiner/Splitter.

Does that help?

Original comment by kak@google.com on 7 Jan 2014 at 9:03

GoogleCodeExporter commented 9 years ago
We thought about the connection between these two classes, but felt the 
correspondences between "similar" features of Joiner and Splitter were actually 
much more tenuous than they first appear.

Original comment by kevinb@google.com on 7 Jan 2014 at 9:06

GoogleCodeExporter commented 9 years ago
In response to Comment 1:

Converter would not help for two reasons.
#1 There is no easy way to turn a Splitter/Joiner into a function
#2 The point of the common functionality would be to have a common way of 
declaring that splitting/joining will be on a specified character/string 
without having to repeat it.

Original comment by jos.k.st...@gmail.com on 7 Jan 2014 at 10:36

GoogleCodeExporter commented 9 years ago
1) You'd declare them separately, like (excuse any typos):
class StringCleaver extends Converter<String, Iterable<String>> {
private static final String SEPARATOR = "!";
private static final Splitter SPLITTER = Splitter.on(SEPARATOR);
private static final Joiner JOINER = Joiner.on(SEPARATOR);
protected String doBackward(Iterable<String> input) {
  return JOINER.join(input);
}
protected Iterable<String> doForward(String input) {
  return SPLITTER.split(input);
}
}

2) see the constant above

Original comment by kak@google.com on 7 Jan 2014 at 10:50

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:10

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07