new UI types?
[jalview.git] / forester / java / src / org / forester / go / BasicGoRelationship.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.go;
27
28 public class BasicGoRelationship implements GoRelationship {
29
30     final Type _type;
31     final GoId _go_id;
32
33     public BasicGoRelationship( final String s ) {
34         final String[] sa = s.split( " " );
35         if ( sa.length != 2 ) {
36             throw new IllegalArgumentException( "unexpected format for GO relationship: " + s );
37         }
38         final String type = sa[ 0 ].trim();
39         final String go_id = sa[ 1 ].trim();
40         if ( type.toLowerCase().equals( PART_OF_STR ) ) {
41             _type = Type.PART_OF;
42         }
43         else if ( type.toLowerCase().equals( REGULATES_STR ) ) {
44             _type = Type.REGULATES;
45         }
46         else if ( type.toLowerCase().equals( NEGATIVELY_REGULATES_STR ) ) {
47             _type = Type.NEGATIVELY_REGULATES;
48         }
49         else if ( type.toLowerCase().equals( POSITIVELY_REGULATES_STR ) ) {
50             _type = Type.POSITIVELY_REGULATES;
51         }
52         else {
53             throw new IllegalArgumentException( "unknown GO relationship type: " + type );
54         }
55         _go_id = new GoId( go_id );
56     }
57
58     public BasicGoRelationship( final String type, final String go_id ) {
59         if ( type.toLowerCase().equals( PART_OF_STR ) ) {
60             _type = Type.PART_OF;
61         }
62         else {
63             throw new IllegalArgumentException( "unknown GO relationship type: " + type );
64         }
65         _go_id = new GoId( go_id );
66     }
67
68     public BasicGoRelationship( final Type type, final GoId go_id ) {
69         _type = type;
70         _go_id = go_id;
71     }
72
73     @Override
74     public int compareTo( final GoRelationship rel ) {
75         return getGoId().compareTo( rel.getGoId() );
76     }
77
78     /**
79      * Based on value and type.
80      * 
81      * 
82      */
83     @Override
84     public boolean equals( final Object o ) {
85         if ( this == o ) {
86             return true;
87         }
88         else if ( o == null ) {
89             throw new IllegalArgumentException( "attempt to check go relationship equality to null" );
90         }
91         else if ( o.getClass() != this.getClass() ) {
92             throw new IllegalArgumentException( "attempt to check go relationship equality to " + o + " ["
93                     + o.getClass() + "]" );
94         }
95         else {
96             return getType().equals( ( ( GoRelationship ) o ).getType() )
97                     && getGoId().equals( ( ( GoRelationship ) o ).getGoId() );
98         }
99     }
100
101     @Override
102     public GoId getGoId() {
103         return _go_id;
104     }
105
106     @Override
107     public Type getType() {
108         return _type;
109     }
110
111     @Override
112     public String toString() {
113         final StringBuffer sb = new StringBuffer();
114         switch ( getType() ) {
115             case PART_OF:
116                 sb.append( PART_OF_STR );
117                 break;
118             case NEGATIVELY_REGULATES:
119                 sb.append( NEGATIVELY_REGULATES_STR );
120                 break;
121             case POSITIVELY_REGULATES:
122                 sb.append( POSITIVELY_REGULATES_STR );
123                 break;
124             case REGULATES:
125                 sb.append( REGULATES_STR );
126                 break;
127             default:
128                 new AssertionError( "unknown type: " + getType() );
129         }
130         sb.append( ": " );
131         sb.append( getGoId().toString() );
132         return sb.toString();
133     }
134 }