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