2

I have some problem with guava funnel , I read this article https://code.google.com/p/guava-libraries/wiki/HashingExplained and others , but I don't know how I can use funnel when my class contains not only primitive types.

Funnel<Person> personFunnel = new Funnel<Person>() {
  @Override
  public void funnel(Person person, PrimitiveSink into) {
    into
      .putInt(person.id)
      .putString(person.firstName, Charsets.UTF_8)
      .putString(person.lastName, Charsets.UTF_8)
      .putInt(birthYear)
      //.putObject(myObject,myObjectFunnel);I want to do something like this
  }
};

after I need to do like this

HashFunction hf = Hashing.md5();
HashCode hc = hf.newHasher()
       .putObject(person, personFunnel)
       .hash();

PrimitiveSink class hasn't putObject method , only Hasher class has it. I can transform myObject to byte array and use putBytes method , but probably somebody knows better approach.

Dominic
  • 47
  • 1
  • 5

1 Answers1

3

You're right: at the moment, it's not possible to do it following the API chained methods only.

But I see that you have a myObjectFunnel. So why not use it?

What about:

Funnel<Person> personFunnel = new Funnel<Person>() {
  @Override
  public void funnel(Person person, PrimitiveSink into) {
    into
      .putInt(person.id)
      .putString(person.firstName, Charsets.UTF_8)
      .putString(person.lastName, Charsets.UTF_8)
      .putInt(birthYear);
    myObjectFunnel.funnel(myObject, into);
  }
};
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137