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