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$
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.swing;
34
35 import java.util.List;
36
37 /**
38 * Strategy who split the elements in 2 groups of same cardinal.
39 *
40 * @author Laurent DUTHEIL
41 */
42 public class SplitByNumber extends SplitStrategy {
43
44 /**
45 *
46 */
47 private static final long serialVersionUID = -6484279333222332702L;
48
49 @Override
50 public void splitElements(final List<TreeMapNode> v, final List<TreeMapNode> v1, final List<TreeMapNode> v2) {
51 final int size = v.size();
52 final int middle = size / 2;
53 int index = 0;
54 // we add first elements to v1
55 for (; index < middle; index++) {
56 v1.add(v.get(index));
57 }
58 // we add last elements to v2
59 for (; index < size; index++) {
60 v2.add(v.get(index));
61 }
62 }
63
64 @Override
65 public double sumWeight(final List<TreeMapNode> v) {
66 // all the elements must have the same weight
67 double weight = 0.0;
68 for (final TreeMapNode node : v) {
69 if (node.isLeaf()) {
70 weight += 1;
71 } else {
72 weight += this.sumWeight(node.getChildren());
73 }
74 }
75 return weight;
76 }
77 }
78 /*
79 * ObjectLab is supporing JTreeMap
80 *
81 * Based in London, we are world leaders in the design and development
82 * of bespoke applications for the securities financing markets.
83 *
84 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
85 * ___ _ _ _ _ _
86 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
87 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
88 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
89 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
90 * |__/
91 *
92 * www.ObjectLab.co.uk
93 */