okaywit / guava-libraries

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

Add HashCodes.fromString(String) to allow roundtripping hashcodes #1460

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be useful to be able roundtrip a hashcode from a string (for example, 
if it had been persisted to disk). That is, the following test should pass:

HashCode hash1 = Hashing.sha1().hashString("Hello world");
String stringHash = hash1.toString();
HashCode hash2 = HashCodes.fromString(stringHash);
assertEquals(hash1, hash2);

Original issue reported on code.google.com by simon.m.stewart on 28 Jun 2013 at 1:28

GoogleCodeExporter commented 9 years ago

Original comment by cgdec...@gmail.com on 28 Jun 2013 at 4:25

GoogleCodeExporter commented 9 years ago
I think I implemented this as follows, but I am having some hassle with the 
reflection based testing in guava.

Any thoughts ? Is this wanted, it would be super useful to have in guava proper.

Original comment by g.j.bow...@gmail.com on 31 Jul 2013 at 1:05

Attachments:

GoogleCodeExporter commented 9 years ago
From the email discussion we had that led to this feature request being filed...

You should be able to roundtrip them with BaseEncoding.base16().lowerCase():
     HashCode hash1 = Hashing.sha1().hashString("Hello world");
     String stringHash = hash1.toString();
     HashCode hash2 = HashCodes.fromBytes(BaseEncoding.base16().lowerCase().decode(stringHash));
     assertEquals(hash1, hash2);

Whether we want to add HashCodes.fromString() is still in the research 
bucket...but thanks for voicing your positive vote for this API ;-)

Original comment by kak@google.com on 31 Jul 2013 at 2:05

GoogleCodeExporter commented 9 years ago
Humm ok, that feels a little none obvious but it works

Original comment by g.j.bow...@gmail.com on 1 Aug 2013 at 8:46

GoogleCodeExporter commented 9 years ago
I'm going to add this...still haven't decided on the name. Any opinions on 
HashCodes.fromString(String) vs. HashCodes.fromHexString(String)?

Original comment by kak@google.com on 1 Aug 2013 at 8:56

GoogleCodeExporter commented 9 years ago
I'm mixed on that, because fromHexString is more descriptive, but fromString() 
is properly parallel with toString (and I've tried to push the pattern of using 
fromString() for toString()'s inverse and some tools like Caliper even exploit 
that).

Original comment by kevinb@google.com on 1 Aug 2013 at 9:01

GoogleCodeExporter commented 9 years ago
I'm also +1 on fromString. The docs will explain it sufficiently.

Another question: do we want to be strict when de-hex'ing? Should we allow 
both: "7f8005ff0e" and "7F8005FF0E"?

Keep in mind that toString() prints in lowercase.

Original comment by kak@google.com on 1 Aug 2013 at 9:39

GoogleCodeExporter commented 9 years ago
Going w/ fromString() and strict parsing for now.

Original comment by kak@google.com on 6 Aug 2013 at 6:17

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

GoogleCodeExporter commented 9 years ago

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