Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NanoDecor |
|
| 4.333333333333333;4.333 |
1 | /** | |
2 | * Copyright (c) 2012-2015, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.log; | |
31 | ||
32 | import java.util.Formattable; | |
33 | import java.util.FormattableFlags; | |
34 | import java.util.Formatter; | |
35 | import lombok.EqualsAndHashCode; | |
36 | import lombok.ToString; | |
37 | ||
38 | /** | |
39 | * Decorate time interval in nanoseconds. | |
40 | * | |
41 | * <p>For example: | |
42 | * | |
43 | * <pre> | |
44 | * final long start = System.nanoTime(); | |
45 | * // some operations | |
46 | * Logger.debug("completed in %[nano]s", System.nanoTime() - start); | |
47 | * </pre> | |
48 | * | |
49 | * @author Marina Kosenko (marina.kosenko@gmail.com) | |
50 | * @author Yegor Bugayenko (yegor@teamed.io) | |
51 | * @version $Id: 726c4059bd66e1e3431078d36ce1fc879d18bd22 $ | |
52 | * @since 0.1 | |
53 | */ | |
54 | 0 | @ToString |
55 | 0 | @EqualsAndHashCode(of = "nano") |
56 | final class NanoDecor implements Formattable { | |
57 | ||
58 | /** | |
59 | * The period to work with, in nanoseconds. | |
60 | */ | |
61 | private final transient Double nano; | |
62 | ||
63 | /** | |
64 | * Public ctor. | |
65 | * @param nan The interval in nanoseconds | |
66 | */ | |
67 | @SuppressWarnings("PMD.NullAssignment") | |
68 | 0 | NanoDecor(final Long nan) { |
69 | 0 | if (nan == null) { |
70 | 0 | this.nano = null; |
71 | } else { | |
72 | 0 | this.nano = Double.valueOf(nan); |
73 | } | |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * {@inheritDoc} | |
78 | * @checkstyle ParameterNumber (4 lines) | |
79 | */ | |
80 | @Override | |
81 | public void formatTo(final Formatter formatter, final int flags, | |
82 | final int width, final int precision) { | |
83 | 0 | if (this.nano == null) { |
84 | 0 | formatter.format("NULL"); |
85 | } else { | |
86 | 0 | final StringBuilder format = new StringBuilder(0); |
87 | 0 | format.append('%'); |
88 | 0 | if ((flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags |
89 | .LEFT_JUSTIFY) { | |
90 | 0 | format.append('-'); |
91 | } | |
92 | 0 | if (width > 0) { |
93 | 0 | format.append(Integer.toString(width)); |
94 | } | |
95 | 0 | if ((flags & FormattableFlags.UPPERCASE) == FormattableFlags |
96 | .UPPERCASE) { | |
97 | 0 | format.append('S'); |
98 | } else { | |
99 | 0 | format.append('s'); |
100 | } | |
101 | 0 | formatter.format(format.toString(), this.toText(precision)); |
102 | } | |
103 | 0 | } |
104 | ||
105 | /** | |
106 | * Create text. | |
107 | * @param precision The precision | |
108 | * @return The text | |
109 | */ | |
110 | private String toText(final int precision) { | |
111 | double number; | |
112 | final String title; | |
113 | // @checkstyle MagicNumber (15 lines) | |
114 | 0 | if (this.nano < 1000L) { |
115 | 0 | number = this.nano; |
116 | 0 | title = "ns"; |
117 | 0 | } else if (this.nano < 1000L * 1000) { |
118 | 0 | number = this.nano / 1000L; |
119 | 0 | title = "µs"; |
120 | 0 | } else if (this.nano < 1000L * 1000 * 1000) { |
121 | 0 | number = this.nano / (1000L * 1000); |
122 | 0 | title = "ms"; |
123 | 0 | } else if (this.nano < 1000L * 1000 * 1000 * 60) { |
124 | 0 | number = this.nano / (1000L * 1000 * 1000); |
125 | 0 | title = "s"; |
126 | } else { | |
127 | 0 | number = this.nano / (1000L * 1000 * 1000 * 60); |
128 | 0 | title = "min"; |
129 | } | |
130 | final String format; | |
131 | 0 | if (precision >= 0) { |
132 | 0 | format = String.format("%%.%df%%s", precision); |
133 | } else { | |
134 | 0 | format = "%.0f%s"; |
135 | } | |
136 | 0 | return String.format(format, number, title); |
137 | } | |
138 | ||
139 | } |