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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe, Thomas J. Black
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 hudson.Util;
27 import hudson.model.Node;
28
29 import java.io.IOException;
30
31 import org.kohsuke.stapler.export.ExportedBean;
32 import org.kohsuke.stapler.export.Exported;
33
34 /**
35 * Represents a clock difference. Immutable.
36 *
37 * @author Kohsuke Kawaguchi
38 */
39 @ExportedBean
40 public final class ClockDifference {
41 /**
42 * The difference in milliseconds.
43 *
44 * Positive value means the slave is behind the master,
45 * negative value means the slave is ahead of the master.
46 */
47 @Exported
48 public final long diff;
49
50 public ClockDifference(long value) {
51 this.diff = value;
52 }
53
54 /**
55 * Returns true if the difference is big enough to be considered dangerous.
56 */
57 public boolean isDangerous() {
58 return Math.abs(diff)>5000;
59 }
60
61 /**
62 * Gets the absolute value of {@link #diff}.
63 */
64 public long abs() {
65 return Math.abs(diff);
66 }
67
68 /**
69 * Gets the clock difference in HTML string.
70 */
71 @Override
72 public String toString() {
73 if(-1000<diff && diff <1000)
74 return Messages.ClockDifference_InSync(); // clock is in sync
75
76 long abs = Math.abs(diff);
77
78 String s = Util.getTimeSpanString(abs);
79 if(diff<0)
80 s += Messages.ClockDifference_Ahead();
81 else
82 s += Messages.ClockDifference_Behind();
83
84 return s;
85 }
86
87 public String toHtml() {
88 String s = toString();
89 if(isDangerous())
90 s = Util.wrapToErrorSpan(s);
91 return s;
92 }
93
94 public static String toHtml(Node d) {
95 try {
96 if(d==null) return FAILED_HTML;
97 return d.getClockDifference().toHtml();
98 } catch (IOException e) {
99 return FAILED_HTML;
100 } catch (InterruptedException e) {
101 return FAILED_HTML;
102 }
103 }
104
105 /**
106 * Gets the clock difference in HTML string.
107 * This version handles null {@link ClockDifference}.
108 */
109 public static String toHtml(ClockDifference d) {
110 if(d==null) return FAILED_HTML;
111 return d.toHtml();
112 }
113
114 public static final ClockDifference ZERO = new ClockDifference(0);
115
116 private static final String FAILED_HTML =
117 "<span class='error'>" + Messages.ClockDifference_Failed() + "</span>";
118 }
119