applet gsdi
[jalview.git] / forester / java / src / org / forester / sequence / BasicSequence.java
index c14277c..4a4662a 100644 (file)
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.sequence;
 
+import org.forester.util.ForesterUtil;
+
 public class BasicSequence implements Sequence {
 
     private final char[] _mol_sequence;
@@ -33,6 +35,12 @@ public class BasicSequence implements Sequence {
     private final TYPE   _type;
 
     private BasicSequence( final String identifier, final String mol_sequence, final TYPE type ) {
+        if ( ForesterUtil.isEmpty( identifier ) ) {
+            throw new IllegalArgumentException( "identifier of sequence cannot be empty" );
+        }
+        if ( ForesterUtil.isEmpty( mol_sequence ) ) {
+            throw new IllegalArgumentException( "molecular sequence cannot be empty" );
+        }
         _mol_sequence = mol_sequence.toCharArray();
         _identifier = identifier;
         _type = type;
@@ -40,6 +48,12 @@ public class BasicSequence implements Sequence {
 
     // Only use if you know what you are doing!
     public BasicSequence( final String identifier, final char[] mol_sequence, final TYPE type ) {
+        if ( ForesterUtil.isEmpty( identifier ) ) {
+            throw new IllegalArgumentException( "identifier of sequence cannot be empty" );
+        }
+        if ( ( mol_sequence == null ) || ( mol_sequence.length < 1 ) ) {
+            throw new IllegalArgumentException( "molecular sequence cannot be empty" );
+        }
         _mol_sequence = mol_sequence;
         _identifier = identifier;
         _type = type;
@@ -73,8 +87,8 @@ public class BasicSequence implements Sequence {
     @Override
     public int getNumberOfGapResidues() {
         int gaps = 0;
-        for( int i = 0; i < _mol_sequence.length; ++i ) {
-            if ( _mol_sequence[ i ] == GAP ) {
+        for( final char element : _mol_sequence ) {
+            if ( element == GAP ) {
                 ++gaps;
             }
         }
@@ -82,11 +96,31 @@ public class BasicSequence implements Sequence {
     }
 
     @Override
+    public boolean equals( final Object obj ) {
+        if ( obj == null ) {
+            return false;
+        }
+        if ( obj.getClass() != getClass() ) {
+            return false;
+        }
+        final Sequence other = ( Sequence ) obj;
+        if ( getMolecularSequenceAsString().equals( other.getMolecularSequenceAsString() ) ) {
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return getMolecularSequenceAsString().hashCode();
+    }
+
+    @Override
     public String toString() {
         final StringBuffer sb = new StringBuffer();
         sb.append( _identifier.toString() );
-        sb.append( " " );
-        sb.append( new String( _mol_sequence ) );
+        sb.append( ": " );
+        sb.append( getMolecularSequenceAsString() );
         return sb.toString();
     }
 
@@ -112,4 +146,9 @@ public class BasicSequence implements Sequence {
         return new BasicSequence( identifier, mol_sequence.toUpperCase().replaceAll( "\\.", GAP_STR )
                 .replaceAll( RNA_REGEXP, Character.toString( UNSPECIFIED_NUC ) ), TYPE.RNA );
     }
+
+    @Override
+    public String getMolecularSequenceAsString() {
+        return new String( getMolecularSequence() );
+    }
 }