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