|
20 | 20 | import java.util.Collection; |
21 | 21 | import java.util.Collections; |
22 | 22 | import java.util.Comparator; |
| 23 | +import java.util.Iterator; |
23 | 24 | import java.util.LinkedHashMap; |
24 | 25 | import java.util.LinkedHashSet; |
25 | 26 | import java.util.Map; |
@@ -50,18 +51,19 @@ public class Bucket { |
50 | 51 | */ |
51 | 52 | public static final Charset CHARSET = StandardCharsets.UTF_8; |
52 | 53 |
|
| 54 | + private static final Comparator<String> COMPARATOR = new NullSafeComparator<>(Comparator.<String> naturalOrder(), |
| 55 | + true); |
| 56 | + |
53 | 57 | /** |
54 | 58 | * The Redis data as {@link Map} sorted by the keys. |
55 | 59 | */ |
56 | 60 | private final NavigableMap<String, byte[]> data = new TreeMap<>( |
57 | | - new NullSafeComparator<>(Comparator.<String> naturalOrder(), true)); |
| 61 | + COMPARATOR); |
58 | 62 |
|
59 | 63 | /** |
60 | | - * Creates new empty bucket |
| 64 | + * Creates a new empty bucket. |
61 | 65 | */ |
62 | | - public Bucket() { |
63 | | - |
64 | | - } |
| 66 | + public Bucket() {} |
65 | 67 |
|
66 | 68 | Bucket(Map<String, byte[]> data) { |
67 | 69 |
|
@@ -158,7 +160,6 @@ public Map<String, byte[]> asMap() { |
158 | 160 | * @return |
159 | 161 | */ |
160 | 162 | public Bucket extract(String prefix) { |
161 | | - |
162 | 163 | return new Bucket(data.subMap(prefix, prefix + Character.MAX_VALUE)); |
163 | 164 | } |
164 | 165 |
|
@@ -269,19 +270,32 @@ public String toString() { |
269 | 270 |
|
270 | 271 | private String safeToString() { |
271 | 272 |
|
272 | | - Map<String, String> serialized = new LinkedHashMap<>(); |
273 | | - for (Map.Entry<String, byte[]> entry : data.entrySet()) { |
274 | | - if (entry.getValue() != null) { |
275 | | - serialized.put(entry.getKey(), toUtf8String(entry.getValue())); |
276 | | - } else { |
277 | | - serialized.put(entry.getKey(), null); |
| 273 | + if (data.isEmpty()) { |
| 274 | + return "{}"; |
| 275 | + } |
| 276 | + |
| 277 | + StringBuilder sb = new StringBuilder(); |
| 278 | + sb.append('{'); |
| 279 | + |
| 280 | + Iterator<Entry<String, byte[]>> iterator = data.entrySet().iterator(); |
| 281 | + |
| 282 | + for (;;) { |
| 283 | + |
| 284 | + Entry<String, byte[]> e = iterator.next(); |
| 285 | + sb.append(e.getKey()); |
| 286 | + sb.append('='); |
| 287 | + sb.append(toUtf8String(e.getValue())); |
| 288 | + |
| 289 | + if (!iterator.hasNext()) { |
| 290 | + return sb.append('}').toString(); |
278 | 291 | } |
| 292 | + |
| 293 | + sb.append(',').append(' '); |
279 | 294 | } |
280 | | - return serialized.toString(); |
281 | 295 | } |
282 | 296 |
|
283 | 297 | @Nullable |
284 | | - private String toUtf8String(byte[] raw) { |
| 298 | + private static String toUtf8String(byte[] raw) { |
285 | 299 |
|
286 | 300 | try { |
287 | 301 | return new String(raw, CHARSET); |
|
0 commit comments