Skip to content

Commit a14e576

Browse files
authored
Miscellaneous code cleanup (#57)
1 parent f4bf490 commit a14e576

17 files changed

+78
-74
lines changed

src/main/java/com/cloudbees/syslog/Facility.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public enum Facility implements Comparable<Facility> {
128128
LOCAL7(23, "LOCAL7");
129129

130130
// mapping
131-
private final static Map<String, Facility> facilityFromLabel = new HashMap<String, Facility>();
132-
private final static Map<Integer, Facility> facilityFromNumericalCode = new HashMap<Integer, Facility>();
131+
private final static Map<String, Facility> facilityFromLabel = new HashMap<>();
132+
private final static Map<Integer, Facility> facilityFromNumericalCode = new HashMap<>();
133133

134134
static {
135135
for (Facility facility : Facility.values()) {
@@ -148,7 +148,7 @@ public enum Facility implements Comparable<Facility> {
148148
@NonNull
149149
private final String label;
150150

151-
private Facility(int numericalCode, @NonNull String label) {
151+
Facility(int numericalCode, @NonNull String label) {
152152
this.numericalCode = numericalCode;
153153
this.label = label;
154154
}
@@ -202,11 +202,6 @@ public String label() {
202202
* Compare on {@link Facility#numericalCode()}
203203
*/
204204
public static Comparator<Facility> comparator() {
205-
return new Comparator<Facility>() {
206-
@Override
207-
public int compare(Facility f1, Facility f2) {
208-
return Integer.compare(f1.numericalCode, f2.numericalCode);
209-
}
210-
};
205+
return Comparator.comparingInt(f -> f.numericalCode);
211206
}
212207
}

src/main/java/com/cloudbees/syslog/SDElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String getSdID() {
5858
return sdID;
5959
}
6060

61-
private List<SDParam> sdParams = new ArrayList<SDParam>();
61+
private List<SDParam> sdParams = new ArrayList<>();
6262

6363
/**
6464
* Get the value of sdParams

src/main/java/com/cloudbees/syslog/Severity.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public enum Severity {
6161
DEBUG(7, "DEBUG");
6262

6363
// mapping
64-
private final static Map<String, Severity> severityFromLabel = new HashMap<String, Severity>();
65-
private final static Map<Integer, Severity> severityFromNumericalCode = new HashMap<Integer, Severity>();
64+
private final static Map<String, Severity> severityFromLabel = new HashMap<>();
65+
private final static Map<Integer, Severity> severityFromNumericalCode = new HashMap<>();
6666

6767
static {
6868
for (Severity severity : Severity.values()) {
@@ -75,7 +75,7 @@ public enum Severity {
7575
@NonNull
7676
private final String label;
7777

78-
private Severity(int numericalCode, @NonNull String label) {
78+
Severity(int numericalCode, @NonNull String label) {
7979
this.numericalCode = numericalCode;
8080
this.label = label;
8181
}
@@ -129,12 +129,7 @@ public String label() {
129129
* Compare on {@link Severity#numericalCode()}
130130
*/
131131
public static Comparator<Severity> comparator() {
132-
return new Comparator<Severity>() {
133-
@Override
134-
public int compare(Severity s1, Severity s2) {
135-
return Integer.compare(s1.numericalCode, s2.numericalCode);
136-
}
137-
};
132+
return Comparator.comparingInt(s -> s.numericalCode);
138133
}
139134
}
140135

src/main/java/com/cloudbees/syslog/SyslogMessage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected String newObject() {
7474
TimeZone.getTimeZone("GMT"),
7575
concurrency);
7676

77-
/**
77+
/*
7878
* According to <a href="http://tools.ietf.org/html/rfc3164#section-4.1.2">RFC31614- 4.1.2 HEADER Part of a syslog Packet</a>,
7979
* we should use local time and not GMT.
8080
* <quote>
@@ -133,7 +133,7 @@ public Date getTimestamp() {
133133
}
134134

135135
public void setTimestamp(Date timestamp) {
136-
this.timestamp = (timestamp == null ? null : timestamp.getTime());
136+
this.timestamp = timestamp == null ? null : timestamp.getTime();
137137
}
138138

139139
public SyslogMessage withTimestamp(long timestamp) {
@@ -142,7 +142,7 @@ public SyslogMessage withTimestamp(long timestamp) {
142142
}
143143

144144
public SyslogMessage withTimestamp(Date timestamp) {
145-
this.timestamp = (timestamp == null ? null : timestamp.getTime());
145+
this.timestamp = timestamp == null ? null : timestamp.getTime();
146146
return this;
147147
}
148148

@@ -222,7 +222,7 @@ public SyslogMessage withMsg(final String msg) {
222222
public Set<SDElement> getSDElements() {
223223
Set<SDElement> ssde = sdElements;
224224
if (ssde == null) {
225-
ssde = new HashSet<SDElement>(0);
225+
ssde = new HashSet<>(0);
226226
}
227227
return ssde;
228228
}
@@ -233,7 +233,7 @@ public void setSDElements(Set<SDElement> ssde) {
233233

234234
public SyslogMessage withSDElement(SDElement sde) {
235235
if (sdElements == null) {
236-
sdElements = new HashSet<SDElement>();
236+
sdElements = new HashSet<>();
237237
}
238238
sdElements.add(sde);
239239
return this;
@@ -381,7 +381,7 @@ public void toRfc3164SyslogMessage(Writer out) throws IOException {
381381
out.write('>');
382382
out.write(rfc3164DateFormat.format(timestamp == null ? new Date() : new Date(timestamp))); // message time
383383
out.write(SP);
384-
out.write((hostname == null) ? localhostNameReference.get() : hostname); // emitting server hostname
384+
out.write(hostname == null ? localhostNameReference.get() : hostname); // emitting server hostname
385385
out.write(SP);
386386
writeNillableValue(appName, out); // appname
387387

src/main/java/com/cloudbees/syslog/integration/jul/AbstractHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727

2828
import edu.umd.cs.findbugs.annotations.NonNull;
2929
import edu.umd.cs.findbugs.annotations.Nullable;
30-
import java.util.logging.*;
30+
import java.util.logging.Filter;
31+
import java.util.logging.Formatter;
32+
import java.util.logging.Handler;
33+
import java.util.logging.Level;
34+
import java.util.logging.LogManager;
35+
import java.util.logging.LogRecord;
36+
import java.util.logging.SimpleFormatter;
3137

3238
/**
3339
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
@@ -69,6 +75,7 @@ protected Formatter getDefaultFormatter() {
6975
/**
7076
* {@inheritDoc}
7177
*/
78+
@Override
7279
public boolean isLoggable(LogRecord record) {
7380
if (record == null) {
7481
return false;
@@ -88,6 +95,7 @@ public Level getLevel() {
8895
* {@inheritDoc}
8996
*/
9097
@Nullable
98+
@Override
9199
public Filter getFilter() {
92100
return filter;
93101
}

src/main/java/com/cloudbees/syslog/integration/jul/SyslogHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333

3434
import edu.umd.cs.findbugs.annotations.NonNull;
3535
import edu.umd.cs.findbugs.annotations.Nullable;
36-
import java.io.Closeable;
3736
import java.io.IOException;
38-
import java.util.logging.*;
37+
import java.util.logging.Filter;
38+
import java.util.logging.Formatter;
39+
import java.util.logging.Level;
40+
import java.util.logging.LogManager;
41+
import java.util.logging.LogRecord;
3942

4043
/**
4144
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>

src/main/java/com/cloudbees/syslog/integration/jul/util/LevelHelper.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
import com.cloudbees.syslog.Severity;
2727

28-
import edu.umd.cs.findbugs.annotations.NonNull;
2928
import edu.umd.cs.findbugs.annotations.Nullable;
30-
import java.util.*;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.Comparator;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
3135
import java.util.logging.Level;
3236

3337
/**
@@ -43,8 +47,8 @@ public class LevelHelper {
4347
private final static Map<Level, Severity> julLevelToSyslogSeverity;
4448

4549
static {
46-
Map<String, Level> levelsByNameMap = new HashMap<String, Level>();
47-
Map<Integer, Level> levelsByValueMap = new HashMap<Integer, Level>();
50+
Map<String, Level> levelsByNameMap = new HashMap<>();
51+
Map<Integer, Level> levelsByValueMap = new HashMap<>();
4852
for (Level level : levels) {
4953
levelsByNameMap.put(level.getName(), level);
5054
levelsByValueMap.put(level.intValue(), level);
@@ -88,11 +92,6 @@ public static Severity toSeverity(@Nullable Level level) {
8892
* Compare on {@link java.util.logging.Level#intValue()}
8993
*/
9094
public static Comparator<Level> comparator() {
91-
return new Comparator<Level>() {
92-
@Override
93-
public int compare(Level l1, Level l2) {
94-
return Integer.compare(l1.intValue(), l2.intValue());
95-
}
96-
};
95+
return Comparator.comparingInt(Level::intValue);
9796
}
9897
}

src/main/java/com/cloudbees/syslog/integration/jul/util/LogManagerHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static Filter getFilterProperty(@NonNull LogManager manager, @Nullable St
6969
String val = manager.getProperty(name);
7070
try {
7171
if (val != null) {
72-
Class clz = ClassLoader.getSystemClassLoader().loadClass(val);
72+
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
7373
return (Filter) clz.newInstance();
7474
}
7575
} catch (Exception ex) {
@@ -96,7 +96,7 @@ public static Formatter getFormatterProperty(@NonNull LogManager manager, @Nulla
9696
String val = manager.getProperty(name);
9797
try {
9898
if (val != null) {
99-
Class clz = ClassLoader.getSystemClassLoader().loadClass(val);
99+
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
100100
return (Formatter) clz.newInstance();
101101
}
102102
} catch (Exception ex) {

src/main/java/com/cloudbees/syslog/sender/AbstractSyslogMessageSender.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.cloudbees.syslog.sender;
22

3-
import com.cloudbees.syslog.*;
3+
import com.cloudbees.syslog.Facility;
4+
import com.cloudbees.syslog.MessageFormat;
5+
import com.cloudbees.syslog.Severity;
6+
import com.cloudbees.syslog.SyslogMessage;
47
import com.cloudbees.syslog.util.InternalLogger;
58

69
import edu.umd.cs.findbugs.annotations.NonNull;
710
import java.io.CharArrayWriter;
811
import java.io.IOException;
912
import java.nio.charset.Charset;
13+
import java.nio.charset.StandardCharsets;
1014
import java.util.concurrent.TimeUnit;
1115
import java.util.concurrent.atomic.AtomicInteger;
1216
import java.util.concurrent.atomic.AtomicLong;
@@ -15,7 +19,11 @@
1519
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
1620
*/
1721
public abstract class AbstractSyslogMessageSender implements SyslogMessageSender {
18-
protected final static Charset UTF_8 = Charset.forName("UTF-8");
22+
/**
23+
* @deprecated use {@link StandardCharsets#UTF_8}
24+
*/
25+
@Deprecated
26+
protected final static Charset UTF_8 = StandardCharsets.UTF_8;
1927
protected final InternalLogger logger = InternalLogger.getLogger(getClass());
2028
// default values
2129
protected String defaultAppName;

src/main/java/com/cloudbees/syslog/sender/SyslogMessageSender.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
*/
3636
@ThreadSafe
3737
public interface SyslogMessageSender extends Closeable {
38-
public static final long DEFAULT_INET_ADDRESS_TTL_IN_MILLIS = TimeUnit.MILLISECONDS.convert(30, TimeUnit.SECONDS);
39-
public static final long DEFAULT_INET_ADDRESS_TTL_IN_NANOS = TimeUnit.NANOSECONDS.convert(DEFAULT_INET_ADDRESS_TTL_IN_MILLIS, TimeUnit.MILLISECONDS);
40-
public static final String DEFAULT_SYSLOG_HOST = "localhost";
41-
public static final MessageFormat DEFAULT_SYSLOG_MESSAGE_FORMAT = MessageFormat.RFC_3164;
42-
public static final int DEFAULT_SYSLOG_PORT = 514;
38+
long DEFAULT_INET_ADDRESS_TTL_IN_MILLIS = TimeUnit.MILLISECONDS.convert(30, TimeUnit.SECONDS);
39+
long DEFAULT_INET_ADDRESS_TTL_IN_NANOS = TimeUnit.NANOSECONDS.convert(DEFAULT_INET_ADDRESS_TTL_IN_MILLIS, TimeUnit.MILLISECONDS);
40+
String DEFAULT_SYSLOG_HOST = "localhost";
41+
MessageFormat DEFAULT_SYSLOG_MESSAGE_FORMAT = MessageFormat.RFC_3164;
42+
int DEFAULT_SYSLOG_PORT = 514;
4343

4444
/**
4545
* Send the given message ; the Syslog fields (appName, severity, priority, hostname ...) are the default values

0 commit comments

Comments
 (0)