3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.surfacing;
29 import java.util.HashMap;
32 import org.forester.protein.BasicDomain;
33 import org.forester.protein.BinaryDomainCombination;
34 import org.forester.util.ForesterUtil;
36 public class BasicBinaryDomainCombination implements BinaryDomainCombination {
38 final static boolean VERBOSE = true;
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>();
44 BasicBinaryDomainCombination() {
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" );
53 if ( ( id0.indexOf( SEPARATOR ) != -1 ) || ( id1.indexOf( SEPARATOR ) != -1 ) ) {
54 throw new IllegalArgumentException( "ill formatted domain id: " + id0 + ", " + id1 );
56 if ( id0.toLowerCase().compareTo( id1.toLowerCase() ) < 0 ) {
57 _id0 = BasicDomain.obtainIdAsShort( id0 );
58 _id1 = BasicDomain.obtainIdAsShort( id1 );
61 _id0 = BasicDomain.obtainIdAsShort( id1 );
62 _id1 = BasicDomain.obtainIdAsShort( id0 );
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() + "]" );
72 if ( equals( binary_domain_combination ) ) {
75 final int x = getId0().compareTo( binary_domain_combination.getId0() );
80 return getId1().compareTo( binary_domain_combination.getId1() );
85 final public boolean equals( final Object o ) {
89 else if ( o == null ) {
90 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
92 else if ( o.getClass() != this.getClass() ) {
93 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to ["
94 + o.getClass() + "]" );
97 return ( getId0Code() == ( ( BinaryDomainCombination ) o ).getId0Code() )
98 && ( getId1Code() == ( ( BinaryDomainCombination ) o ).getId1Code() );
103 final public String getId0() {
104 return BasicDomain.obtainIdFromShort( _id0 );
108 final public short getId0Code() {
113 final public String getId1() {
114 return BasicDomain.obtainIdFromShort( _id1 );
118 final public short getId1Code() {
123 final public int hashCode() {
124 return calcCode( _id0, _id1 );
128 final public StringBuffer toGraphDescribingLanguage( final OutputFormat format,
129 final String node_attribute,
130 final String edge_attribute ) {
131 final StringBuffer sb = new StringBuffer();
134 if ( ForesterUtil.isEmpty( node_attribute ) ) {
135 sb.append( getId0() );
137 sb.append( getId1() );
138 if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
140 sb.append( edge_attribute );
145 sb.append( getId0() );
147 sb.append( node_attribute );
149 sb.append( ForesterUtil.LINE_SEPARATOR );
150 sb.append( getId1() );
152 sb.append( node_attribute );
154 sb.append( ForesterUtil.LINE_SEPARATOR );
155 sb.append( getId0() );
157 sb.append( getId1() );
158 if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
160 sb.append( edge_attribute );
166 throw new AssertionError( "unknown format:" + format );
172 final public String toString() {
173 final int code = calcCode( _id0, _id1 );
174 if ( S_POOL.containsKey( code ) ) {
175 return S_POOL.get( code );
178 final String s = getId0() + SEPARATOR + getId1();
179 S_POOL.put( code, s );
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 + "]" );
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 + "]" );
192 return BasicBinaryDomainCombination.obtainInstance( ids_ary[ 0 ], ids_ary[ 1 ] );
195 public static BasicBinaryDomainCombination obtainInstance( final String id0, final String id1 ) {
197 if ( id0.toLowerCase().compareTo( id1.toLowerCase() ) < 0 ) {
198 code = calcCode( BasicDomain.obtainIdAsShort( id0 ), BasicDomain.obtainIdAsShort( id1 ) );
201 code = calcCode( BasicDomain.obtainIdAsShort( id1 ), BasicDomain.obtainIdAsShort( id0 ) );
203 if ( DC_POOL.containsKey( code ) ) {
204 return DC_POOL.get( code );
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() );
216 final static int calcCode( final int id0, final int id1 ) {
217 return ( id0 * ( Short.MAX_VALUE + 1 ) ) + id1;