Project

General

Profile

« Previous | Next » 

Revision 7a72a0fe

Added by Andreas Kohlbecker almost 6 years ago

ref #7386 reducing redundancy in name relationship handling methods, adding methods for general use

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/name/NameRelationshipType.java
216 216
		}
217 217
	}
218 218

  
219
	/**
220
	 * @param type
221
	 * @return
222
	 */
223
	@Transient
224
	protected boolean isRelationshipType(NameRelationshipType type) {
225
	    if (type == null){
226
	        throw new IllegalStateException("NameRelationships have not been initialized yet. Please initialize DefinedTerms first");
227
	    }
228
	    return this.equals(type);
229
	}
230

  
219 231
	@Transient
220 232
	public boolean isBasionymRelation(){
221
		if (BASIONYM() == null){
222
			throw new IllegalStateException("NameRelationships have not been initialized yet. Please initialize DefinedTerms first");
223
		}
224
		return this.equals(BASIONYM());
233
        return isRelationshipType(BASIONYM());
225 234
	}
226 235

  
227 236
	@Transient
228 237
	public boolean isReplacedSynonymRelation(){
229
		if (REPLACED_SYNONYM() == null){
230
			throw new IllegalStateException("NameRelationships have not been initialized yet. Please initialize DefinedTerms first");
231
		}
232
		return this.equals(REPLACED_SYNONYM());
238
        return isRelationshipType(REPLACED_SYNONYM());
233 239
	}
234 240

  
235 241

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/name/TaxonName.java
67 67
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
68 68
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
69 69
import eu.etaxonomy.cdm.model.common.CdmBase;
70
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
71 70
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
72 71
import eu.etaxonomy.cdm.model.common.IParsable;
73 72
import eu.etaxonomy.cdm.model.common.IRelated;
74 73
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
75 74
import eu.etaxonomy.cdm.model.common.RelationshipBase;
76
import eu.etaxonomy.cdm.model.common.TermType;
77
import eu.etaxonomy.cdm.model.common.TermVocabulary;
75
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
78 76
import eu.etaxonomy.cdm.model.description.IDescribable;
79 77
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
80 78
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
......
1781 1779
        }
1782 1780
    }
1783 1781

  
1782
    public void removeRelationWithTaxonName(TaxonName otherTaxonName, Direction direction, NameRelationshipType type) {
1783

  
1784
        for(NameRelationship nameRelationship : relationsWithThisName(direction)) {
1785
            if (direction.equals(Direction.relatedFrom) && nameRelationship.getToName().equals(otherTaxonName) ||
1786
                    direction.equals(Direction.relatedTo) && nameRelationship.getFromName().equals(otherTaxonName)) {
1787
                this.removeNameRelationship(nameRelationship);
1788
            }
1789
        }
1790
    }
1791

  
1784 1792

  
1785 1793
    /**
1786 1794
     * If relation is of type NameRelationship, addNameRelationship is called;
......
2035 2043
    @Override
2036 2044
    @Transient
2037 2045
    public Set<TaxonName> getBasionyms(){
2046

  
2047
        return getRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM());
2048
    }
2049

  
2050
    /**
2051
     *
2052
     * @param direction
2053
     * @param type
2054
     * @return
2055
     */
2056
    public Set<TaxonName> getRelatedNames(Direction direction, NameRelationshipType type) {
2057

  
2058
        return getRelatedNames(relationsWithThisName(direction), NameRelationshipType.BASIONYM());
2059
    }
2060

  
2061
    /**
2062
     * @param rels
2063
     * @param type
2064
     * @return
2065
     */
