Skip to content

Commit

Permalink
[SPARK-8301] removed null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekbecker committed Jun 18, 2015
1 parent 1c327eb commit 9ca0473
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
33 changes: 14 additions & 19 deletions unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.spark.unsafe.PlatformDependent;

Expand All @@ -34,7 +34,7 @@
*/
public final class UTF8String implements Comparable<UTF8String>, Serializable {

@Nonnull
@Nullable
private byte[] bytes;

private static int[] bytesOfCodePointInUTF8 = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
Expand All @@ -56,16 +56,12 @@ public static UTF8String fromString(String str) {
* Updates the UTF8String with String.
*/
protected UTF8String set(final String str) {
if (str == null) {
bytes = new byte[0];
} else {
try {
bytes = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// Turn the exception into unchecked so we can find out about it at runtime, but
// don't need to add lots of boilerplate code everywhere.
PlatformDependent.throwException(e);
}
try {
bytes = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// Turn the exception into unchecked so we can find out about it at runtime, but
// don't need to add lots of boilerplate code everywhere.
PlatformDependent.throwException(e);
}
return this;
}
Expand All @@ -74,7 +70,7 @@ protected UTF8String set(final String str) {
* Updates the UTF8String with byte[], which should be encoded in UTF-8.
*/
protected UTF8String set(final byte[] bytes) {
this.bytes = (bytes != null) ? bytes : new byte[0];
this.bytes = bytes;
return this;
}

Expand Down Expand Up @@ -129,7 +125,6 @@ public UTF8String substring(final int start, final int until) {
}

public boolean contains(final UTF8String substring) {
if (substring == null) return false;
final byte[] b = substring.getBytes();
if (b.length == 0) {
return true;
Expand All @@ -143,23 +138,23 @@ public boolean contains(final UTF8String substring) {
return false;
}

private boolean startsWith(final byte[] prefix, int offset) {
if (prefix.length + offset > bytes.length || offset < 0) {
private boolean startsWith(final byte[] prefix, int offsetInBytes) {
if (prefix.length + offsetInBytes > bytes.length || offsetInBytes < 0) {
return false;
}
int i = 0;
while (i < prefix.length && prefix[i] == bytes[i + offset]) {
while (i < prefix.length && prefix[i] == bytes[i + offsetInBytes]) {
i++;
}
return i == prefix.length;
}

public boolean startsWith(final UTF8String prefix) {
return prefix != null && startsWith(prefix.getBytes(), 0);
return startsWith(prefix.getBytes(), 0);
}

public boolean endsWith(final UTF8String suffix) {
return suffix != null && startsWith(suffix.getBytes(), bytes.length - suffix.getBytes().length);
return startsWith(suffix.getBytes(), bytes.length - suffix.getBytes().length);
}

public UTF8String toUpperCase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void basicTest() throws UnsupportedEncodingException {

@Test
public void contains() {
Assert.assertFalse(UTF8String.fromString("hello").contains(null));
Assert.assertTrue(UTF8String.fromString("hello").contains(UTF8String.fromString("ello")));
Assert.assertFalse(UTF8String.fromString("hello").contains(UTF8String.fromString("vello")));
Assert.assertFalse(UTF8String.fromString("hello").contains(UTF8String.fromString("hellooo")));
Expand All @@ -58,7 +57,6 @@ public void contains() {

@Test
public void startsWith() {
Assert.assertFalse(UTF8String.fromString("hello").startsWith(null));
Assert.assertTrue(UTF8String.fromString("hello").startsWith(UTF8String.fromString("hell")));
Assert.assertFalse(UTF8String.fromString("hello").startsWith(UTF8String.fromString("ell")));
Assert.assertFalse(UTF8String.fromString("hello").startsWith(UTF8String.fromString("hellooo")));
Expand All @@ -70,7 +68,6 @@ public void startsWith() {

@Test
public void endsWith() {
Assert.assertFalse(UTF8String.fromString("hello").endsWith(null));
Assert.assertTrue(UTF8String.fromString("hello").endsWith(UTF8String.fromString("ello")));
Assert.assertFalse(UTF8String.fromString("hello").endsWith(UTF8String.fromString("ellov")));
Assert.assertFalse(UTF8String.fromString("hello").endsWith(UTF8String.fromString("hhhello")));
Expand Down

0 comments on commit 9ca0473

Please sign in to comment.