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