Skip to content

Commit

Permalink
Using generics with collections when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
garcia-jj committed Feb 17, 2014
1 parent 3478cda commit 9e3e9a3
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 38 deletions.
16 changes: 8 additions & 8 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Expand Up @@ -123,7 +123,7 @@ private static boolean messageContainsOrgSlf4jImplStaticLoggerBinder(String msg)

private final static void bind() {
try {
Set staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
Set<URL> staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
// the next line does the binding
StaticLoggerBinder.getSingleton();
Expand Down Expand Up @@ -213,14 +213,14 @@ private final static void versionSanityCheck() {
// the class itself.
private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";

private static Set findPossibleStaticLoggerBinderPathSet() {
private static Set<URL> findPossibleStaticLoggerBinderPathSet() {
// use Set instead of list in order to deal with bug #138
// LinkedHashSet appropriate here because it preserves insertion order during iteration
Set staticLoggerBinderPathSet = new LinkedHashSet();
Set<URL> staticLoggerBinderPathSet = new LinkedHashSet<URL>();
try {
ClassLoader loggerFactoryClassLoader = LoggerFactory.class
.getClassLoader();
Enumeration paths;
Enumeration<URL> paths;
if (loggerFactoryClassLoader == null) {
paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);
} else {
Expand All @@ -237,7 +237,7 @@ private static Set findPossibleStaticLoggerBinderPathSet() {
return staticLoggerBinderPathSet;
}

private static boolean isAmbiguousStaticLoggerBinderPathSet(Set staticLoggerBinderPathSet) {
private static boolean isAmbiguousStaticLoggerBinderPathSet(Set<URL> staticLoggerBinderPathSet) {
return staticLoggerBinderPathSet.size() > 1;
}

Expand All @@ -246,10 +246,10 @@ private static boolean isAmbiguousStaticLoggerBinderPathSet(Set staticLoggerBind
* No reporting is done otherwise.
*
*/
private static void reportMultipleBindingAmbiguity(Set staticLoggerBinderPathSet) {
private static void reportMultipleBindingAmbiguity(Set<URL> staticLoggerBinderPathSet) {
if (isAmbiguousStaticLoggerBinderPathSet(staticLoggerBinderPathSet)) {
Util.report("Class path contains multiple SLF4J bindings.");
Iterator iterator = staticLoggerBinderPathSet.iterator();
Iterator<URL> iterator = staticLoggerBinderPathSet.iterator();
while (iterator.hasNext()) {
URL path = (URL) iterator.next();
Util.report("Found binding in [" + path + "]");
Expand All @@ -258,7 +258,7 @@ private static void reportMultipleBindingAmbiguity(Set staticLoggerBinderPathSet
}
}

private static void reportActualBinding(Set staticLoggerBinderPathSet) {
private static void reportActualBinding(Set<URL> staticLoggerBinderPathSet) {
if (isAmbiguousStaticLoggerBinderPathSet(staticLoggerBinderPathSet)) {
Util.report("Actual binding is of type ["+StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr()+"]");
}
Expand Down
4 changes: 2 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/MDC.java
Expand Up @@ -180,7 +180,7 @@ public static void clear() {
* @return A copy of the current thread's context map. May be null.
* @since 1.5.1
*/
public static Map getCopyOfContextMap() {
public static Map<String, String> getCopyOfContextMap() {
if (mdcAdapter == null) {
throw new IllegalStateException("MDCAdapter cannot be null. See also "
+ NULL_MDCA_URL);
Expand All @@ -197,7 +197,7 @@ public static Map getCopyOfContextMap() {
* must contain only keys and values of type String
* @since 1.5.1
*/
public static void setContextMap(Map contextMap) {
public static void setContextMap(Map<String, String> contextMap) {
if (mdcAdapter == null) {
throw new IllegalStateException("MDCAdapter cannot be null. See also "
+ NULL_MDCA_URL);
Expand Down
2 changes: 1 addition & 1 deletion slf4j-api/src/main/java/org/slf4j/Marker.java
Expand Up @@ -95,7 +95,7 @@ public interface Marker extends Serializable {
*
* @return Iterator over the references of this marker
*/
public Iterator iterator();
public Iterator<Marker> iterator();

/**
* Does this marker contain a reference to the 'other' marker? Marker A is defined
Expand Down
27 changes: 14 additions & 13 deletions slf4j-api/src/main/java/org/slf4j/helpers/BasicMDCAdapter.java
Expand Up @@ -43,7 +43,8 @@
*/
public class BasicMDCAdapter implements MDCAdapter {

private InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal();
private InheritableThreadLocal<Map<String, String>> inheritableThreadLocal
= new InheritableThreadLocal<Map<String, String>>();

static boolean isJDK14() {
try {
Expand Down Expand Up @@ -74,9 +75,9 @@ public void put(String key, String val) {
if (key == null) {
throw new IllegalArgumentException("key cannot be null");
}
Map map = (Map) inheritableThreadLocal.get();
Map<String, String> map = (Map<String, String>) inheritableThreadLocal.get();
if (map == null) {
map = Collections.synchronizedMap(new HashMap());
map = Collections.<String, String>synchronizedMap(new HashMap<String, String>());
inheritableThreadLocal.set(map);
}
map.put(key, val);
Expand All @@ -86,7 +87,7 @@ public void put(String key, String val) {
* Get the context identified by the <code>key</code> parameter.
*/
public String get(String key) {
Map Map = (Map) inheritableThreadLocal.get();
Map<String, String> Map = (Map<String, String>) inheritableThreadLocal.get();
if ((Map != null) && (key != null)) {
return (String) Map.get(key);
} else {
Expand All @@ -98,7 +99,7 @@ public String get(String key) {
* Remove the the context identified by the <code>key</code> parameter.
*/
public void remove(String key) {
Map map = (Map) inheritableThreadLocal.get();
Map<String, String> map = (Map<String, String>) inheritableThreadLocal.get();
if (map != null) {
map.remove(key);
}
Expand All @@ -108,7 +109,7 @@ public void remove(String key) {
* Clear all entries in the MDC.
*/
public void clear() {
Map map = (Map) inheritableThreadLocal.get();
Map<String, String> map = (Map<String, String>) inheritableThreadLocal.get();
if (map != null) {
map.clear();
// the InheritableThreadLocal.remove method was introduced in JDK 1.5
Expand All @@ -127,8 +128,8 @@ public void clear() {
*
* @return the keys in the MDC
*/
public Set getKeys() {
Map map = (Map) inheritableThreadLocal.get();
public Set<String> getKeys() {
Map<String, String> map = (Map<String, String>) inheritableThreadLocal.get();
if (map != null) {
return map.keySet();
} else {
Expand All @@ -140,10 +141,10 @@ public Set getKeys() {
* Returned value may be null.
*
*/
public Map getCopyOfContextMap() {
Map oldMap = (Map) inheritableThreadLocal.get();
public Map<String, String> getCopyOfContextMap() {
Map<String, String> oldMap = (Map<String, String>) inheritableThreadLocal.get();
if (oldMap != null) {
Map newMap = Collections.synchronizedMap(new HashMap());
Map<String, String> newMap = Collections.<String, String>synchronizedMap(new HashMap<String, String>());
synchronized (oldMap) {
newMap.putAll(oldMap);
}
Expand All @@ -153,8 +154,8 @@ public Map getCopyOfContextMap() {
}
}

public void setContextMap(Map contextMap) {
Map map = Collections.synchronizedMap(new HashMap(contextMap));
public void setContextMap(Map<String, String> contextMap) {
Map<String, String> map = Collections.<String, String>synchronizedMap(new HashMap<String, String>(contextMap));
inheritableThreadLocal.set(map);
}

Expand Down
10 changes: 5 additions & 5 deletions slf4j-api/src/main/java/org/slf4j/helpers/BasicMarker.java
Expand Up @@ -42,7 +42,7 @@ public class BasicMarker implements Marker {
private static final long serialVersionUID = 1803952589649545191L;

private final String name;
private List refereceList;
private List<Marker> refereceList;

BasicMarker(String name) {
if (name == null) {
Expand Down Expand Up @@ -71,7 +71,7 @@ public synchronized void add(Marker reference) {
} else {
// let's add the reference
if (refereceList == null) {
refereceList = new Vector();
refereceList = new Vector<Marker>();
}
refereceList.add(reference);
}
Expand All @@ -86,11 +86,11 @@ public boolean hasChildren() {
return hasReferences();
}

public synchronized Iterator iterator() {
public synchronized Iterator<Marker> iterator() {
if (refereceList != null) {
return refereceList.iterator();
} else {
return Collections.EMPTY_LIST.iterator();
return Collections.emptyIterator();
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ public String toString() {
if (!this.hasReferences()) {
return this.getName();
}
Iterator it = this.iterator();
Iterator<Marker> it = this.iterator();
Marker reference;
StringBuffer sb = new StringBuffer(this.getName());
sb.append(' ').append(OPEN);
Expand Down
Expand Up @@ -222,13 +222,13 @@ final public static FormattingTuple arrayFormat(final String messagePattern,
// itself escaped: "abc x:\\{}"
// we have to consume one backward slash
sbuf.append(messagePattern.substring(i, j - 1));
deeplyAppendParameter(sbuf, argArray[L], new HashMap());
deeplyAppendParameter(sbuf, argArray[L], new HashMap<Object[], Object>());
i = j + 2;
}
} else {
// normal case
sbuf.append(messagePattern.substring(i, j));
deeplyAppendParameter(sbuf, argArray[L], new HashMap());
deeplyAppendParameter(sbuf, argArray[L], new HashMap<Object[], Object>());
i = j + 2;
}
}
Expand Down Expand Up @@ -268,7 +268,7 @@ final static boolean isDoubleEscaped(String messagePattern,

// special treatment of array values was suggested by 'lizongbo'
private static void deeplyAppendParameter(StringBuilder sbuf, Object o,
Map seenMap) {
Map<Object[], Object> seenMap) {
if (o == null) {
sbuf.append("null");
return;
Expand Down Expand Up @@ -315,7 +315,7 @@ private static void safeObjectAppend(StringBuilder sbuf, Object o) {
}

private static void objectArrayAppend(StringBuilder sbuf, Object[] a,
Map seenMap) {
Map<Object[], Object> seenMap) {
sbuf.append('[');
if (!seenMap.containsKey(a)) {
seenMap.put(a, null);
Expand Down
4 changes: 2 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/helpers/NOPMDCAdapter.java
Expand Up @@ -52,11 +52,11 @@ public void put(String key, String val) {
public void remove(String key) {
}

public Map getCopyOfContextMap() {
public Map<String, String> getCopyOfContextMap() {
return null;
}

public void setContextMap(Map contextMap) {
public void setContextMap(Map<String, String> contextMap) {
// NOP
}

Expand Down
Expand Up @@ -53,7 +53,7 @@ public Logger getLogger(String name) {
return logger;
}

public List getLoggerNames() {
public List<String> getLoggerNames() {
return new ArrayList<String>(loggers.keySet());
}

Expand Down
4 changes: 2 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java
Expand Up @@ -76,7 +76,7 @@ public interface MDCAdapter {
* @return A copy of the current thread's context map. May be null.
* @since 1.5.1
*/
public Map getCopyOfContextMap();
public Map<String, String> getCopyOfContextMap();

/**
* Set the current thread's context map by first clearing any existing
Expand All @@ -87,5 +87,5 @@ public interface MDCAdapter {
*
* @since 1.5.1
*/
public void setContextMap(Map contextMap);
public void setContextMap(Map<String, String> contextMap);
}

0 comments on commit 9e3e9a3

Please sign in to comment.