| 1 | /* |
| 2 | * TreeTraitProvider.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST 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 |
| 18 | * GNU 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 BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.evolution.tree; |
| 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | |
| 33 | /** |
| 34 | * An interface for objects that can provide TreeTraits (i.e., information about the nodes and |
| 35 | * branches of a tree). |
| 36 | * |
| 37 | * @author Andrew Rambaut |
| 38 | * @version $Id$ |
| 39 | */ |
| 40 | public interface TreeTraitProvider { |
| 41 | |
| 42 | /** |
| 43 | * Returns an array of all the available traits |
| 44 | * @return the array |
| 45 | */ |
| 46 | TreeTrait[] getTreeTraits(); |
| 47 | |
| 48 | /** |
| 49 | * Returns a trait that is stored using a specific key. This will often be the same |
| 50 | * as the 'name' of the trait but may not be depending on the application. |
| 51 | * @param key a unique key |
| 52 | * @return the trait |
| 53 | */ |
| 54 | TreeTrait getTreeTrait(String key); |
| 55 | |
| 56 | /** |
| 57 | * This is a concrete helper class that can be used either as a mix in base to another |
| 58 | * class or as a delegate. It is itself a TreeTraitProvider so can be instantiated and |
| 59 | * passed as is. |
| 60 | */ |
| 61 | public class Helper implements TreeTraitProvider { |
| 62 | |
| 63 | /** |
| 64 | * Default constructor |
| 65 | */ |
| 66 | public Helper() { |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Constructor taking a single initial trait |
| 71 | * @param trait the TreeTrait |
| 72 | */ |
| 73 | public Helper(TreeTrait trait) { |
| 74 | addTrait(trait); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Constructor taking a single initial trait |
| 79 | * @param trait the TreeTrait |
| 80 | */ |
| 81 | public Helper(String key, TreeTrait trait) { |
| 82 | addTrait(key, trait); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Constructor taking an array of initial traits |
| 87 | * @param traits the array of TreeTraits |
| 88 | */ |
| 89 | public Helper(TreeTrait[] traits) { |
| 90 | addTraits(traits); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Constructor taking a collection of initial traits |
| 95 | * @param traits the collection of TreeTraits |
| 96 | */ |
| 97 | public Helper(Collection<TreeTrait> traits) { |
| 98 | addTraits(traits); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Add a single trait to the list keyed by its name |
| 103 | * @param trait the TreeTrait |
| 104 | */ |
| 105 | public void addTrait(TreeTrait trait) { |
| 106 | traits.put(trait.getTraitName(), trait); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add a single trait to the list using a given key |
| 111 | * @param key the key |
| 112 | * @param trait the TreeTrait |
| 113 | */ |
| 114 | public void addTrait(String key, TreeTrait trait) { |
| 115 | if (traits.containsKey(key)) { |
| 116 | throw new RuntimeException("All traits must have unique names"); |
| 117 | } |
| 118 | traits.put(key, trait); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Add an array of traits to the list, keying each with its |
| 123 | * respective name |
| 124 | * @param traits the array of TreeTraits |
| 125 | */ |
| 126 | public void addTraits(TreeTrait[] traits) { |
| 127 | for (TreeTrait trait : traits) { |
| 128 | this.traits.put(trait.getTraitName(), trait); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Add a collection of traits to the list, keying each with its |
| 134 | * respective name |
| 135 | * @param traits the collection of TreeTraits |
| 136 | */ |
| 137 | public void addTraits(Collection<TreeTrait> traits) { |
| 138 | for (TreeTrait trait : traits) { |
| 139 | this.traits.put(trait.getTraitName(), trait); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | // Implementation of TreeTraitProvider interface |
| 145 | |
| 146 | public TreeTrait[] getTreeTraits() { |
| 147 | TreeTrait[] traitArray = new TreeTrait[traits.values().size()]; |
| 148 | return traits.values().toArray(traitArray); |
| 149 | } |
| 150 | |
| 151 | public TreeTrait getTreeTrait(String key) { |
| 152 | return traits.get(key); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object o) { |
| 157 | if (this == o) return true; |
| 158 | if (o == null || getClass() != o.getClass()) return false; |
| 159 | |
| 160 | Helper helper = (Helper) o; |
| 161 | |
| 162 | if (traits != null ? !traits.equals(helper.traits) : helper.traits != null) return false; |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int hashCode() { |
| 169 | return traits != null ? traits.hashCode() : 0; |
| 170 | } |
| 171 | |
| 172 | // Private members |
| 173 | |
| 174 | private Map<String, TreeTrait> traits = new HashMap<String, TreeTrait>(); |
| 175 | } |
| 176 | } |