Skip to content

Commit 5088456

Browse files
committed
fix mutable collections in GeneSequence
1 parent 0e2e841 commit 5088456

6 files changed

Lines changed: 22 additions & 10 deletions

File tree

biojava-core/src/main/java/org/biojava/nbio/core/sequence/AccessionID.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public AccessionID(){
5353
* @param id non-null
5454
*/
5555
public AccessionID(String id) {
56-
this.id = id.trim();
57-
this.source = DataSource.LOCAL;
56+
this(id, DataSource.LOCAL);
5857
}
5958

6059
/**

biojava-core/src/main/java/org/biojava/nbio/core/sequence/GeneSequence.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,19 @@ public ExonSequence addExon(AccessionID accession, int begin, int end) throws Ex
290290
}
291291

292292
/**
293-
* Get the exons as an ArrayList
293+
* Get the exons as an ArrayList. Modifying this list will not modify the underlying collection
294294
* @return exons
295295
*/
296296
public ArrayList<ExonSequence> getExonSequences() {
297-
return exonSequenceList;
297+
return new ArrayList<>(exonSequenceList);
298298
}
299299

300300
/**
301-
* Get the introns as an ArrayList
301+
* Get the introns as an ArrayList. Modifying this list will not modify the underlying collection
302302
* @return introns
303303
*/
304304
public ArrayList<IntronSequence> getIntronSequences() {
305-
return intronSequenceList;
305+
return new ArrayList<>(intronSequenceList);
306306
}
307307

308308
/**

biojava-core/src/main/java/org/biojava/nbio/core/sequence/SequenceComparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.Comparator;
2929

3030
/**
31-
* Used to sort sequences
31+
* Used to sort sequences in ascending order of bioBegin property.
3232
* @author Scooter Willis <willishf at gmail dot com>
3333
*/
3434
public class SequenceComparator implements Comparator<AbstractSequence<?>>, Serializable{

biojava-core/src/main/java/org/biojava/nbio/core/sequence/TaxonomyID.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
/**
2727
* A sequence can be associated with a species or Taxonomy ID
2828
* @author Scooter Willis
29+
*
2930
*/
3031
public class TaxonomyID {
32+
//TODO this should implement equals and hashcode if is value object?
3133

3234

3335
private String id = null;

biojava-core/src/test/java/org/biojava/nbio/core/sequence/GeneSequenceTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.jupiter.api.Nested;
77
import org.junit.jupiter.api.Test;
88

9+
import java.util.ArrayList;
910
import java.util.List;
1011

1112
import static org.junit.jupiter.api.Assertions.*;
@@ -114,12 +115,23 @@ void addRemoveExon() throws Exception {
114115
}
115116

116117
@Test
117-
void returnedCollectionsAreMutable() throws Exception {
118+
void returnedExonCollectionsAreNotMutable() throws Exception {
118119
geneSequence = new GeneSequence(chromosomeSequence, new AccessionID("geneId"), 5,150, Strand.POSITIVE);
119120
geneSequence.addExon(new AccessionID("a"), 20, 50);
120121
List<ExonSequence> exons = geneSequence.getExonSequences();
121122
// this breaks encapsulation of the collections
122123
exons.remove(0);
123-
assertEquals(0, geneSequence.getExonSequences().size());
124+
assertEquals(1, geneSequence.getExonSequences().size());
124125
}
126+
@Test
127+
void returnedIntronCollectionsAreNotMutable() throws Exception {
128+
geneSequence = SequenceTestUtils.anyGeneSequence();
129+
geneSequence.addExon(new AccessionID("a"), 20, 50);
130+
geneSequence.addExon(new AccessionID("b"), 80, 100);
131+
geneSequence.addIntronsUsingExons();
132+
ArrayList<IntronSequence> introns = geneSequence.getIntronSequences();
133+
assertEquals(1, introns.size());
134+
introns.remove(0);
135+
assertEquals(1, geneSequence.getIntronSequences().size());
136+
}
125137
}

biojava-core/src/test/java/org/biojava/nbio/core/sequence/TranscriptSequenceTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ void CDSListIsEmpty() {
4242
}
4343

4444
@Test
45-
// whether it's -ve or +ve doesn't affect equals?
4645
void equals() {
4746
assertTrue(transcriptSeq.equals(transcriptSeq));
4847
}

0 commit comments

Comments
 (0)