2066
    private Set<TaxonName> getRelatedNames(Set<NameRelationship> rels, NameRelationshipType type) {
2038 2067
        Set<TaxonName> result = new HashSet<>();
2039
        Set<NameRelationship> rels = this.getRelationsToThisName();
2040 2068
        for (NameRelationship rel : rels){
2041
            if (rel.getType()!= null && rel.getType().isBasionymRelation()){
2069
            if (rel.getType()!= null && rel.getType().isRelationshipType(type)){
2042 2070
                TaxonName basionym = rel.getFromName();
2043 2071
                result.add(basionym);
2044 2072
            }
......
2091 2119
    @Override
2092 2120
    @Transient
2093 2121
    public Set<TaxonName> getReplacedSynonyms(){
2094
        Set<TaxonName> result = new HashSet<>();
2095
        Set<NameRelationship> rels = this.getRelationsToThisName();
2096
        for (NameRelationship rel : rels){
2097
            if (rel.getType().isReplacedSynonymRelation()){
2098
                TaxonName replacedSynonym = rel.getFromName();
2099
                result.add(replacedSynonym);
2100
            }
2101
        }
2102
        return result;
2122

  
2123
        return getRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM());
2103 2124
    }
2104 2125

  
2105 2126
    /**
......
2133 2154
     */
2134 2155
    @Override
2135 2156
    public void removeBasionyms(){
2157
        removeNameRelations(Direction.relatedTo, NameRelationshipType.BASIONYM());
2158
    }
2159

  
2160

  
2161
    /**
2162
     * Removes all {@link NameRelationship relationships} of the given <code>type</code> from the set of
2163
     * relations in the specified <code>direction</code> direction wich are related from or to this
2164
     * <i>this</i> taxon name. The same relationship will be removed from the set of
2165
     * reverse relations of the other taxon name.
2166
     *
2167
     * @param direction
2168
     * @param type
2169
     */
2170
    public void removeNameRelations(Direction direction, NameRelationshipType type) {
2171
        Set<NameRelationship> relationsWithThisName = relationsWithThisName(direction);
2136 2172
        Set<NameRelationship> removeRelations = new HashSet<NameRelationship>();
2137
        for (NameRelationship nameRelation : this.getRelationsToThisName()){
2138
            if (nameRelation.getType().isBasionymRelation()){
2173
        for (NameRelationship nameRelation : relationsWithThisName){
2174
            if (nameRelation.getType().isRelationshipType(type)){
2139 2175
                removeRelations.add(nameRelation);
2140 2176
            }
2141 2177
        }
......
2147 2183
        }
2148 2184
    }
2149 2185

  
2186

  
2187
    /**
2188
     * @param direction
2189
     * @return
2190
     */
2191
    protected Set<NameRelationship> relationsWithThisName(Direction direction) {
2192

  
2193
        switch(direction) {
2194
            case relatedTo:
2195
                return this.getRelationsToThisName();
2196
            case relatedFrom:
2197
                return this.getRelationsFromThisName();
2198
            default: throw new RuntimeException("invalid Direction:" + direction);
2199
        }
2200
    }
2201

  
2150 2202
    /**
2151 2203
     * Returns the taxonomic {@link Rank rank} of <i>this</i> taxon name.
2152 2204
     *
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/name/TaxonNameTest.java
24 24

  
25 25
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
26 26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
27 28
import eu.etaxonomy.cdm.model.description.Feature;
28 29
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
29 30
import eu.etaxonomy.cdm.model.description.TextData;
......
663 664
	@Test
664 665
	public void testRemoveBasionyms(){
665 666
		TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(null);
666
		TaxonName basionym = TaxonNameFactory.NewBotanicalInstance(null);
667
		TaxonName name3 = TaxonNameFactory.NewBotanicalInstance(null);
667
		TaxonName basionym1 = TaxonNameFactory.NewBotanicalInstance(null);
668
		TaxonName basionym2 = TaxonNameFactory.NewBotanicalInstance(null);
668 669

  
669
		name1.addBasionym(basionym);
670
		name1.addBasionym(basionym1);
670 671
		assertEquals(1, name1.getBasionyms().size());
671
		name1.addBasionym(name3);
672
		name1.addBasionym(basionym2);
672 673
		assertEquals(2, name1.getBasionyms().size());
673 674
		name1.removeBasionyms();
674 675
		assertEquals(0, name1.getBasionyms().size());
675 676
	}
676 677

  
678

  
679
	@Test
680
    public void testRemoveRelationWithTaxonName(){
681
	    TaxonName name1 = TaxonNameFactory.NewBotanicalInstance(null);
682
        TaxonName basionym1 = TaxonNameFactory.NewBotanicalInstance(null);
683
        TaxonName basionym2 = TaxonNameFactory.NewBotanicalInstance(null);
684

  
685
        name1.addBasionym(basionym1);
686
        assertEquals(1, name1.getBasionyms().size());
687
        name1.addBasionym(basionym2);
688
        assertEquals(2, name1.getBasionyms().size());
689
        name1.removeRelationWithTaxonName(basionym1, Direction.relatedTo, NameRelationshipType.BASIONYM());
690
        assertEquals(1, name1.getBasionyms().size());
691
    }
692

  
693

  
677 694
	/**
678 695
	 * Test method for {@link eu.etaxonomy.cdm.model.name.TaxonName#isSupraGeneric()}.
679 696
	 */

Also available in: Unified diff