Skip to content

Commit

Permalink
Fix a bunch of FindBugs and IntelliJ inspections
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed Apr 24, 2015
1 parent 29a7575 commit ef6b3d3
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
*/
public final class LongArray {

private static final int WIDTH = 8;
// This is a long so that we perform long multiplications when computing offsets.
private static final long WIDTH = 8;

private final MemoryBlock memory;
private final Object baseObj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ public BytesToBytesMap(
}

@Override
public void finalize() {
// In case the programmer forgot to call `free()`, try to perform that cleanup now:
free();
protected void finalize() throws Throwable {
try {
// In case the programmer forgot to call `free()`, try to perform that cleanup now:
free();
} finally {
super.finalize();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public interface MemoryAllocator {
* Allocates a contiguous block of memory. Note that the allocated memory is not guaranteed
* to be zeroed out (call `zero()` on the result if this is necessary).
*/
public MemoryBlock allocate(long size) throws OutOfMemoryError;
MemoryBlock allocate(long size) throws OutOfMemoryError;

public void free(MemoryBlock memory);
void free(MemoryBlock memory);

public static final MemoryAllocator UNSAFE = new UnsafeMemoryAllocator();
MemoryAllocator UNSAFE = new UnsafeMemoryAllocator();

public static final MemoryAllocator HEAP = new HeapMemoryAllocator();
MemoryAllocator HEAP = new HeapMemoryAllocator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class MemoryBlock extends MemoryLocation {

final long length;
private final long length;

MemoryBlock(@Nullable Object obj, long offset, long length) {
super(obj, offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
public class MemoryLocation {

@Nullable
protected Object obj;
Object obj;

protected long offset;
long offset;

public MemoryLocation(@Nullable Object obj, long offset) {
this.obj = obj;
Expand All @@ -44,10 +44,6 @@ public void setObjAndOffset(Object newObj, long newOffset) {
this.offset = newOffset;
}

public void setOffset(long newOffset) {
this.offset = newOffset;
}

public final Object getBaseObject() {
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static int getLengthInCodePoints(Object baseObject, long baseOffset, int
}

public static String toJavaString(Object baseObject, long baseOffset, int lengthInBytes) {
final byte[] bytes = new byte[(int) lengthInBytes];
final byte[] bytes = new byte[lengthInBytes];
PlatformDependent.UNSAFE.copyMemory(
baseObject,
baseOffset,
Expand All @@ -129,8 +129,11 @@ public static String toJavaString(Object baseObject, long baseOffset, int length
*
* @return the number of bytes written, including the space for tracking the string's length.
*/
public static int createFromJavaString(Object baseObject, long baseOffset, String str) {
final byte[] strBytes = str.getBytes();
public static int createFromJavaString(
Object baseObject,
long baseOffset,
String str) throws UnsupportedEncodingException {
final byte[] strBytes = str.getBytes("utf-8");
final int strLengthInBytes = strBytes.length;
PlatformDependent.copyMemory(
strBytes,
Expand Down Expand Up @@ -159,7 +162,7 @@ public static int numOfBytes(byte b) {
* number of tailing bytes in a UTF8 sequence for a code point
* see http://en.wikipedia.org/wiki/UTF-8, 192-256 of Byte 1
*/
private static int[] bytesOfCodePointInUTF8 = new int[] {
private static final int[] bytesOfCodePointInUTF8 = new int[] {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class TestLongArray {

private LongArray createTestData() {
private static LongArray createTestData() {
byte[] bytes = new byte[16];
LongArray arr = new LongArray(MemoryBlock.fromByteArray(bytes));
arr.set(0, 1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class TestBitSet {

private BitSet createBitSet(int capacity) {
private static BitSet createBitSet(int capacity) {
assert capacity % 64 == 0;
return new BitSet(MemoryBlock.fromLongArray(new long[capacity / 64]).zero());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public class TestMurmur3_x86_32 {

private static Murmur3_x86_32 hasher = new Murmur3_x86_32(0);
private static final Murmur3_x86_32 hasher = new Murmur3_x86_32(0);

@Test
public void testKnownIntegerInputs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@

package org.apache.spark.unsafe.map;

import java.lang.Exception;
import java.nio.ByteBuffer;
import java.util.*;

import org.junit.Assert;
import org.junit.Test;

import org.apache.spark.unsafe.array.ByteArrayMethods;
import org.apache.spark.unsafe.PlatformDependent;
import org.apache.spark.unsafe.memory.MemoryAllocator;
import org.apache.spark.unsafe.memory.MemoryLocation;
import static org.apache.spark.unsafe.PlatformDependent.BYTE_ARRAY_OFFSET;
import org.junit.Assert;
import org.junit.Test;

import java.lang.Exception;
import java.lang.IllegalStateException;
import java.nio.ByteBuffer;
import java.util.*;

public abstract class AbstractTestBytesToBytesMap {

protected final Random rand = new Random(42);
private final Random rand = new Random(42);

protected final MemoryAllocator allocator = getMemoryAllocator();
private final MemoryAllocator allocator = getMemoryAllocator();

protected abstract MemoryAllocator getMemoryAllocator();

protected byte[] getByteArray(MemoryLocation loc, int size) {
private static byte[] getByteArray(MemoryLocation loc, int size) {
final byte[] arr = new byte[size];
PlatformDependent.UNSAFE.copyMemory(
loc.getBaseObject(),
Expand All @@ -50,7 +50,7 @@ protected byte[] getByteArray(MemoryLocation loc, int size) {
return arr;
}

protected byte[] getRandomByteArray(int numWords) {
private byte[] getRandomByteArray(int numWords) {
Assert.assertTrue(numWords > 0);
final int lengthInBytes = numWords * 8;
final byte[] bytes = new byte[lengthInBytes];
Expand All @@ -62,7 +62,7 @@ protected byte[] getRandomByteArray(int numWords) {
* Fast equality checking for byte arrays, since these comparisons are a bottleneck
* in our stress tests.
*/
protected boolean arrayEquals(
private static boolean arrayEquals(
byte[] expected,
MemoryLocation actualAddr,
long actualLengthBytes) {
Expand Down

0 comments on commit ef6b3d3

Please sign in to comment.