DaveAKing / guava-libraries

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

Add Converter for booleans #1710

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
All of the primitives have stock Converter implementations, except for 
booleans. Such a converter should be added.

Original issue reported on code.google.com by rdicr...@lapis.com on 31 Mar 2014 at 7:36

GoogleCodeExporter commented 9 years ago
This question came up internally, so I'm just copying what Kevin wrote earlier:

"I think Boolean is legitimately a special case, because of the greater variety 
of different rules different users will want or even expect, plus the fact that 
it's pretty easy to hand-roll whatever exact rule you want.
If we do add Booleans.stringConverter() (which we're not exactly fundamentally 
opposed to, it's just that even things this small take work), I think the best 
thing it can do is convert only the exact strings "true" and "false", nothing 
else. Any surprises will just prompt users to write the particular converter 
they really want (any two of which I suspect will be unlikely to be exactly the 
same)."

I also looked a handful of Converter<String, Boolean> implementations that have 
been checked into the Google code base and found:
4 use Boolean.valueOf() or parseBoolean()
2 convert "", "no", "false", "0", "null" (ignoring case) to false
1 converts "Yes" (ignoring case) to true, everything else converst to false

What semantics would you expect Booleans.stringConverter() to have?

Original comment by kak@google.com on 31 Mar 2014 at 7:41

GoogleCodeExporter commented 9 years ago
I would expect the same semantics as Boolean.valueOf() / parseBoolean(); my 
implementation uses the latter.

I agree that there are a lot of different ways to represent a boolean, but 
those methods are baked into Java and thus are a "standard" conversion in my 
opinion. So it would make sense to have a stock converter for the standard 
behavior, and then non-standard behavior can be covered by custom converters.

Original comment by rdicr...@lapis.com on 31 Mar 2014 at 8:37

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:09

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