logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1392429 - /logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
Date Mon, 01 Oct 2012 16:31:49 GMT
Author: ggregory
Date: Mon Oct  1 16:31:48 2012
New Revision: 1392429

URL: http://svn.apache.org/viewvc?rev=1392429&view=rev
Log:
Use a SortedMap for the data. The data was typed as Map<String, String> but every time
we want to do something with it, it is converted to a TreeMap (impls. SortedMap).
- This is wasteful if the map is already a SortedMap.
- There is a lot code duplication to create the sorted maps.

Modified:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java?rev=1392429&r1=1392428&r2=1392429&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
Mon Oct  1 16:31:48 2012
@@ -18,7 +18,6 @@ package org.apache.logging.log4j.message
 
 import java.io.Serializable;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.SortedMap;
 import java.util.TreeMap;
@@ -42,13 +41,13 @@ public class MapMessage implements Multi
 
     private static final long serialVersionUID = -5031471831131487120L;
 
-    private final Map<String, String> data;
+    private final SortedMap<String, String> data;
 
     /**
      * Constructor.
      */
     public MapMessage() {
-        data = new HashMap<String, String>();
+        data = new TreeMap<String, String>();
     }
 
     /**
@@ -56,7 +55,7 @@ public class MapMessage implements Multi
      * @param map The Map.
      */
     public MapMessage(Map<String, String> map) {
-        this.data = map;
+        this.data = map instanceof SortedMap ? (SortedMap<String, String>) map : new
TreeMap<String, String>(map);
     }
 
     public String[] getFormats() {
@@ -192,8 +191,7 @@ public class MapMessage implements Multi
 
     public void asXML(StringBuilder sb) {
         sb.append("<Map>\n");
-        SortedMap<String, String> sorted = new TreeMap<String, String>(data);
-        for (Map.Entry<String, String> entry : sorted.entrySet()) {
+        for (Map.Entry<String, String> entry : data.entrySet()) {
             sb.append("  <Entry key=").append(entry.getKey()).append(">").append(entry.getValue()).append("</Entry>\n");
         }
         sb.append("</Map>");
@@ -232,9 +230,8 @@ public class MapMessage implements Multi
     }
 
     protected void appendMap(StringBuilder sb) {
-        SortedMap<String, String> sorted = new TreeMap<String, String>(data);
         boolean first = true;
-        for (Map.Entry<String, String> entry : sorted.entrySet()) {
+        for (Map.Entry<String, String> entry : data.entrySet()) {
             if (!first) {
                 sb.append(" ");
             }
@@ -244,10 +241,9 @@ public class MapMessage implements Multi
     }
 
     protected void asJSON(StringBuilder sb) {
-        SortedMap<String, String> sorted = new TreeMap<String, String>(data);
         boolean first = true;
         sb.append("{");
-        for (Map.Entry<String, String> entry : sorted.entrySet()) {
+        for (Map.Entry<String, String> entry : data.entrySet()) {
             if (!first) {
                 sb.append(", ");
             }
@@ -260,10 +256,9 @@ public class MapMessage implements Multi
 
 
     protected void asJava(StringBuilder sb) {
-        SortedMap<String, String> sorted = new TreeMap<String, String>(data);
         boolean first = true;
         sb.append("{");
-        for (Map.Entry<String, String> entry : sorted.entrySet()) {
+        for (Map.Entry<String, String> entry : data.entrySet()) {
             if (!first) {
                 sb.append(", ");
             }



Mime
View raw message