Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SizeDecor |
|
| 3.3333333333333335;3.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 java.util.concurrent.ConcurrentHashMap; | |
36 | import java.util.concurrent.ConcurrentMap; | |
37 | import lombok.EqualsAndHashCode; | |
38 | import lombok.ToString; | |
39 | ||
40 | /** | |
41 | * Size decorator. | |
42 | * @author Marina Kosenko (marina.kosenko@gmail.com) | |
43 | * @author Yegor Bugayenko (yegor@teamed.io) | |
44 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
45 | * @version $Id: 5344ac17c1929ea2ebc75088692334e5cd25e8a4 $ | |
46 | * @since 0.1 | |
47 | */ | |
48 | 0 | @ToString |
49 | 0 | @EqualsAndHashCode(of = "size") |
50 | final class SizeDecor implements Formattable { | |
51 | ||
52 | /** | |
53 | * Highest power supported by this SizeDecor. | |
54 | */ | |
55 | private static final int MAX_POWER = 6; | |
56 | ||
57 | /** | |
58 | * Map of prefixes for powers of 1024. | |
59 | */ | |
60 | 0 | private static final ConcurrentMap<Integer, String> SUFFIXES = |
61 | new ConcurrentHashMap<Integer, String>(); | |
62 | ||
63 | /** | |
64 | * The size to work with. | |
65 | */ | |
66 | private final transient Long size; | |
67 | ||
68 | static { | |
69 | // @checkstyle MagicNumber (9 lines) | |
70 | 0 | SizeDecor.SUFFIXES.put(0, "b"); |
71 | 0 | SizeDecor.SUFFIXES.put(1, "Kb"); |
72 | 0 | SizeDecor.SUFFIXES.put(2, "Mb"); |
73 | 0 | SizeDecor.SUFFIXES.put(3, "Gb"); |
74 | 0 | SizeDecor.SUFFIXES.put(4, "Tb"); |
75 | 0 | SizeDecor.SUFFIXES.put(5, "Pb"); |
76 | 0 | SizeDecor.SUFFIXES.put(6, "Eb"); |
77 | 0 | } |
78 | ||
79 | /** | |
80 | * Public ctor. | |
81 | * @param sze The size | |
82 | */ | |
83 | 0 | public SizeDecor(final Long sze) { |
84 | 0 | this.size = sze; |
85 | 0 | } |
86 | ||
87 | /** | |
88 | * {@inheritDoc} | |
89 | * @checkstyle ParameterNumber (4 lines) | |
90 | */ | |
91 | @Override | |
92 | public void formatTo(final Formatter formatter, final int flags, | |
93 | final int width, final int precision) { | |
94 | 0 | if (this.size == null) { |
95 | 0 | formatter.format("NULL"); |
96 | } else { | |
97 | 0 | final StringBuilder format = new StringBuilder().append('%'); |
98 | 0 | if ((flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags |
99 | .LEFT_JUSTIFY) { | |
100 | 0 | format.append('-'); |
101 | } | |
102 | 0 | if (width > 0) { |
103 | 0 | format.append(Integer.toString(width)); |
104 | } | |
105 | 0 | if ((flags & FormattableFlags.UPPERCASE) == FormattableFlags |
106 | .UPPERCASE) { | |
107 | 0 | format.append('S'); |
108 | } else { | |
109 | 0 | format.append('s'); |
110 | } | |
111 | 0 | formatter.format( |
112 | format.toString(), this.formatSizeWithSuffix(precision) | |
113 | ); | |
114 | } | |
115 | 0 | } |
116 | ||
117 | /** | |
118 | * Format the size, with suffix. | |
119 | * @param precision The precision to use | |
120 | * @return The formatted size | |
121 | */ | |
122 | private String formatSizeWithSuffix(final int precision) { | |
123 | 0 | int power = 0; |
124 | 0 | double number = this.size; |
125 | // @checkstyle MagicNumber (2 lines) | |
126 | 0 | while (number / 1024 >= 1 && power < SizeDecor.MAX_POWER) { |
127 | 0 | number = number / 1024; |
128 | 0 | power += 1; |
129 | } | |
130 | 0 | final String suffix = SizeDecor.SUFFIXES.get(power); |
131 | final String format; | |
132 | 0 | if (precision >= 0) { |
133 | 0 | format = String.format("%%.%df%%s", precision); |
134 | } else { | |
135 | 0 | format = "%.0f%s"; |
136 | } | |
137 | 0 | return String.format(format, number, suffix); |
138 | } | |
139 | ||
140 | } |