inprogress
[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: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.surfacing;
28
29 import org.forester.protein.BinaryDomainCombination;
30 import org.forester.util.ForesterUtil;
31
32 public class BasicBinaryDomainCombination implements BinaryDomainCombination {
33
34     String _data;
35
36     public BasicBinaryDomainCombination( final String id0, final String id1 ) {
37         if ( ( id0 == null ) || ( id1 == null ) ) {
38             throw new IllegalArgumentException( "attempt to create binary domain combination using null" );
39         }
40         if ( ( id0.indexOf( SEPARATOR ) != -1 ) || ( id1.indexOf( SEPARATOR ) != -1 ) ) {
41             throw new IllegalArgumentException( "ill formatted domain id: " + id0 + ", " + id1 );
42         }
43         if ( id0.toLowerCase().compareTo( id1.toLowerCase() ) < 0 ) {
44             _data = id0 + SEPARATOR + id1;
45         }
46         else {
47             _data = id1 + SEPARATOR + id0;
48         }
49     }
50
51     BasicBinaryDomainCombination() {
52         _data = null;
53     }
54
55     @Override
56     public int compareTo( final BinaryDomainCombination binary_domain_combination ) {
57         if ( binary_domain_combination.getClass() != this.getClass() ) {
58             throw new IllegalArgumentException( "attempt to compare [" + binary_domain_combination.getClass() + "] to "
59                     + "[" + this.getClass() + "]" );
60         }
61         if ( equals( binary_domain_combination ) ) {
62             return 0;
63         }
64         final int x = getId0().compareTo( binary_domain_combination.getId0() );
65         if ( x != 0 ) {
66             return x;
67         }
68         else {
69             return getId1().compareTo( binary_domain_combination.getId1() );
70         }
71     }
72
73     @Override
74     public boolean equals( final Object o ) {
75         if ( this == o ) {
76             return true;
77         }
78         else if ( o == null ) {
79             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
80         }
81         else if ( o.getClass() != this.getClass() ) {
82             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to ["
83                     + o.getClass() + "]" );
84         }
85         else {
86             return ( getId0().equals( ( ( BinaryDomainCombination ) o ).getId0() ) )
87                     && ( getId1().equals( ( ( BinaryDomainCombination ) o ).getId1() ) );
88         }
89     }
90
91     @Override
92     public String getId0() {
93         return _data.substring( 0, _data.indexOf( SEPARATOR ) );
94     }
95
96     @Override
97     public String getId1() {
98         return _data.substring( _data.indexOf( SEPARATOR ) + 1 );
99     }
100
101     @Override
102     public int hashCode() {
103         return getAsStr().hashCode();
104     }
105
106     @Override
107     public StringBuffer toGraphDescribingLanguage( final OutputFormat format,
108                                                    final String node_attribute,
109                                                    final String edge_attribute ) {
110         final StringBuffer sb = new StringBuffer();
111         switch ( format ) {
112             case DOT:
113                 if ( ForesterUtil.isEmpty( node_attribute ) ) {
114                     sb.append( getId0() );
115                     sb.append( " -- " );
116                     sb.append( getId1() );
117                     if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
118                         sb.append( " " );
119                         sb.append( edge_attribute );
120                     }
121                     sb.append( ";" );
122                 }
123                 else {
124                     sb.append( getId0() );
125                     sb.append( " " );
126                     sb.append( node_attribute );
127                     sb.append( ";" );
128                     sb.append( ForesterUtil.LINE_SEPARATOR );
129                     sb.append( getId1() );
130                     sb.append( " " );
131                     sb.append( node_attribute );
132                     sb.append( ";" );
133                     sb.append( ForesterUtil.LINE_SEPARATOR );
134                     sb.append( getId0() );
135                     sb.append( " -- " );
136                     sb.append( getId1() );
137                     if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
138                         sb.append( " " );
139                         sb.append( edge_attribute );
140                     }
141                     sb.append( ";" );
142                 }
143                 break;
144             default:
145                 throw new AssertionError( "unknown format:" + format );
146         }
147         return sb;
148     }
149
150     @Override
151     public String toString() {
152         return getAsStr();
153     }
154
155     private String getAsStr() {
156         return _data;
157     }
158
159     public static BinaryDomainCombination createInstance( final String ids ) {
160         if ( ids.indexOf( BinaryDomainCombination.SEPARATOR ) < 1 ) {
161             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
162         }
163         final String[] ids_ary = ids.split( BinaryDomainCombination.SEPARATOR );
164         if ( ids_ary.length != 2 ) {
165             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
166         }
167         return new BasicBinaryDomainCombination( ids_ary[ 0 ], ids_ary[ 1 ] );
168     }
169 }