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 java.util.ArrayList;
30 import java.util.List;
31
32 import org.forester.go.GoId;
33 import org.forester.util.ForesterUtil;
34
35 public class DomainId implements Comparable<DomainId> {
36
37     final private String _id;
38     private List<GoId>   _go_ids;
39
40     public DomainId( final String id ) {
41         if ( ForesterUtil.isEmpty( id ) ) {
42             throw new IllegalArgumentException( "attempt to create domain id from empty or null string" );
43         }
44         _id = id.trim();
45         if ( _id.indexOf( ' ' ) > -1 ) {
46             throw new IllegalArgumentException( "attempt to create domain id from string containing one ore more spaces ["
47                     + _id + "]" );
48         }
49         else if ( _id.indexOf( BinaryDomainCombination.SEPARATOR ) > -1 ) {
50             throw new IllegalArgumentException( "attempt to create domain id from string containing the separator character ["
51                     + BinaryDomainCombination.SEPARATOR + "] for domain combinations [" + _id + "]" );
52         }
53         setGoIds( null );
54     }
55
56     public void addGoId( final GoId go_id ) {
57         if ( getGoIds() == null ) {
58             setGoIds( new ArrayList<GoId>() );
59         }
60         getGoIds().add( go_id );
61     }
62
63     @Override
64     public int compareTo( final DomainId domain_id ) {
65         if ( this == domain_id ) {
66             return 0;
67         }
68         return getId().toLowerCase().compareTo( domain_id.getId().toLowerCase() );
69     }
70
71     @Override
72     public boolean equals( final Object o ) {
73         if ( this == o ) {
74             return true;
75         }
76         else if ( o == null ) {
77             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
78         }
79         else if ( o.getClass() != this.getClass() ) {
80             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
81                     + o.getClass() + "]" );
82         }
83         else {
84             return getId().equals( ( ( DomainId ) o ).getId() );
85         }
86     }
87
88     public GoId getGoId( final int i ) {
89         return getGoIds().get( i );
90     }
91
92     // Note.
93     // The fact that equals and compareTo do not behave the same in cases where ids only differ by their case
94     // is not ideal. From Sun regarding Interface SortedSet<E>:
95     // "Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided)
96     // must be consistent with equals if the sorted set is to correctly implement the Set interface.
97     // (See the Comparable interface or Comparator interface for a precise definition of consistent 
98     // with equals.) This is so because the Set interface is defined in terms of the equals  operation,
99     // but a sorted set performs all element comparisons using its compareTo (or compare) method, 
100     // so two elements that are deemed equal by this method are, from the standpoint of the sorted set,
101     // equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; 
102     // it just fails to obey the general contract of the Set interface."
103     public List<GoId> getGoIds() {
104         return _go_ids;
105     }
106
107     public String getId() {
108         return _id;
109     }
110
111     public int getNumberOfGoIds() {
112         if ( getGoIds() == null ) {
113             return 0;
114         }
115         return getGoIds().size();
116     }
117
118     @Override
119     public int hashCode() {
120         return getId().hashCode();
121     }
122
123     private void setGoIds( final List<GoId> go_ids ) {
124         _go_ids = go_ids;
125     }
126
127     @Override
128     public String toString() {
129         return getId();
130     }
131 }