[
https://issues.apache.org/jira/browse/COMMONSRDF-14?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14539602#comment-14539602
]
ASF GitHub Bot commented on COMMONSRDF-14:
------------------------------------------
GitHub user stain opened a pull request:
https://github.com/apache/incubator-commonsrdf/pull/13
COMMONSRDF-14 define explicit hashCode
Fixes [COMMONSRDF-14](https://issues.apache.org/jira/browse/COMMONSRDF-14)
by defining explicit `hashCode()` for `BlankNode`, `IRI`, `Literal` and
`Triple`.
For comments on this pull request, please primarily use the Jira issue
COMMONSRDF-14 thread, as GitHub email synchronization is not always working
well.
Generated javadocs:
http://stain.github.io/incubator-commonsrdf/COMMONSRDF-14/org/apache/commons/rdf/api/BlankNode.html#hashCode--
http://stain.github.io/incubator-commonsrdf/COMMONSRDF-14/org/apache/commons/rdf/api/IRI.html#hashCode--
http://stain.github.io/incubator-commonsrdf/COMMONSRDF-14/org/apache/commons/rdf/api/Literal.html#hashCode--
http://stain.github.io/incubator-commonsrdf/COMMONSRDF-14/org/apache/commons/rdf/api/Triple.html#hashCode--
The approach I suggest here favours simplicity in the documentation - so
`BlankNode` hashCode is equal to the String.hashCode() of its
`uniqueReference()` without modifications, and likewise for `IRI` with
`getUriString().hashCode()`:
> The returned hash code MUST be equal to the String.hashCode() of the
getIRIString().
.. while the compound objects are using
[Objects.hash()](http://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#hash-java.lang.Object...-):
> The returned hash code MUST be equal to the result of
Objects.hash(Object...) with the arguments getLexicalForm(), getDatatype(),
getLanguageTag().
This means that our `hashCode()` definition does not need to define in
detail how to combine these - but unfortunately `Objects.hash()` is also not
explicit about this, so clients would then be forced to use Objects.hash().
They are not required to use the actual getter methods (e.g. LiteralImpl uses
the private fields directly) - as long as the resulting hashCode is the same AS
IF this had been done. Due to the direct mappings to the uniqueReference and
IRI this means that a `TripleImpl` can take several shortcuts if it wants to.
Combined with
[COMMONSRDF-7](https://issues.apache.org/jira/browse/COMMONSRDF-7) for
immutability the hashCode would stay the same for the duration of the object
and can thus also be cached if so needed.
## Implementation notes:
[Optional.hashCode()](http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#hashCode--)
returns the hashCode of the value, or `0` if it is not present.
(Warning: GPLv3 code links below!)
In OpenJDK 8,
[Objects.hash](https://github.com/stain/jdk8u/blob/master/src/share/classes/java/util/Objects.java#L127)
calls
[Arrays.hashCode](https://github.com/stain/jdk8u/blob/master/src/share/classes/java/util/Arrays.java#L4118)
which adds the hashCode with `31` as multiplier. This is however not explicit.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/stain/incubator-commonsrdf
COMMONSRDF-14-hashCode
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/incubator-commonsrdf/pull/13.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #13
----
commit 1a1bc326c9232b6d560c14d43c5e05a1e1e640b9
Author: Stian Soiland-Reyes <[email protected]>
Date: 2015-05-06T21:49:29Z
COMMONSRDF-14 specify hashCode
commit 26da40856a86f6fdb511fdc0290b11c24518b704
Author: Stian Soiland-Reyes <[email protected]>
Date: 2015-05-12T09:06:29Z
COMMONSRDF-14 Tests for hashCode requirements
commit 24da09e94a24081478eef19969a4c4be2f7ad66b
Author: Stian Soiland-Reyes <[email protected]>
Date: 2015-05-12T09:06:43Z
COMMONSRDF-14 Update simple for hashCode definition
commit e0d8b5c6187d509004aa3be3865a0a3f2790d0cb
Author: Stian Soiland-Reyes <[email protected]>
Date: 2015-05-12T09:16:42Z
COMMONSRDF-14 Javadoc formatting
----
> Define value returned by hashCode()
> -----------------------------------
>
> Key: COMMONSRDF-14
> URL: https://issues.apache.org/jira/browse/COMMONSRDF-14
> Project: Apache Commons RDF
> Issue Type: Improvement
> Reporter: Reto Gmür
> Priority: Critical
> Fix For: 0.2
>
>
> To allow interoperability the API must define what the hashCode for Literals
> and IRIs has to be. For grounded Triples the hashcode of instances of
> different implementations should also be equals, so the API should also
> define the hashcode of a triple as a function of the hashcode of its
> subjects, predicate and object.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)