chen870647924 / guava-libraries

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

MapSplitter doesn't trim keys and values as expected #870

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A test like this should pass:
    @Test
    public void mapSplitter() {
        Map<String, String> map = Splitter.on(",").omitEmptyStrings().trimResults().withKeyValueSeparator("->").split("cows-> big, sheep -> annoying, geese -> welcome");
        assertEquals("welcome", map.get("geese"));
        assertEquals("big", map.get("cows"));
    }

Right now the map is from "geese " to " welcome" rather than "geese" to 
"welcome"

Original issue reported on code.google.com by nik9...@gmail.com on 17 Jan 2012 at 9:04

GoogleCodeExporter commented 9 years ago
The omitEmptyStrings part of the above is sort of meaningless and can be 
removed.

Original comment by nik9...@gmail.com on 17 Jan 2012 at 9:17

GoogleCodeExporter commented 9 years ago
The first #trimResults only applies to the key splitter. You can get what you 
want with:
    Map<String, String> map = Splitter
        .on(",")
        .omitEmptyStrings()
        .trimResults()
        .withKeyValueSeparator(Splitter.on("->").trimResults())
        .split("cows-> big, sheep -> annoying, geese -> welcome");

Perhaps we need to make this more clear in the javadocs somehow though?

Original comment by kak@google.com on 17 Jan 2012 at 9:19

GoogleCodeExporter commented 9 years ago

Original comment by kak@google.com on 17 Jan 2012 at 9:19

GoogleCodeExporter commented 9 years ago
Its more like the trim works on the splitter that splits the entries - the one 
that uses the ",".  Would it be ok to provide the trim behavior to the keyvalue 
splitter?  Something like:

   @CheckReturnValue
   @Beta
   public MapSplitter withKeyValueSeparator(String separator) {
-    return withKeyValueSeparator(on(separator));
+    return withKeyValueSeparator(on(separator).trimResults(trimmer));
   }

Original comment by nik9...@gmail.com on 17 Jan 2012 at 9:27

GoogleCodeExporter commented 9 years ago
Unfortunately, this would leave the user no apparent way to specify when they 
*don't* want that trimming. I don't know if there's going to be a good solution 
to this problem. :-(

Original comment by kevinb@google.com on 18 Jan 2012 at 3:06

GoogleCodeExporter commented 9 years ago
Wouldn't the withKeyValueSeparator(Splitter) syntax do that?  Those that want 
the current behavior I'm seeing could do
Splitter.on(",").trimResults().withKeyValueSerarator(Splitter.on("->")).split(""
)

Something like that'd still require a note in the javadoc for the String 
version of withKeyValueSeparator, but I think it'd be overall more intuitive.

Original comment by nik9...@gmail.com on 18 Jan 2012 at 3:30

GoogleCodeExporter commented 9 years ago

Original comment by wasserman.louis on 16 Feb 2012 at 6:27

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago
We are starting to wonder if it was a mistake to implement MapSplitter in the 
way we did (start with a Splitter, then get a MapSplitter from it).  A parallel 
API probably would have been a lot less confusing.  I am not sure we how 
feasible it is to fix this behavior now.

Original comment by kevinb@google.com on 16 Feb 2012 at 9:52

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:51

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 22 Jun 2012 at 6:16

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

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

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

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

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