/** * Returns the authority. The default implementation returns the first citation returned by {@link * #getAuthorities()}, with the addition of identifiers from all additional authorities returned * by the above method. * * @see #getAuthorities */ @Override public synchronized Citation getAuthority() { // No need to synchronize; this is not a big deal if we create this object twice. if (authority == null) { final Citation[] authorities = getAuthorities(); switch (authorities.length) { case 0: authority = Citations.EPSG; break; case 1: authority = authorities[0]; break; default: { final CitationImpl c = new CitationImpl(authorities[0]); final Collection<Identifier> identifiers = c.getIdentifiers(); for (int i = 1; i < authorities.length; i++) { identifiers.addAll(authorities[i].getIdentifiers()); } c.freeze(); authority = c; break; } } } return authority; }
/** Tests the hash code computation. */ @Test public void testHashCode() { final CitationImpl citation = new CitationImpl(); final PropertyAccessor accessor = createPropertyAccessor(citation); int hashCode = accessor.hashCode(citation); assertEquals("Empty metadata.", 0, hashCode); final String ISBN = "Dummy ISBN"; citation.setISBN(ISBN); hashCode = accessor.hashCode(citation); assertEquals("Metadata with a single String value.", ISBN.hashCode(), hashCode); final Set<Object> set = new HashSet<Object>(); assertEquals("By Set.hashCode() contract.", 0, set.hashCode()); assertTrue(set.add(ISBN)); assertEquals("Expected Metadata.hashCode() == Set.hashCode().", set.hashCode(), hashCode); final InternationalString title = new SimpleInternationalString("Dummy title"); citation.setTitle(title); hashCode = accessor.hashCode(citation); assertEquals("Metadata with two values.", ISBN.hashCode() + title.hashCode(), hashCode); assertTrue(set.add(title)); assertEquals("Expected Metadata.hashCode() == Set.hashCode().", set.hashCode(), hashCode); assertEquals("CitationsImpl.hashCode() should delegate.", hashCode, citation.hashCode()); final Collection<Object> values = citation.asMap().values(); assertEquals(hashCode, new HashSet<Object>(values).hashCode()); assertTrue(values.containsAll(set)); assertTrue(set.containsAll(values)); }