in progress
[jalview.git] / forester / java / src / org / forester / surfacing / BasicBinaryDomainCombination.java
1 // $Id:
2 // Exp $
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: www.phylosoft.org/forester
26
27 package org.forester.surfacing;
28
29 import org.forester.protein.BinaryDomainCombination;
30 import org.forester.protein.DomainId;
31 import org.forester.util.ForesterUtil;
32
33 public class BasicBinaryDomainCombination implements BinaryDomainCombination {
34
35     String _id_0;
36     String _id_1;
37
38     //DomainId _id_0;
39     //DomainId _id_1;
40     BasicBinaryDomainCombination() {
41         _id_0 = null;
42         _id_1 = null;
43     }
44
45     public BasicBinaryDomainCombination( final String id_0, final String id_1 ) {
46         if ( ( id_0 == null ) || ( id_1 == null ) ) {
47             throw new IllegalArgumentException( "attempt to create binary domain combination using null" );
48         }
49         final String my_id_0 = id_0.trim();
50         final String my_id_1 = id_1.trim();
51         if ( my_id_0.toLowerCase().compareTo( my_id_1.toLowerCase() ) < 0 ) {
52             _id_0 = my_id_0;
53             _id_1 = my_id_1;
54         }
55         else {
56             _id_0 = my_id_1;
57             _id_1 = my_id_0;
58         }
59     }
60
61     public BasicBinaryDomainCombination( final DomainId id_0, final DomainId id_1 ) {
62         this( id_0.getId(), id_1.getId() );
63     }
64
65     @Override
66     public int compareTo( final BinaryDomainCombination binary_domain_combination ) {
67         if ( binary_domain_combination.getClass() != this.getClass() ) {
68             throw new IllegalArgumentException( "attempt to compare [" + binary_domain_combination.getClass() + "] to "
69                     + "[" + this.getClass() + "]" );
70         }
71         if ( equals( binary_domain_combination ) ) {
72             return 0;
73         }
74         final int x = getId0().compareTo( binary_domain_combination.getId0() );
75         if ( x != 0 ) {
76             return x;
77         }
78         else {
79             return getId1().compareTo( binary_domain_combination.getId1() );
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 [" + this.getClass() + "] equality to null" );
90         }
91         else if ( o.getClass() != this.getClass() ) {
92             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to ["
93                     + o.getClass() + "]" );
94         }
95         else {
96             return ( getId0().equals( ( ( BinaryDomainCombination ) o ).getId0() ) )
97                     && ( getId1().equals( ( ( BinaryDomainCombination ) o ).getId1() ) );
98         }
99     }
100
101     @Override
102     public DomainId getId0() {
103         return new DomainId( _id_0 );
104     }
105
106     @Override
107     public DomainId getId1() {
108         return new DomainId( _id_1 );
109     }
110
111     @Override
112     public int hashCode() {
113         // return getId0().hashCode() + ( 19 * getId1().hashCode() );
114         return ( _id_0 + _id_1 ).hashCode();
115     }
116
117     @Override
118     public StringBuffer toGraphDescribingLanguage( final OutputFormat format,
119                                                    final String node_attribute,
120                                                    final String edge_attribute ) {
121         final StringBuffer sb = new StringBuffer();
122         switch ( format ) {
123             case DOT:
124                 if ( ForesterUtil.isEmpty( node_attribute ) ) {
125                     sb.append( getId0() );
126                     sb.append( " -- " );
127                     sb.append( getId1() );
128                     if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
129                         sb.append( " " );
130                         sb.append( edge_attribute );
131                     }
132                     sb.append( ";" );
133                 }
134                 else {
135                     sb.append( getId0() );
136                     sb.append( " " );
137                     sb.append( node_attribute );
138                     sb.append( ";" );
139                     sb.append( ForesterUtil.LINE_SEPARATOR );
140                     sb.append( getId1() );
141                     sb.append( " " );
142                     sb.append( node_attribute );
143                     sb.append( ";" );
144                     sb.append( ForesterUtil.LINE_SEPARATOR );
145                     sb.append( getId0() );
146                     sb.append( " -- " );
147                     sb.append( getId1() );
148                     if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
149                         sb.append( " " );
150                         sb.append( edge_attribute );
151                     }
152                     sb.append( ";" );
153                 }
154                 break;
155             default:
156                 throw new AssertionError( "unknown format:" + format );
157         }
158         return sb;
159     }
160
161     @Override
162     public String toString() {
163         final StringBuffer sb = new StringBuffer();
164         sb.append( _id_0 );
165         sb.append( BinaryDomainCombination.SEPARATOR );
166         sb.append( _id_1 );
167         return sb.toString();
168     }
169
170     public static BinaryDomainCombination createInstance( final String ids ) {
171         if ( ids.indexOf( BinaryDomainCombination.SEPARATOR ) < 1 ) {
172             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
173         }
174         final String[] ids_ary = ids.split( BinaryDomainCombination.SEPARATOR );
175         if ( ids_ary.length != 2 ) {
176             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
177         }
178         return new BasicBinaryDomainCombination( ids_ary[ 0 ], ids_ary[ 1 ] );
179     }
180 }