Skip to content

Commit d6d472d

Browse files
committed
Merge pull request organicveggie#9 from dclements/checkstyle
Cleanup with an emphasis on checkstyle.
2 parents d7d5dc0 + 8dbbe33 commit d6d472d

File tree

10 files changed

+727
-764
lines changed

10 files changed

+727
-764
lines changed

build.gradle

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ buildscript {
77
}
88
}
99

10+
defaultTasks 'build'
11+
1012
subprojects {
1113

1214
apply plugin: 'java'
@@ -48,21 +50,21 @@ subprojects {
4850
}
4951

5052
dependencies {
51-
compile ([
53+
compile (
5254
'org.slf4j:slf4j-api:1.7.5',
53-
])
55+
)
5456

55-
optional ([
57+
optional (
5658
'com.google.code.findbugs:jsr305:2.0.1',
5759
'com.google.code.findbugs:annotations:2.0.1',
58-
])
60+
)
5961

60-
testCompile ([
62+
testCompile (
6163
'junit:junit:4.11',
6264
'org.mockito:mockito-all:1.9.5',
6365
'org.slf4j:slf4j-jdk14:1.7.5',
6466
'org.easytesting:fest-assert-core:2.0M10',
65-
])
67+
)
6668
}
6769

6870
tasks.withType(Compile) {
@@ -78,6 +80,7 @@ subprojects {
7880

7981
findbugs {
8082
toolVersion = '2.0.1'
83+
sourceSets = [sourceSets.main]
8184
ignoreFailures = true
8285
}
8386

@@ -89,8 +92,6 @@ subprojects {
8992
}
9093
}
9194

92-
defaultTasks 'build'
93-
9495
jacocoTestReport.dependsOn(test)
9596
build.dependsOn javadoc
9697
build.dependsOn jacocoTestReport

config/checkstyle/checkstyle.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
</module>
7070
<module name="SimplifyBooleanExpression"/>
7171
<module name="SimplifyBooleanReturn"/>
72-
<module name="FinalClass"/>
7372
<module name="HideUtilityClassConstructor"/>
7473
<module name="InterfaceIsType"/>
7574
<module name="VisibilityModifier"/>
@@ -102,4 +101,9 @@
102101
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
103102
</module>
104103
<module name="SuppressWithNearbyCommentFilter"/>
104+
<module name="SuppressionCommentFilter">
105+
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>
106+
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>
107+
<property name="checkFormat" value="$1"/>
108+
</module>
105109
</module>

metrics3-statsd/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
version = '3.0.0-SNAPSHOT'
22

33
dependencies {
4-
provided 'com.codahale.metrics:metrics-core:3.0.0'
4+
provided (
5+
'com.codahale.metrics:metrics-core:3.0.0'
6+
)
7+
}
8+
9+
checkstyle {
10+
showViolations = true
511
}

metrics3-statsd/src/main/java/com/readytalk/metrics/DatagramSocketFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import java.net.SocketException;
77

88
public class DatagramSocketFactory {
9-
public DatagramSocket createSocket() throws SocketException {
10-
return new DatagramSocket();
11-
}
9+
public DatagramSocket createSocket() throws SocketException {
10+
return new DatagramSocket();
11+
}
1212

13-
public DatagramPacket createPacket(byte[] bytes, int length, InetSocketAddress address) throws SocketException {
14-
return new DatagramPacket(bytes, length, address);
15-
}
13+
public DatagramPacket createPacket(final byte[] bytes, final int length, final InetSocketAddress address)
14+
throws SocketException {
15+
return new DatagramPacket(bytes, length, address);
16+
}
1617
}
Lines changed: 96 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,110 @@
11
package com.readytalk.metrics;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
5-
3+
import javax.annotation.concurrent.NotThreadSafe;
64
import java.io.Closeable;
75
import java.io.IOException;
86
import java.net.DatagramSocket;
97
import java.net.InetSocketAddress;
108
import java.nio.charset.Charset;
119
import java.util.regex.Pattern;
1210

11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
1314
/**
1415
* A client to a StatsD server.
1516
*/
17+
@NotThreadSafe
1618
public class StatsD implements Closeable {
1719

18-
private static final Logger LOG = LoggerFactory.getLogger(StatsD.class);
19-
20-
private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
21-
22-
private static final Charset UTF_8 = Charset.forName("UTF-8");
23-
24-
private final InetSocketAddress address;
25-
private final DatagramSocketFactory socketFactory;
26-
27-
private DatagramSocket socket;
28-
private int failures;
29-
30-
/**
31-
* Creates a new client which connects to the given address using the default
32-
* {@link DatagramSocketFactory}.
33-
*
34-
* @param address the address of the StatsD server
35-
*/
36-
public StatsD(InetSocketAddress address) {
37-
this(address, new DatagramSocketFactory());
38-
}
39-
40-
/**
41-
* Creates a new client which connects to the given address and socket factory.
42-
*
43-
* @param address the address of the Carbon server
44-
* @param socketFactory the socket factory
45-
*/
46-
public StatsD(InetSocketAddress address, DatagramSocketFactory socketFactory) {
47-
this.address = address;
48-
this.socketFactory = socketFactory;
49-
}
50-
51-
/**
52-
* Connects to the server.
53-
*
54-
* @throws IllegalStateException if the client is already connected
55-
* @throws IOException if there is an error connecting
56-
*/
57-
public void connect() throws IllegalStateException, IOException {
58-
if (socket != null) {
59-
throw new IllegalStateException("Already connected");
60-
}
61-
62-
this.socket = socketFactory.createSocket();
63-
}
64-
65-
/**
66-
* Sends the given measurement to the server.
67-
*
68-
* @param name the name of the metric
69-
* @param value the value of the metric
70-
* @throws IOException if there was an error sending the metric
71-
*/
72-
public void send(String name, String value) throws IOException {
73-
try {
74-
String formatted = String.format("%s:%s|g", sanitize(name), sanitize(value));
75-
byte[] bytes = formatted.getBytes(UTF_8);
76-
socket.send(socketFactory.createPacket(bytes, bytes.length, address));
77-
failures = 0;
78-
} catch (IOException e) {
79-
failures++;
80-
81-
if (failures == 1) {
82-
LOG.warn("unable to send packet to statsd at '{}:{}'", address.getHostName(), address.getPort());
83-
} else {
84-
LOG.debug("unable to send packet to statsd at '{}:{}'", address.getHostName(), address.getPort());
85-
}
86-
87-
throw e;
88-
}
89-
}
90-
91-
/**
92-
* Returns the number of failed writes to the server.
93-
*
94-
* @return the number of failed writes to the server
95-
*/
96-
public int getFailures() {
97-
return failures;
98-
}
99-
100-
@Override
101-
public void close() throws IOException {
102-
if (socket != null) {
103-
socket.close();
104-
}
105-
this.socket = null;
106-
}
107-
108-
protected String sanitize(String s) {
109-
return WHITESPACE.matcher(s).replaceAll("-");
110-
}
111-
}
20+
private static final Logger LOG = LoggerFactory.getLogger(StatsD.class);
21+
22+
private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
23+
24+
private static final Charset UTF_8 = Charset.forName("UTF-8");
25+
26+
private final InetSocketAddress address;
27+
private final DatagramSocketFactory socketFactory;
28+
29+
private DatagramSocket socket;
30+
private int failures;
31+
32+
/**
33+
* Creates a new client which connects to the given address using the default
34+
* {@link DatagramSocketFactory}.
35+
*
36+
* @param address the address of the StatsD server
37+
*/
38+
public StatsD(final InetSocketAddress address) {
39+
this(address, new DatagramSocketFactory());
40+
}
41+
42+
/**
43+
* Creates a new client which connects to the given address and socket factory.
44+
*
45+
* @param address the address of the Carbon server
46+
* @param socketFactory the socket factory
47+
*/
48+
public StatsD(final InetSocketAddress address, final DatagramSocketFactory socketFactory) {
49+
this.address = address;
50+
this.socketFactory = socketFactory;
51+
}
52+
53+
/**
54+
* Connects to the server.
55+
*
56+
* @throws IllegalStateException if the client is already connected
57+
* @throws IOException if there is an error connecting
58+
*/
59+
public void connect() throws IOException {
60+
if (socket != null) {
61+
throw new IllegalStateException("Already connected");
62+
}
63+
64+
this.socket = socketFactory.createSocket();
65+
}
66+
67+
/**
68+
* Sends the given measurement to the server. Logs exceptions.
69+
*
70+
* @param name the name of the metric
71+
* @param value the value of the metric
72+
*/
73+
public void send(final String name, final String value) {
74+
try {
75+
String formatted = String.format("%s:%s|g", sanitize(name), sanitize(value));
76+
byte[] bytes = formatted.getBytes(UTF_8);
77+
socket.send(socketFactory.createPacket(bytes, bytes.length, address));
78+
failures = 0;
79+
} catch (IOException e) {
80+
failures++;
81+
82+
if (failures == 1) {
83+
LOG.warn("unable to send packet to statsd at '{}:{}'", address.getHostName(), address.getPort());
84+
} else {
85+
LOG.debug("unable to send packet to statsd at '{}:{}'", address.getHostName(), address.getPort());
86+
}
87+
}
88+
}
89+
90+
/**
91+
* Returns the number of failed writes to the server.
92+
*
93+
* @return the number of failed writes to the server
94+
*/
95+
public int getFailures() {
96+
return failures;
97+
}
98+
99+
@Override
100+
public void close() throws IOException {
101+
if (socket != null) {
102+
socket.close();
103+
}
104+
this.socket = null;
105+
}
106+
107+
private String sanitize(final String s) {
108+
return WHITESPACE.matcher(s).replaceAll("-");
109+
}
110+
}

0 commit comments

Comments
 (0)