2020 */
2121package org .biojava .nbio .structure .align .client ;
2222
23+ import java .util .Objects ;
24+
2325import org .biojava .nbio .structure .StructureException ;
2426
2527/**
26- * A pair for structure alignment
27- * <p>
28- * name1 is always < name2
28+ * A pair for structure alignment.
29+ * a pair is considered equal to another pair if their two poles are equal regardless to their order.
30+ * i.e. both <code>new PdbPair("1abc", "2abc").equals(new PdbPair("1abc", "2abc"))</code> and
31+ * <code>new PdbPair("1abc", "2abc").equals(new PdbPair("2abc", "1abc"))</code> are <code>true</code>.
2932 * @author Andreas Prlic
3033 *
3134 */
@@ -35,26 +38,26 @@ public class PdbPair implements Comparable<PdbPair> {
3538 private StructureName name2 ;
3639
3740 public PdbPair (String name1 , String name2 ) {
38- this (new StructureName (name1 ),new StructureName (name2 ));
41+ this (new StructureName (Objects .requireNonNull (name1 )),
42+ new StructureName (Objects .requireNonNull (name1 )));
3943 }
4044
4145 public PdbPair (StructureName name1 , StructureName name2 ) {
42- super ();
43- this .name1 = name1 ;
44- this .name2 = name2 ;
46+ this .name1 = Objects .requireNonNull (name1 );
47+ this .name2 = Objects .requireNonNull (name2 );
4548 }
4649
4750 public String getName1 () {
4851 return name1 .getIdentifier ();
4952 }
5053 public void setName1 (String name1 ) {
51- this .name1 = new StructureName (name1 );
54+ this .name1 = new StructureName (Objects . requireNonNull ( name1 ) );
5255 }
5356 public String getName2 () {
5457 return name2 .getIdentifier ();
5558 }
5659 public void setName2 (String name2 ) {
57- this .name2 = new StructureName (name2 );
60+ this .name2 = new StructureName (Objects . requireNonNull ( name2 ) );
5861 }
5962
6063 @ Override
@@ -65,10 +68,8 @@ public String toString() {
6568 @ Override
6669 public int hashCode () {
6770 final int prime = 31 ;
68- int result = 1 ;
69- result = prime * result + ((name1 == null ) ? 0 : name1 .hashCode ());
70- result = prime * result + ((name2 == null ) ? 0 : name2 .hashCode ());
71- return result ;
71+ return prime * name1 .hashCode () +
72+ prime * name2 .hashCode ();
7273 }
7374
7475 @ Override
@@ -80,23 +81,16 @@ public boolean equals(Object obj) {
8081 if (getClass () != obj .getClass ())
8182 return false ;
8283 PdbPair other = (PdbPair ) obj ;
83- if (name1 == null ) {
84- if (other .name1 != null )
85- return false ;
86- } else if (!name1 .equals (other .name1 ))
87- return false ;
88- if (name2 == null ) {
89- if (other .name2 != null )
90- return false ;
91- } else if (!name2 .equals (other .name2 ))
92- return false ;
93- return true ;
84+ return (this .name1 .equals (other .name1 ) && this .name2 .equals (other .name2 )) ||
85+ (this .name1 .equals (other .name2 ) && this .name2 .equals (other .name1 ));
9486 }
9587
9688 @ Override
9789 public int compareTo (PdbPair o ) {
90+ //make sure they are not just reverse.
9891 if ( this .equals (o ))
9992 return 0 ;
93+
10094 // Use StructureName's compareTo method
10195 int c = name1 .compareTo (o .name1 );
10296 if ( c != 0 )
@@ -119,7 +113,6 @@ public String getChainId2(){
119113 }
120114
121115 public PdbPair getReverse () {
122- PdbPair newPair = new PdbPair (name2 , name1 );
123- return newPair ;
116+ return new PdbPair (name2 , name1 );
124117 }
125118}
0 commit comments