Skip to content

Commit efdf9f0

Browse files
committed
clean up formatting, use commons utils for hashcode/equals.
1 parent 115ba3c commit efdf9f0

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

src/com/googlecode/jmxtrans/model/output/GangliaWriter.java

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.googlecode.jmxtrans.model.output;
22

33
import java.io.IOException;
4-
import java.net.*;
4+
import java.net.DatagramPacket;
5+
import java.net.DatagramSocket;
6+
import java.net.InetAddress;
7+
import java.net.InetSocketAddress;
8+
import java.net.UnknownHostException;
59
import java.util.HashSet;
610
import java.util.List;
711
import java.util.Map;
@@ -11,6 +15,8 @@
1115
import java.util.regex.Pattern;
1216

1317
import org.apache.commons.lang.StringUtils;
18+
import org.apache.commons.lang.builder.EqualsBuilder;
19+
import org.apache.commons.lang.builder.HashCodeBuilder;
1420
import org.apache.commons.pool.KeyedObjectPool;
1521
import org.slf4j.Logger;
1622
import org.slf4j.LoggerFactory;
@@ -290,26 +296,26 @@ private void sendMetricMetadata(DatagramSocket socket, MetricMetaData metaData)
290296
/**
291297
* Puts a string into the buffer by first writing the size of the string as
292298
* an int, followed by the bytes of the string, padded if necessary to a
293-
* multiple of 4.
294-
* If the String value exceeds the available length of the buffer, this will
295-
* trim the String to fit and add an ellipsis at the end.
299+
* multiple of 4. If the String value exceeds the available length of the
300+
* buffer, this will trim the String to fit and add an ellipsis at the end.
296301
*/
297302
protected void xdr_string(String s) {
298303
byte[] bytes = s.getBytes();
299304
int len = bytes.length;
300-
// The available buffer is equal to the total buffer size, minus our current offset, minus 4 bytes
305+
// The available buffer is equal to the total buffer size, minus our
306+
// current offset, minus 4 bytes
301307
// to write the xdr integer length value
302308
int availableBuffer = BUFFER_SIZE - offset - 4;
303-
if( len < availableBuffer ) {
309+
if (len < availableBuffer) {
304310
xdr_int(len);
305311
System.arraycopy(bytes, 0, buffer, offset, len);
306312
offset += len;
307313
} else {
308-
xdr_int( availableBuffer );
309-
bytes[ availableBuffer - 1 ] = '.';
310-
bytes[ availableBuffer - 2 ] = '.';
311-
bytes[ availableBuffer - 3 ] = '.';
312-
System.arraycopy( bytes, 0, buffer, offset, availableBuffer );
314+
xdr_int(availableBuffer);
315+
bytes[availableBuffer - 1] = '.';
316+
bytes[availableBuffer - 2] = '.';
317+
bytes[availableBuffer - 3] = '.';
318+
System.arraycopy(bytes, 0, buffer, offset, availableBuffer);
313319
offset += availableBuffer;
314320
}
315321
pad();
@@ -348,25 +354,28 @@ private MetricMetaData(String hostName, String metricName, DataType type) {
348354
}
349355

350356
public boolean equals(Object o) {
351-
if (this == o)
352-
return true;
353-
if (o == null || getClass() != o.getClass())
354-
return false;
355-
MetricMetaData that = (MetricMetaData) o;
356-
if (hostName != null ? !hostName.equals(that.hostName) : that.hostName != null)
357+
if (o == null) {
357358
return false;
358-
if (metricName != null ? !metricName.equals(that.metricName) : that.metricName != null)
359+
}
360+
if (o == this) {
361+
return true;
362+
}
363+
if (o.getClass() != this.getClass()) {
359364
return false;
360-
if (type != that.type)
365+
}
366+
367+
if (!(o instanceof MetricMetaData)) {
361368
return false;
362-
return true;
369+
}
370+
371+
MetricMetaData other = (MetricMetaData) o;
372+
373+
return new EqualsBuilder().append(this.hostName, other.hostName).append(this.metricName, other.metricName).append(this.type, other.type)
374+
.isEquals();
363375
}
364376

365377
public int hashCode() {
366-
int result = hostName != null ? hostName.hashCode() : 0;
367-
result = 31 * result + (metricName != null ? metricName.hashCode() : 0);
368-
result = 31 * result + (type != null ? type.hashCode() : 0);
369-
return result;
378+
return new HashCodeBuilder(41, 97).append(hostName).append(metricName).append(type).toHashCode();
370379
}
371380

372381
public String toString() {

test/com/googlecode/jmxtrans/model/output/GangliaWriterTests.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,35 @@
66

77
/**
88
* @author Zack Radick
9-
* Date: 1/25/12
109
*/
1110
public class GangliaWriterTests {
12-
11+
1312
@Test
1413
public void testBufferOverflowOnLongString() {
15-
final TestGangliaWriter writer = new TestGangliaWriter();
16-
final int dataSize = 99999;
17-
final byte[] overflow = new byte[dataSize]; // Buffer size is 1500, overflow by a large margin
18-
for( int i = 0; i < dataSize; i++ ) {
14+
TestGangliaWriter writer = new TestGangliaWriter();
15+
int dataSize = 99999;
16+
byte[] overflow = new byte[dataSize]; // Buffer size is 1500, overflow
17+
// by a large margin
18+
for (int i = 0; i < dataSize; i++) {
1919
overflow[i] = 'z';
2020
}
21-
writer.xdr_string( new String( overflow ) );
21+
writer.xdr_string(new String(overflow));
2222
final byte[] buffer = writer.getBuffer();
23-
// The first four bytes are the int value of the String length, which should be equal to the buffer size - 4
23+
// The first four bytes are the int value of the String length, which
24+
// should be equal to the buffer size - 4
2425
int valueLength = 0;
25-
for( int i = 0; i < 4; i++ ) {
26-
valueLength += ( buffer[ i ] & 0xff ) << ( 8 * ( 3 - i ) );
26+
for (int i = 0; i < 4; i++) {
27+
valueLength += (buffer[i] & 0xff) << (8 * (3 - i));
2728
}
28-
assertEquals( buffer.length - 4, valueLength );
29-
// The remaining bytes in the buffer should be equal to the characters put into the byte array
29+
assertEquals(buffer.length - 4, valueLength);
30+
// The remaining bytes in the buffer should be equal to the characters
31+
// put into the byte array
3032
// with the final three characters being an ellipsis
31-
for( int i = 4; i < buffer.length; i++ ) {
32-
assertEquals( i < buffer.length - 3 ? 'z' : '.', buffer[ i ] );
33+
for (int i = 4; i < buffer.length; i++) {
34+
assertEquals(i < buffer.length - 3 ? 'z' : '.', buffer[i]);
3335
}
3436
}
35-
37+
3638
private class TestGangliaWriter extends GangliaWriter {
3739
public byte[] getBuffer() {
3840
return buffer;

0 commit comments

Comments
 (0)