| 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 | 2 | /******************************************************************************* |
| 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 | 1 | return new LRUMap<String, java.text.MessageFormat>(); |
| 39 | }; | |
| 40 | }; | |
| 41 | ||
| 42 | 27295 | private MessageFormat(String pattern, Object... args) { |
| 43 | 27295 | this.pattern = pattern; |
| 44 | 27295 | this.args = args; |
| 45 | 27295 | } |
| 46 | ||
| 47 | public static MessageFormat format(String s, Object... args) { | |
| 48 | 27295 | return new MessageFormat(s, args); |
| 49 | } | |
| 50 | ||
| 51 | public String toString() { | |
| 52 | try { | |
| 53 | 129 | java.text.MessageFormat formatter = formatter(pattern); |
| 54 | 129 | 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 | 129 | java.text.MessageFormat formatter = threadLocal.get().get(pattern); |
| 62 | 129 | if (formatter == null) { |
| 63 | 3 | formatter = new java.text.MessageFormat(pattern); |
| 64 | 3 | threadLocal.get().put(pattern, formatter); |
| 65 | } | |
| 66 | 129 | return formatter; |
| 67 | } | |
| 68 | ||
| 69 | 2 | 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 | 3 | return size() > MAX_SIZE; |
| 74 | } | |
| 75 | } | |
| 76 | } |