e84c94a04a4544910855c1fb9610a230770f17de
[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 java.util.HashMap;
30 import java.util.Map;
31
32 import org.forester.protein.BinaryDomainCombination;
33 import org.forester.util.ForesterUtil;
34
35 public class BasicBinaryDomainCombination implements BinaryDomainCombination {
36
37     final static Map<Short, String> id2str = new HashMap<Short, String>();
38     final static Map<String, Short> str2id = new HashMap<String, Short>();
39     static short                    count  = 0;
40     short                           _id0;
41     short                           _id1;
42
43     static short getId( final String id ) {
44         if ( !str2id.containsKey( id ) ) {
45             if ( count >= Short.MAX_VALUE ) {
46                 throw new RuntimeException( "too many domain ids!" );
47             }
48             id2str.put( count, id );
49             str2id.put( id, count );
50             ++count;
51         }
52         return  str2id.get( id ) ;
53     }
54
55      static String getStr( final short id ) {
56         return id2str.get( id );
57     }
58
59     public BasicBinaryDomainCombination( final String id0, final String id1 ) {
60         if ( ( id0 == null ) || ( id1 == null ) ) {
61             throw new IllegalArgumentException( "attempt to create binary domain combination using null" );
62         }
63         if ( ( id0.indexOf( SEPARATOR ) != -1 ) || ( id1.indexOf( SEPARATOR ) != -1 ) ) {
64             throw new IllegalArgumentException( "ill formatted domain id: " + id0 + ", " + id1 );
65         }
66         if ( id0.toLowerCase().compareTo( id1.toLowerCase() ) < 0 ) {
67            _id0 = getId( id0 );
68            _id1 = getId( id1 );
69         }
70         else {
71             // _data = id1 + SEPARATOR + id0;
72             _id0 = getId( id1 );
73             _id1 = getId( id0 );
74         }
75     }
76
77     BasicBinaryDomainCombination() {
78         _id0 = -1;
79         _id1 = -1;
80     }
81
82     @Override
83     public int compareTo( final BinaryDomainCombination binary_domain_combination ) {
84         if ( binary_domain_combination.getClass() != this.getClass() ) {
85             throw new IllegalArgumentException( "attempt to compare [" + binary_domain_combination.getClass() + "] to "
86                     + "[" + this.getClass() + "]" );
87         }
88         if ( equals( binary_domain_combination ) ) {
89             return 0;
90         }
91         final int x = getId0().compareTo( binary_domain_combination.getId0() );
92         if ( x != 0 ) {
93             return x;
94         }
95         else {
96             return getId1().compareTo( binary_domain_combination.getId1() );
97         }
98     }
99
100     @Override
101     public boolean equals( final Object o ) {
102         if ( this == o ) {
103             return true;
104         }
105         else if ( o == null ) {
106             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
107         }
108         else if ( o.getClass() != this.getClass() ) {
109             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to ["
110                     + o.getClass() + "]" );
111         }
112         else {
113             return ( getId0().equals( ( ( BinaryDomainCombination ) o ).getId0() ) )
114                     && ( getId1().equals( ( ( BinaryDomainCombination ) o ).getId1() ) );
115         }
116     }
117
118     @Override
119     public String getId0() {
120         //return _data.substring( 0, _data.indexOf( SEPARATOR ) );
121         return getStr( _id0 );
122     }
123
124     @Override
125     public String getId1() {
126         //return _data.substring( _data.indexOf( SEPARATOR ) + 1 );
127         return getStr( _id1 );
128     }
129
130     @Override
131     public int hashCode() {
132         return getAsStr().hashCode();
133     }
134
135     @Override
136     public StringBuffer toGraphDescribingLanguage( final OutputFormat format,
137                                                    final String node_attribute,
138                                                    final String edge_attribute ) {
139         final StringBuffer sb = new StringBuffer();
140         switch ( format ) {
141             case DOT:
142                 if ( ForesterUtil.isEmpty( node_attribute ) ) {
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                 else {
153                     sb.append( getId0() );
154                     sb.append( " " );
155                     sb.append( node_attribute );
156                     sb.append( ";" );
157                     sb.append( ForesterUtil.LINE_SEPARATOR );
158                     sb.append( getId1() );
159                     sb.append( " " );
160                     sb.append( node_attribute );
161                     sb.append( ";" );
162                     sb.append( ForesterUtil.LINE_SEPARATOR );
163                     sb.append( getId0() );
164                     sb.append( " -- " );
165                     sb.append( getId1() );
166                     if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
167                         sb.append( " " );
168                         sb.append( edge_attribute );
169                     }
170                     sb.append( ";" );
171                 }
172                 break;
173             default:
174                 throw new AssertionError( "unknown format:" + format );
175         }
176         return sb;
177     }
178
179     @Override
180     public String toString() {
181         return getAsStr();
182     }
183
184     private String getAsStr() {
185         return getId0() + SEPARATOR + getId1();
186     }
187
188     public static BinaryDomainCombination createInstance( final String ids ) {
189         if ( ids.indexOf( BinaryDomainCombination.SEPARATOR ) < 1 ) {
190             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
191         }
192         final String[] ids_ary = ids.split( BinaryDomainCombination.SEPARATOR );
193         if ( ids_ary.length != 2 ) {
194             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
195         }
196         return new BasicBinaryDomainCombination( ids_ary[ 0 ], ids_ary[ 1 ] );
197     }
198 }