2015-05-07 204 views
1

我有一些番石榴漏斗的问题,我读了这篇文章https://code.google.com/p/guava-libraries/wiki/HashingExplained和其他人,但我不知道如何我可以使用漏斗,当我的类不仅包含原始类型。谷歌番石榴散列

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 
    } 
}; 

后,我需要做这样的

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

PrimitiveSink类没有putObject方法,只有散列器类有它。 我可以将myObject转换为字节数组并使用putBytes方法,但可能有人知道更好的方法。

回答

2

你是对的:目前,不可能仅遵循API链接的方法来完成它。我看到你有一个myObjectFunnel。那么为什么不使用它?

关于什么:

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); 
    } 
}; 
+0

哦,真的很不错的主意,谢谢。 – Dominic