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