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.BinaryDomainCombination;
33 import org.forester.util.ForesterUtil;
35 public class BasicBinaryDomainCombination implements BinaryDomainCombination {
37 final static Map<Integer, String> id2str = new HashMap<Integer, String>();
38 final static Map<String, Integer> str2id = new HashMap<String, Integer>();
39 static short count = 0;
43 public static short getId( final String id ) {
44 if ( !str2id.containsKey( id ) ) {
45 id2str.put( ( int ) count, id );
46 str2id.put( id, ( int ) count );
49 return Short.valueOf( str2id.get( id ) + "" );
52 public static String getStr( final short id ) {
53 return id2str.get( id );
56 public BasicBinaryDomainCombination( final String id0, final String id1 ) {
57 if ( ( id0 == null ) || ( id1 == null ) ) {
58 throw new IllegalArgumentException( "attempt to create binary domain combination using null" );
60 if ( ( id0.indexOf( SEPARATOR ) != -1 ) || ( id1.indexOf( SEPARATOR ) != -1 ) ) {
61 throw new IllegalArgumentException( "ill formatted domain id: " + id0 + ", " + id1 );
63 if ( id0.toLowerCase().compareTo( id1.toLowerCase() ) < 0 ) {
64 // _data = id0 + SEPARATOR + id1;
67 // _data = id1 + SEPARATOR + id0;
71 BasicBinaryDomainCombination() {
76 public int compareTo( final BinaryDomainCombination binary_domain_combination ) {
77 if ( binary_domain_combination.getClass() != this.getClass() ) {
78 throw new IllegalArgumentException( "attempt to compare [" + binary_domain_combination.getClass() + "] to "
79 + "[" + this.getClass() + "]" );
81 if ( equals( binary_domain_combination ) ) {
84 final int x = getId0().compareTo( binary_domain_combination.getId0() );
89 return getId1().compareTo( binary_domain_combination.getId1() );
94 public boolean equals( final Object o ) {
98 else if ( o == null ) {
99 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
101 else if ( o.getClass() != this.getClass() ) {
102 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to ["
103 + o.getClass() + "]" );
106 return ( getId0().equals( ( ( BinaryDomainCombination ) o ).getId0() ) )
107 && ( getId1().equals( ( ( BinaryDomainCombination ) o ).getId1() ) );
112 public String getId0() {
113 return _data.substring( 0, _data.indexOf( SEPARATOR ) );
117 public String getId1() {
118 return _data.substring( _data.indexOf( SEPARATOR ) + 1 );
122 public int hashCode() {
123 return getAsStr().hashCode();
127 public StringBuffer toGraphDescribingLanguage( final OutputFormat format,
128 final String node_attribute,
129 final String edge_attribute ) {
130 final StringBuffer sb = new StringBuffer();
133 if ( ForesterUtil.isEmpty( node_attribute ) ) {
134 sb.append( getId0() );
136 sb.append( getId1() );
137 if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
139 sb.append( edge_attribute );
144 sb.append( getId0() );
146 sb.append( node_attribute );
148 sb.append( ForesterUtil.LINE_SEPARATOR );
149 sb.append( getId1() );
151 sb.append( node_attribute );
153 sb.append( ForesterUtil.LINE_SEPARATOR );
154 sb.append( getId0() );
156 sb.append( getId1() );
157 if ( !ForesterUtil.isEmpty( edge_attribute ) ) {
159 sb.append( edge_attribute );
165 throw new AssertionError( "unknown format:" + format );
171 public String toString() {
175 private String getAsStr() {
179 public static BinaryDomainCombination createInstance( final String ids ) {
180 if ( ids.indexOf( BinaryDomainCombination.SEPARATOR ) < 1 ) {
181 throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
183 final String[] ids_ary = ids.split( BinaryDomainCombination.SEPARATOR );
184 if ( ids_ary.length != 2 ) {
185 throw new IllegalArgumentException( "Unexpected format for binary domain combination [" + ids + "]" );
187 return new BasicBinaryDomainCombination( ids_ary[ 0 ], ids_ary[ 1 ] );