File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\TagCloud.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import java.util.AbstractList;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31 * Represents an order-preserving tag cloud (http://en.wikipedia.org/wiki/Tag_cloud)
32 * where each keyword gets a weight and displayed according to their weight.
33 *
34 * TODO: define a view on its own.
35 *
36 * @since 1.322
37 */
38 public class TagCloud<T> extends AbstractList<TagCloud<T>.Entry> {
39 public final class Entry {
40 public final T item;
41 public final float weight;
42
43 public Entry(T item, float weight) {
44 this.item = item;
45 this.weight = weight;
46 }
47
48 public float scale() {
49 // TODO: it's not obvious if linear scaling is the right approach or not.
50 return weight*9/max;
51 }
52
53 public String getClassName() {
54 return "tag"+((int)scale());
55 }
56 }
57
58 public interface WeightFunction<T> {
59 float weight(T item);
60 }
61
62 private final List<Entry> entries = new ArrayList<Entry>();
63 private float max = 1;
64
65 /**
66 * Creates a tag cloud.
67 *
68 * @param f
69 * Assigns weight to each item.
70 */
71 public TagCloud(Iterable<? extends T> inputs, WeightFunction<T> f) {
72 for (T input : inputs) {
73 float w = Math.abs(f.weight(input));
74 max = Math.max(w,max);
75 entries.add(new Entry(input, w));
76 }
77 }
78
79 public Entry get(int index) {
80 return entries.get(index);
81 }
82
83 public int size() {
84 return entries.size();
85 }
86 }
87