2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28 package org.forester.phylogeny;
30 import org.forester.phylogeny.data.PhylogenyData;
33 * @author Christian M. Zmasek
35 public class PhylogenyBranch {
37 private final PhylogenyNode _node_1;
38 private final PhylogenyNode _node_2;
39 private PhylogenyData _data;
40 private final boolean _is_directed;
41 private boolean _towards_1;
43 public PhylogenyBranch( final PhylogenyNode first_node, final PhylogenyNode second_node ) {
44 if ( ( first_node == null ) || ( second_node == null ) ) {
45 throw new IllegalArgumentException( "Attempt to create a branch with a null node" );
48 _node_2 = second_node;
52 public PhylogenyBranch( final PhylogenyNode first_node,
53 final PhylogenyNode second_node,
54 final boolean direction_towards_first ) {
55 if ( ( first_node == null ) || ( second_node == null ) ) {
56 throw new IllegalArgumentException( "Attempt to create a branch with a null node" );
59 _node_2 = second_node;
61 _towards_1 = direction_towards_first;
65 public boolean equals( final Object obj ) {
72 if ( getClass() != obj.getClass() ) {
75 final PhylogenyBranch other = ( PhylogenyBranch ) obj;
76 return hashCode() == other.hashCode();
79 public PhylogenyNode getConnectedNode( final PhylogenyNode node ) throws IllegalArgumentException {
80 if ( node == _node_1 ) {
83 else if ( node == _node_2 ) {
87 throw new IllegalArgumentException( "Attempt to get " + "connected node on branch with node which is "
88 + "not connected by the branch" );
92 public PhylogenyData getData() {
96 public PhylogenyNode getFirstNode() {
100 public PhylogenyNode getSecondNode() {
105 public int hashCode() {
106 final int PRIME = 31;
108 final int node_1_hc = _node_1.hashCode();
109 final int node_2_hc = _node_2.hashCode();
112 if ( !_is_directed ) {
113 if ( node_1_hc > node_2_hc ) {
132 result = ( PRIME * result ) + ( ( _data == null ) ? 0 : _data.hashCode() );
133 result = ( PRIME * result ) + ( _is_directed ? 1231 : 1237 );
134 result = ( PRIME * result ) + hc_1;
135 result = ( PRIME * result ) + hc_2;
139 public boolean isDirected() {
143 public boolean isDirectionTowards( final PhylogenyNode node ) throws RuntimeException {
144 if ( !isDirected() ) {
145 throw new RuntimeException( "Attempt to get direction of undirected branch" );
147 return ( ( node == _node_1 ) && _towards_1 );
150 public void setDirectionTowards( final PhylogenyNode node ) {
151 _towards_1 = node == _node_1;
155 public String toString() {
156 if ( isDirected() ) {
157 if ( isDirectionTowards( getFirstNode() ) ) {
158 return ( getSecondNode().getName() + " -> " + getFirstNode().getName() );
161 return ( getFirstNode().getName() + " -> " + getSecondNode().getName() );
165 return ( getFirstNode().getName() + " -- " + getSecondNode().getName() );