Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MessageFormat |
|
| 1.5;1.5 | ||||
MessageFormat$1 |
|
| 1.5;1.5 | ||||
MessageFormat$LRUMap |
|
| 1.5;1.5 |
1 | 6 | /******************************************************************************* |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | * Hans Schwaebli - http://swtbot.org/bugzilla/show_bug.cgi?id=112 | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.swtbot.swt.finder.utils; | |
13 | ||
14 | import java.util.Arrays; | |
15 | import java.util.LinkedHashMap; | |
16 | import java.util.Map; | |
17 | import java.util.Map.Entry; | |
18 | ||
19 | /** | |
20 | * Message formatter to optimize logging performance. The cost of logging is mostly in the string concatenation and | |
21 | * parameter evaluation of arguments. Log4j logs the object by invoking {@link #toString()} on the object being logged. | |
22 | * This class performs lazy evaluation of the message, only when {@link #toString()} is invoked, which happens at the | |
23 | * time of logging. | |
24 | * <p> | |
25 | * <b>Note:</b> This class uses a ThreadLocal cache instead of a single cache, since the cache would then need to be | |
26 | * synchronized since multiple threads would access it. | |
27 | * </p> | |
28 | * | |
29 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
30 | * @version $Id$ | |
31 | */ | |
32 | 1 | public class MessageFormat { |
33 | ||
34 | private final String pattern; | |
35 | private final Object[] args; | |
36 | 1 | private static final ThreadLocal<Map<String, java.text.MessageFormat>> threadLocal = new ThreadLocal<Map<String, java.text.MessageFormat>>() { |
37 | protected java.util.Map<String, java.text.MessageFormat> initialValue() { | |
38 | 5 | return new LRUMap<String, java.text.MessageFormat>(); |
39 | }; | |
40 | }; | |
41 | ||
42 | 22115 | private MessageFormat(String pattern, Object... args) { |
43 | 22115 | this.pattern = pattern; |
44 | 22115 | this.args = args; |
45 | 22115 | } |
46 | ||
47 | public static MessageFormat format(String s, Object... args) { | |
48 | 22115 | return new MessageFormat(s, args); |
49 | } | |
50 | ||
51 | public String toString() { | |
52 | try { | |
53 | 5 | java.text.MessageFormat formatter = formatter(pattern); |
54 | 5 | return formatter.format(args); |
55 | 0 | } catch (Exception e) { |
56 | 0 | return "MessageFormat: Could not translate message: '" + pattern + "' using arguments " + Arrays.asList(args); //$NON-NLS-1$ //$NON-NLS-2$ |
57 | } | |
58 | } | |
59 | ||
60 | private java.text.MessageFormat formatter(String pattern) { | |
61 | 5 | java.text.MessageFormat formatter = threadLocal.get().get(pattern); |
62 | 5 | if (formatter == null) { |
63 | 5 | formatter = new java.text.MessageFormat(pattern); |
64 | 5 | threadLocal.get().put(pattern, formatter); |
65 | } | |
66 | 5 | return formatter; |
67 | } | |
68 | ||
69 | 10 | private static final class LRUMap<K, V> extends LinkedHashMap<K, V> { |
70 | private static final int MAX_SIZE = 512; | |
71 | ||
72 | protected boolean removeEldestEntry(Entry<K, V> eldest) { | |
73 | 5 | return size() > MAX_SIZE; |
74 | } | |
75 | } | |
76 | } |