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