NOTE: despite the push to adopt visitor pattern across the code-base, hashing implementation for now will remain within
the owner class instead of being moved to an outside visitor for the following reasons:
Hashing of sub-structures should be cached (using mobx @computed) and hence we would have to do something like:
gethashCode(): string { returnthis.accept(newHashVisitor()) } gethashCode(): string { returnhashSubElement(this) } // for sub-structures without an `accept` method
On the other hand, we cannot remove get hashCode from sub-structure such as Tag or Stereotype because we want
to cache these, regardless of how we compute hash for bigger structure like Class or Enumeration.
The whole point of the visitor pattern is to avoid the exploring sub-structures in a structure, i.e. we should only
care about the hashCode of a class or an enumeration and trickle down those structure in the visitor; and we can do
that, but then we would still need to call hashCode for each sub-structure to make use of the cache instead of
recompute them on the spot.
A huge benefit of visitor pattern is to collocate the hashing logic in one place to reduce look-up time, but if we
use .hashCode for sub-structures, visitor pattern will actually mean more look-ups.
Also because of caching, certain really simple sub-structure would seem overkill to have hashing logic in a visitor:
NOTE: despite the push to adopt visitor pattern across the code-base, hashing implementation for now will remain within the owner class instead of being moved to an outside visitor for the following reasons:
get hashCode
from sub-structure such asTag
orStereotype
because we want to cache these, regardless of how we compute hash for bigger structure likeClass
orEnumeration
. The whole point of the visitor pattern is to avoid the exploring sub-structures in a structure, i.e. we should only care about thehashCode
of a class or an enumeration and trickle down those structure in the visitor; and we can do that, but then we would still need to callhashCode
for each sub-structure to make use of the cache instead of recompute them on the spot..hashCode
for sub-structures, visitor pattern will actually mean more look-ups.hashCode
andequals
in Java)