35ce62f2c446e7c9698b801bd049be98b1b680bb
[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     public static short getId( final String id ) {
44         if ( !str2id.containsKey( id ) ) {
45             if ( count > ...) error
46             id2str.put(  count, id );
47             str2id.put( id,  count );
48             ++count;
49         }
50         return  str2id.get( id ) ;
51     }
52
53     public static String getStr( final short id ) {
54         return id2str.get( id );
55     }
56
57     public BasicBinaryDomainCombination( final String id0, final String id1 ) {
58         if ( ( id0 == null ) || ( id1 == null ) ) {
59             throw new IllegalArgumentException( "attempt to create binary domain combination using null" );
60         }
61         if ( ( id0.indexOf( SEPARATOR ) != -1 ) || ( id1.indexOf( SEPARATOR ) != -1 ) ) {
62             throw new IllegalArgumentException( "ill formatted domain id: " + id0 + ", " + id1 );
63         }
64         if ( id0.toLowerCase().compareTo( id1.toLowerCase() ) < 0 ) {
65             //  _data = id0 + SEPARATOR + id1;
66         }
67         else {
68             // _data = id1 + SEPARATOR + id0;
69         }
70     }
71
72     BasicBinaryDomainCombination() {
73         _data = null;
74     }
75
76     @Override
77     public int compareTo( final BinaryDomainCombination binary_domain_combination ) {
78         if ( binary_domain_combination.getClass() != this.getClass() ) {
79             throw new IllegalArgumentException( "attempt to compare [" + binary_domain_combination.getClass() + "] to "
80                     + "[" + this.getClass() + "]" );
81         }
82         if ( equals( binary_domain_combination ) ) {
83             return 0;
84         }
85         final int x = getId0().compareTo( binary_domain_combination.getId0() );
86         if ( x != 0 ) {
87             return x;
88         }
89         else {
90             return getId1().compareTo( binary_domain_combination.getId1() );
91         }
92     }
93
94     @Override
95     public boolean equals( final Object o ) {
96         if ( this == o ) {
97             return true;
98         }
99         else if ( o == null ) {
100             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
101         }
102         else if ( o.getClass() != this.getClass() ) {
103             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to ["
104                     + o.getClass() + "]" );
105         }
106         else {
107             return ( getId0().equals( ( ( BinaryDomainCombination ) o ).getId0() ) )
108                     && ( getId1().equals( ( ( BinaryDomainCombination ) o ).getId1() ) );
109         }
110     }
111
112     @Override
113     public String getId0() {
114         return _data.substring( 0, _data.indexOf( SEPARATOR ) );
115     }
116
117     @Override
118     public String getId1() {
119         return _data.substring( _data.indexOf( SEPARATOR ) + 1 );
120     }
121
122     @Override
123     public int hashCode() {
124         return getAsStr().hashCode();
125     }
126
127     @Override
128     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     public String toString() {
173         return getAsStr();
174     }
175
176     private String getAsStr() {
177         return _data;
178     }
179
180     public static BinaryDomainCombination createInstance( final String ids ) {
181         if ( ids.indexOf( BinaryDomainCombination.SEPARATOR ) < 1 ) {
182             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
183         }
184         final String[] ids_ary = ids.split( BinaryDomainCombination.SEPARATOR );
185         if ( ids_ary.length != 2 ) {
186             throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
187         }
188         return new BasicBinaryDomainCombination( ids_ary[ 0 ], ids_ary[ 1 ] );
189     }
190 }