I don't see the point: 1. there is no casting currenty that would be avoided by introducing generics 2. at the end of the ride, Message.getFormattedMessage turns the map contents into a String anyway, so there is no difference to client code calling mapMessage.with(objKey, objValue) and mapMessage.with(objKey.toString(), objValue.toString())
We could add a convenience method "with(Object, Object)" to help make client code a bit shorter, but generics would not come into play, no? On Tue, Oct 27, 2015 at 2:52 AM, Matt Sicker <[email protected]> wrote: > MapMessage can use Objects, not just Strings for its values. > > On 26 October 2015 at 10:45, Mikael Ståldal <[email protected]> > wrote: > >> Generics for what? To avoid putting the "with" method in subclasses? I >> don't think so, but I would be happy to be proven wrong. >> >> On Fri, Oct 23, 2015 at 6:11 PM, Gary Gregory <[email protected]> >> wrote: >> >>> Can generics be used to here? >>> >>> Gary >>> >>> >>> -------- Original message -------- >>> From: [email protected] >>> Date: 10/23/2015 06:19 (GMT-08:00) >>> To: [email protected] >>> Subject: [1/3] logging-log4j2 git commit: Builder like pattern in >>> MapMessage >>> >>> Repository: logging-log4j2 >>> Updated Branches: >>> refs/heads/master 9c3af7114 -> 66bbf4ddf >>> >>> >>> Builder like pattern in MapMessage >>> >>> >>> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo >>> Commit: >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/990a97c0 >>> Tree: >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/990a97c0 >>> Diff: >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/990a97c0 >>> >>> Branch: refs/heads/master >>> Commit: 990a97c0b5688ee9512544edfa6596de9ee939fc >>> Parents: f08c62c >>> Author: Mikael St��ldal <[email protected]> >>> Authored: Thu Oct 15 17:33:55 2015 +0200 >>> Committer: Mikael St��ldal <[email protected]> >>> Committed: Thu Oct 15 17:33:55 2015 +0200 >>> >>> ---------------------------------------------------------------------- >>> .../org/apache/logging/log4j/message/MapMessage.java | 5 +++++ >>> .../logging/log4j/message/StructuredDataMessage.java | 5 +++++ >>> .../apache/logging/log4j/message/MapMessageTest.java | 11 +++++++++++ >>> .../log4j/message/StructuredDataMessageTest.java | 14 +++++++++++++- >>> 4 files changed, 34 insertions(+), 1 deletion(-) >>> ---------------------------------------------------------------------- >>> >>> >>> >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/990a97c0/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java >>> ---------------------------------------------------------------------- >>> diff --git >>> a/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java >>> b/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java >>> index 6172fee..1012d0e 100644 >>> --- >>> a/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java >>> +++ >>> b/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java >>> @@ -109,6 +109,11 @@ public class MapMessage implements >>> MultiformatMessage { >>> data.clear(); >>> } >>> >>> + public MapMessage withValue(final String key, final String value) { >>> + put(key, value); >>> + return this; >>> + } >>> + >>> /** >>> * Add an item to the data Map. >>> * @param key The name of the data item. >>> >>> >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/990a97c0/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java >>> ---------------------------------------------------------------------- >>> diff --git >>> a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java >>> b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java >>> index 97d7204..abacce2 100644 >>> --- >>> a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java >>> +++ >>> b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java >>> @@ -130,6 +130,11 @@ public class StructuredDataMessage extends >>> MapMessage { >>> >>> } >>> >>> + public StructuredDataMessage withValue(final String key, final >>> String value) { >>> + put(key, value); >>> + return this; >>> + } >>> + >>> /** >>> * Returns the supported formats. >>> * @return An array of the supported format names. >>> >>> >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/990a97c0/log4j-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java >>> ---------------------------------------------------------------------- >>> diff --git >>> a/log4j-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java >>> b/log4j-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java >>> index 1ee688e..7f43027 100644 >>> --- >>> a/log4j-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java >>> +++ >>> b/log4j-api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java >>> @@ -37,6 +37,17 @@ public class MapMessageTest { >>> } >>> >>> @Test >>> + public void testBuilder() { >>> + final String testMsg = "Test message {}"; >>> + final MapMessage msg = new MapMessage() >>> + .withValue("message", testMsg) >>> + .withValue("project", "Log4j"); >>> + final String result = msg.getFormattedMessage(); >>> + final String expected = "message=\"Test message {}\" >>> project=\"Log4j\""; >>> + assertEquals(expected, result); >>> + } >>> + >>> + @Test >>> public void testXML() { >>> final String testMsg = "Test message {}"; >>> final MapMessage msg = new MapMessage(); >>> >>> >>> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/990a97c0/log4j-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java >>> ---------------------------------------------------------------------- >>> diff --git >>> a/log4j-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java >>> b/log4j-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java >>> index d741f35..ce9ad9b 100644 >>> --- >>> a/log4j-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java >>> +++ >>> b/log4j-api/src/test/java/org/apache/logging/log4j/message/StructuredDataMessageTest.java >>> @@ -37,7 +37,19 @@ public class StructuredDataMessageTest { >>> assertEquals(expected, result); >>> } >>> >>> - @Test(expected=IllegalArgumentException.class) >>> + @Test >>> + public void testBuilder() { >>> + final String testMsg = "Test message {}"; >>> + final StructuredDataMessage msg = new >>> StructuredDataMessage("MsgId@12345", testMsg, "Alert") >>> + .withValue("message", testMsg) >>> + .withValue("project", "Log4j") >>> + .withValue("memo", "This is a very long test memo to >>> prevent regression of LOG4J2-114"); >>> + final String result = msg.getFormattedMessage(); >>> + final String expected = "Alert [MsgId@12345 memo=\"This is a >>> very long test memo to prevent regression of LOG4J2-114\" message=\"Test >>> message {}\" project=\"Log4j\"] Test message {}"; >>> + assertEquals(expected, result); >>> + } >>> + >>> + @Test(expected = IllegalArgumentException.class) >>> public void testMsgWithKeyTooLong() { >>> final String testMsg = "Test message {}"; >>> final StructuredDataMessage msg = new >>> StructuredDataMessage("MsgId@12345", testMsg, "Alert"); >>> >>> >> >> >> -- >> [image: MagineTV] >> >> *Mikael Ståldal* >> Senior software developer >> >> *Magine TV* >> [email protected] >> Regeringsgatan 25 | 111 53 Stockholm, Sweden | www.magine.com >> >> Privileged and/or Confidential Information may be contained in this >> message. If you are not the addressee indicated in this message >> (or responsible for delivery of the message to such a person), you may >> not copy or deliver this message to anyone. In such case, >> you should destroy this message and kindly notify the sender by reply >> email. >> > > > > -- > Matt Sicker <[email protected]> >
