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