From 8852380e4642cc5e56cf990307b3200ce66e917d Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Thu, 18 Feb 2016 16:00:27 +0100 Subject: [PATCH] Remove dependency on Google Guava --- pom.xml | 5 ---- .../structured/StructuredSyslogMessage.java | 9 +++---- .../graylog2/syslog4j/util/Preconditions.java | 26 +++++++++++++++++++ .../StructuredSyslogMessageTest.java | 14 +++++----- 4 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 src/main/java/org/graylog2/syslog4j/util/Preconditions.java diff --git a/pom.xml b/pom.xml index 943a5bf..eb1e21b 100644 --- a/pom.xml +++ b/pom.xml @@ -73,11 +73,6 @@ 1.6 true - - com.google.guava - guava - 18.0 - org.apache.commons commons-lang3 diff --git a/src/main/java/org/graylog2/syslog4j/impl/message/structured/StructuredSyslogMessage.java b/src/main/java/org/graylog2/syslog4j/impl/message/structured/StructuredSyslogMessage.java index 1235c48..8960291 100644 --- a/src/main/java/org/graylog2/syslog4j/impl/message/structured/StructuredSyslogMessage.java +++ b/src/main/java/org/graylog2/syslog4j/impl/message/structured/StructuredSyslogMessage.java @@ -1,12 +1,12 @@ package org.graylog2.syslog4j.impl.message.structured; -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; import org.graylog2.syslog4j.SyslogConstants; import org.graylog2.syslog4j.impl.message.AbstractSyslogMessage; +import org.graylog2.syslog4j.util.Preconditions; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -121,7 +121,7 @@ public class StructuredSyslogMessage extends AbstractSyslogMessage implements St end=stringMessage.indexOf(SyslogConstants.STRUCTURED_DATA_EMPTY_VALUE)+4; } else { - final Map> structuredDataMap = Maps.newHashMap(); + final Map> structuredDataMap = new HashMap>(); while(start < stringMessage.length() && matchChar(stringMessage, start, '[') == start) { Preconditions.checkArgument(stringMessage.charAt(start) == '[', "Invalid structured data in syslog message '%s'", stringMessage); @@ -129,7 +129,7 @@ public class StructuredSyslogMessage extends AbstractSyslogMessage implements St Preconditions.checkArgument(end != -1 && stringMessage.charAt(end) == ']', "Invalid structured data in syslog message '%s'", stringMessage); String key = null; - Map keyMap = Maps.newHashMap(); + Map keyMap = new HashMap(); while (start < end) { if (key == null) { final int keyEnd = matchChar(stringMessage, ++start, ']', ' '); // Key can be terminated by a space (then more fields to follow) or a ] @@ -138,7 +138,6 @@ public class StructuredSyslogMessage extends AbstractSyslogMessage implements St } else { Preconditions.checkArgument(start < stringMessage.length() && stringMessage.charAt(start) == ' ', "Invalid structured data in syslog message '%s'", stringMessage); start = start + 1; // Start points at the space behind either the key or the previous value - Preconditions.checkArgument(key != null, "Invalid structured data in syslog message '%s'", stringMessage); final int equalsIndex = stringMessage.indexOf('=', start); // Equals terminates the field name. Preconditions.checkArgument(equalsIndex != -1, "Invalid structured data in syslog message '%s'", stringMessage); Preconditions.checkArgument(stringMessage.charAt(equalsIndex + 1) == '"', "Invalid structured data in syslog message '%s'", stringMessage); diff --git a/src/main/java/org/graylog2/syslog4j/util/Preconditions.java b/src/main/java/org/graylog2/syslog4j/util/Preconditions.java new file mode 100644 index 0000000..e64fd22 --- /dev/null +++ b/src/main/java/org/graylog2/syslog4j/util/Preconditions.java @@ -0,0 +1,26 @@ +package org.graylog2.syslog4j.util; + +public final class Preconditions { + private Preconditions() { + } + + public static void checkArgument(boolean expression) { + if (!expression) { + throw new IllegalArgumentException(); + } + } + + public static void checkArgument(boolean expression, Object errorMessage) { + if (!expression) { + throw new IllegalArgumentException(String.valueOf(errorMessage)); + } + } + + public static void checkArgument(boolean expression, + String errorMessageTemplate, + Object... errorMessageArgs) { + if (!expression) { + throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)); + } + } +} diff --git a/src/test/java/org/graylog2/syslog4j/test/message/structured/StructuredSyslogMessageTest.java b/src/test/java/org/graylog2/syslog4j/test/message/structured/StructuredSyslogMessageTest.java index ab4a6e4..5013c68 100644 --- a/src/test/java/org/graylog2/syslog4j/test/message/structured/StructuredSyslogMessageTest.java +++ b/src/test/java/org/graylog2/syslog4j/test/message/structured/StructuredSyslogMessageTest.java @@ -48,13 +48,13 @@ package org.graylog2.syslog4j.test.message.structured; // Date: Jul 15, 2009 // --------------------- -import com.google.common.collect.Maps; import junit.framework.TestCase; import org.graylog2.syslog4j.impl.message.structured.StructuredSyslogMessage; import org.graylog2.syslog4j.server.impl.event.structured.StructuredSyslogServerEvent; import org.junit.Assert; import java.net.InetAddress; +import java.util.HashMap; import java.util.Map; public class StructuredSyslogMessageTest extends TestCase @@ -123,7 +123,7 @@ public class StructuredSyslogMessageTest extends TestCase fail(); } catch (IllegalArgumentException iae) { - // + assertEquals("Invalid structured data in syslog message 'msgId1 [invalid SD] my message!!'", iae.getMessage()); } } @@ -136,7 +136,7 @@ public class StructuredSyslogMessageTest extends TestCase fail(); } catch (IllegalArgumentException iae) { - // + assertEquals("Invalid structured data in syslog message 'msgId1 [data1 a=b] my message!!'", iae.getMessage()); } } @@ -149,7 +149,7 @@ public class StructuredSyslogMessageTest extends TestCase fail(); } catch (IllegalArgumentException iae) { - // + assertEquals("Invalid structured data in syslog message 'msgId1 [data1 a=\"b] my message!!'", iae.getMessage()); } } @@ -162,7 +162,7 @@ public class StructuredSyslogMessageTest extends TestCase fail(); } catch (IllegalArgumentException iae) { - // + assertEquals("Invalid structured data in syslog message 'msgId1 [data1 a=b\"] my message!!'", iae.getMessage()); } } @@ -214,13 +214,13 @@ public class StructuredSyslogMessageTest extends TestCase public void testCreateMessage3() { final StructuredSyslogMessage message = - new StructuredSyslogMessage("msgId", null, Maps.>newHashMap(), "my message"); + new StructuredSyslogMessage("msgId", null, new HashMap>(), "my message"); assertEquals("msgId [0@0] my message", message.createMessage()); } public void testCreateMessage4() { - final Map> map = Maps.newHashMap(); + final Map> map = new HashMap>(); final StructuredSyslogMessage message = new StructuredSyslogMessage("msgId", null, map, "my message"); assertEquals("msgId [0@0] my message", message.createMessage());