I would use lazy initialization. Since the hash should not change once the
object is created you can use:
private Integer hash = null;
then in hashCode()
{
retrurn (hash == null)?calculateHash():hash;
}
private Integer calculcateHash()
{
// your hash calculation here
this.hash = hashResult;
return this.hash;
}
Since the hash does not change calculating it multiple times will not
result in an error thus synchronization is not required.
On Tue, Nov 4, 2014 at 9:28 AM, Andy Seaborne <[email protected]> wrote:
> On 04/11/14 08:19, Claude Warren wrote:
>
>> Andy,
>>
>> You might consider caching the hash code once it is calculated. I assume
>> you use this object in hash maps or hash sets where the hash code needs to
>> be retrieve many times - O(log(n)? per operation. In this case caching
>> may show a performance improvement.
>>
>> Claude
>>
>
> What is the best idiom for doing that? I've done it before caching to a
> long so a MAX_VALUE is outside the range of a hash
>
> This class is not performance critical. It will be used briefly when
> creating TDB datasets. Actually, it's unlikely to be store in hash XYZs.
> Except for the (short <=4) arrays, the operations are quite cheap.
>
> Andy
>
>
>
>> On Mon, Nov 3, 2014 at 8:24 PM, <[email protected]> wrote:
>>
>> Repository: jena
>>> Updated Branches:
>>> refs/heads/master 1f2642ad1 -> be8f0aea6
>>>
>>>
>>> hashCode and .equals (auto-generated)
>>>
>>> Project: http://git-wip-us.apache.org/repos/asf/jena/repo
>>> Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/be8f0aea
>>> Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/be8f0aea
>>> Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/be8f0aea
>>>
>>> Branch: refs/heads/master
>>> Commit: be8f0aea68df5caecc287ca612d7af00231d553f
>>> Parents: 1f2642a
>>> Author: Andy Seaborne <[email protected]>
>>> Authored: Mon Nov 3 20:24:30 2014 +0000
>>> Committer: Andy Seaborne <[email protected]>
>>> Committed: Mon Nov 3 20:24:30 2014 +0000
>>>
>>> ----------------------------------------------------------------------
>>> .../com/hp/hpl/jena/tdb/setup/StoreParams.java | 99
>>> ++++++++++++++++++++
>>> 1 file changed, 99 insertions(+)
>>> ----------------------------------------------------------------------
>>>
>>>
>>>
>>> http://git-wip-us.apache.org/repos/asf/jena/blob/be8f0aea/
>>> jena-tdb/src/main/java/com/hp/hpl/jena/tdb/setup/StoreParams.java
>>> ----------------------------------------------------------------------
>>> diff --git
>>> a/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/setup/StoreParams.java
>>> b/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/setup/StoreParams.java
>>> index 63fdef5..7c1829b 100644
>>> --- a/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/setup/StoreParams.java
>>> +++ b/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/setup/StoreParams.java
>>> @@ -18,6 +18,8 @@
>>>
>>> package com.hp.hpl.jena.tdb.setup;
>>>
>>> +import java.util.Arrays ;
>>> +
>>> import org.apache.jena.atlas.lib.StrUtils ;
>>>
>>> import com.hp.hpl.jena.tdb.base.block.FileMode ;
>>> @@ -207,5 +209,102 @@ public class StoreParams implements IndexParams
>>> buff.append(String.format("%-20s %s\n", name, value)) ;
>>> }
>>>
>>> + @Override
>>> + public int hashCode() {
>>> + final int prime = 31 ;
>>> + int result = 1 ;
>>> + result = prime * result + Node2NodeIdCacheSize ;
>>> + result = prime * result + NodeId2NodeCacheSize ;
>>> + result = prime * result + NodeMissCacheSize ;
>>> + result = prime * result + blockReadCacheSize ;
>>> + result = prime * result + blockSize ;
>>> + result = prime * result + blockWriteCacheSize ;
>>> + result = prime * result + ((fileMode == null) ? 0 :
>>> fileMode.hashCode()) ;
>>> + result = prime * result + ((indexId2Node == null) ? 0 :
>>> indexId2Node.hashCode()) ;
>>> + result = prime * result + ((indexNode2Id == null) ? 0 :
>>> indexNode2Id.hashCode()) ;
>>> + result = prime * result + ((indexPrefix == null) ? 0 :
>>> indexPrefix.hashCode()) ;
>>> + result = prime * result + ((prefixId2Node == null) ? 0 :
>>> prefixId2Node.hashCode()) ;
>>> + result = prime * result + Arrays.hashCode(prefixIndexes) ;
>>> + result = prime * result + ((prefixNode2Id == null) ? 0 :
>>> prefixNode2Id.hashCode()) ;
>>> + result = prime * result + ((primaryIndexPrefix == null) ? 0 :
>>> primaryIndexPrefix.hashCode()) ;
>>> + result = prime * result + ((primaryIndexQuads == null) ? 0 :
>>> primaryIndexQuads.hashCode()) ;
>>> + result = prime * result + ((primaryIndexTriples == null) ? 0 :
>>> primaryIndexTriples.hashCode()) ;
>>> + result = prime * result + Arrays.hashCode(quadIndexes) ;
>>> + result = prime * result + Arrays.hashCode(tripleIndexes) ;
>>> + return result ;
>>> + }
>>> +
>>> + @Override
>>> + public boolean equals(Object obj) {
>>> + if ( this == obj )
>>> + return true ;
>>> + if ( obj == null )
>>> + return false ;
>>> + if ( getClass() != obj.getClass() )
>>> + return false ;
>>> + StoreParams other = (StoreParams)obj ;
>>> + if ( Node2NodeIdCacheSize != other.Node2NodeIdCacheSize )
>>> + return false ;
>>> + if ( NodeId2NodeCacheSize != other.NodeId2NodeCacheSize )
>>> + return false ;
>>> + if ( NodeMissCacheSize != other.NodeMissCacheSize )
>>> + return false ;
>>> + if ( blockReadCacheSize != other.blockReadCacheSize )
>>> + return false ;
>>> + if ( blockSize != other.blockSize )
>>> + return false ;
>>> + if ( blockWriteCacheSize != other.blockWriteCacheSize )
>>> + return false ;
>>> + if ( fileMode != other.fileMode )
>>> + return false ;
>>> + if ( indexId2Node == null ) {
>>> + if ( other.indexId2Node != null )
>>> + return false ;
>>> + } else if ( !indexId2Node.equals(other.indexId2Node) )
>>> + return false ;
>>> + if ( indexNode2Id == null ) {
>>> + if ( other.indexNode2Id != null )
>>> + return false ;
>>> + } else if ( !indexNode2Id.equals(other.indexNode2Id) )
>>> + return false ;
>>> + if ( indexPrefix == null ) {
>>> + if ( other.indexPrefix != null )
>>> + return false ;
>>> + } else if ( !indexPrefix.equals(other.indexPrefix) )
>>> + return false ;
>>> + if ( prefixId2Node == null ) {
>>> + if ( other.prefixId2Node != null )
>>> + return false ;
>>> + } else if ( !prefixId2Node.equals(other.prefixId2Node) )
>>> + return false ;
>>> + if ( !Arrays.equals(prefixIndexes, other.prefixIndexes) )
>>> + return false ;
>>> + if ( prefixNode2Id == null ) {
>>> + if ( other.prefixNode2Id != null )
>>> + return false ;
>>> + } else if ( !prefixNode2Id.equals(other.prefixNode2Id) )
>>> + return false ;
>>> + if ( primaryIndexPrefix == null ) {
>>> + if ( other.primaryIndexPrefix != null )
>>> + return false ;
>>> + } else if ( !primaryIndexPrefix.equals(other.primaryIndexPrefix)
>>> )
>>> + return false ;
>>> + if ( primaryIndexQuads == null ) {
>>> + if ( other.primaryIndexQuads != null )
>>> + return false ;
>>> + } else if ( !primaryIndexQuads.equals(other.primaryIndexQuads)
>>> )
>>> + return false ;
>>> + if ( primaryIndexTriples == null ) {
>>> + if ( other.primaryIndexTriples != null )
>>> + return false ;
>>> + } else if (
>>> !primaryIndexTriples.equals(other.primaryIndexTriples) )
>>> + return false ;
>>> + if ( !Arrays.equals(quadIndexes, other.quadIndexes) )
>>> + return false ;
>>> + if ( !Arrays.equals(tripleIndexes, other.tripleIndexes) )
>>> + return false ;
>>> + return true ;
>>> + }
>>> +
>>> }
>>>
>>>
>>>
>>>
>>
>>
>
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren