View Javadoc

1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3    * 
4    * Based in London, we are world leaders in the design and development 
5    * of bespoke applications for the securities financing markets.
6    * 
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id: TreeMapNodeBuilder.java 75 2006-10-24 23:00:51Z benoitx $
18   * 
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
32   */
33  package net.sf.jtreemap.ktreemap;
34  
35  /**
36   * Tree builder for a KTreeMap.
37   *
38   * @author Laurent Dutheil
39   */
40  
41  public abstract class TreeMapNodeBuilder {
42    private TreeMapNode root;
43  
44    /**
45     * Add a branch to the tree. <BR>
46     * If the parent is null, the build node become the root if and only if the
47     * tree have no root yet. If the parent is null and if the root is already
48     * build, the node will NOT be added to the tree.
49     *
50     * @param value Value of the branch
51     * @param parent father of the branch
52     * @return the created node
53     */
54    public TreeMapNode buildBranch(Object value, TreeMapNode parent) {
55      TreeMapNode node = new TreeMapNode(value);
56      if (parent != null) {
57        parent.add(node);
58      } else if (this.root == null) {
59        this.root = node;
60      }
61      return node;
62    }
63  
64    /**
65     * add a leaf to the tree. <BR>
66     * If the parent is null, the build node become the root if and only if the
67     * tree have no root yet. If the parent is null and if the root is already
68     * build, the node will NOT be added to the tree.
69     *
70     * @param value Value of the leaf
71     * @param parent father of the leaf
72     * @return the created node
73     */
74    public TreeMapNode buildLeaf(Object value, TreeMapNode parent) {
75      TreeMapNode node = new TreeMapNode(value, getWeight(value));
76      if (parent != null) {
77        parent.add(node);
78      } else if (this.root == null) {
79        this.root = node;
80      }
81      return node;
82    }
83  
84    /**
85     * get the build tree.
86     *
87     * @return the root of the tree
88     */
89    public TreeMapNode getRoot() {
90      return this.root;
91    }
92  
93    /**
94     * Return the weight of an Object. <BR>
95     * This method have to be extended
96     *
97     * @param value the object (generaly, the TreeMapNode.getValue())
98     * @return the weight of the value
99     */
100   public abstract double getWeight(Object value);
101 }
102 /*
103  *                 ObjectLab is supporing JTreeMap
104  * 
105  * Based in London, we are world leaders in the design and development 
106  * of bespoke applications for the securities financing markets.
107  * 
108  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
109  *           ___  _     _           _   _          _
110  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
111  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
112  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
113  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
114  *                   |__/
115  *
116  *                     www.ObjectLab.co.uk
117  */