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