inprogress
[jalview.git] / forester / java / src / org / forester / protein / DomainId.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.protein;
28
29 import org.forester.util.ForesterUtil;
30
31 public class DomainId implements Comparable<DomainId> {
32
33     final private String _id;
34
35     //  private List<GoId>   _go_ids;
36     private DomainId( final String id ) {
37         if ( ForesterUtil.isEmpty( id ) ) {
38             throw new IllegalArgumentException( "attempt to create domain id from empty or null string" );
39         }
40         _id = id.trim();
41         if ( _id.indexOf( ' ' ) > -1 ) {
42             throw new IllegalArgumentException( "attempt to create domain id from string containing one ore more spaces ["
43                     + _id + "]" );
44         }
45         else if ( _id.indexOf( BinaryDomainCombination.SEPARATOR ) > -1 ) {
46             throw new IllegalArgumentException( "attempt to create domain id from string containing the separator character ["
47                     + BinaryDomainCombination.SEPARATOR + "] for domain combinations [" + _id + "]" );
48         }
49         ///////////////////////////// //      setGoIds( null );
50     }
51
52     //  public void addGoId( final GoId go_id ) {
53     //       if ( getGoIds() == null ) {
54     //           setGoIds( new ArrayList<GoId>() );
55     //      }
56     //      getGoIds().add( go_id );
57     //  }
58     @Override
59     public int compareTo( final DomainId domain_id ) {
60         if ( this == domain_id ) {
61             return 0;
62         }
63         return getId().toLowerCase().compareTo( domain_id.getId().toLowerCase() );
64     }
65
66     @Override
67     public boolean equals( final Object o ) {
68         if ( this == o ) {
69             return true;
70         }
71         else if ( o == null ) {
72             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
73         }
74         else if ( o.getClass() != this.getClass() ) {
75             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
76                     + o.getClass() + "]" );
77         }
78         else {
79             return getId().equals( ( ( DomainId ) o ).getId() );
80         }
81     }
82
83     // public GoId getGoId( final int i ) {
84     //     return getGoIds().get( i );
85     //}
86     // Note.
87     // The fact that equals and compareTo do not behave the same in cases where ids only differ by their case
88     // is not ideal. From Sun regarding Interface SortedSet<E>:
89     // "Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided)
90     // must be consistent with equals if the sorted set is to correctly implement the Set interface.
91     // (See the Comparable interface or Comparator interface for a precise definition of consistent 
92     // with equals.) This is so because the Set interface is defined in terms of the equals  operation,
93     // but a sorted set performs all element comparisons using its compareTo (or compare) method, 
94     // so two elements that are deemed equal by this method are, from the standpoint of the sorted set,
95     // equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; 
96     // it just fails to obey the general contract of the Set interface."
97     // public List<GoId> getGoIds() {
98     //     return _go_ids;
99     // }
100     private String getId() {
101         return _id;
102     }
103
104     // public int getNumberOfGoIds() {
105     //     if ( getGoIds() == null ) {
106     //         return 0;
107     //     }
108     //     return getGoIds().size();
109     // }
110     @Override
111     public int hashCode() {
112         return getId().hashCode();
113     }
114
115     // private void setGoIds( final List<GoId> go_ids ) {
116     //     _go_ids = go_ids;
117     // }
118     @Override
119     public String toString() {
120         return getId();
121     }
122 }