Skip to content

BytesLocation: Add method to set the name for the Location #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions src/main/java/org/scijava/io/location/BytesLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.scijava.io.ByteArrayByteBank;
import org.scijava.io.ByteBank;
import org.scijava.io.handle.DataHandle;
import org.scijava.util.ByteArray;

/**
Expand All @@ -47,14 +46,27 @@ public class BytesLocation extends AbstractLocation {

private final ByteBank bytes;

private final String name;

/**
* Creates a {@link BytesLocation} backed by the specified
* {@link ByteBank}.
*
* @param bytes the {@link ByteBank} that will back this {@link Location}
*/
public BytesLocation(final ByteBank bytes) {
this(bytes, null);
}

/**
* Creates a {@link BytesLocation} backed by the specified {@link ByteBank}.
*
* @param bytes the {@link ByteBank} that will back this {@link Location}
* @param name the name of this {@link Location}
*/
public BytesLocation(final ByteBank bytes, final String name) {
this.bytes = bytes;
this.name = name;
}

/**
Expand All @@ -63,15 +75,38 @@ public BytesLocation(final ByteBank bytes) {
* can be used to avoid needing to grow the underlying {@link ByteBank}.
*/
public BytesLocation(final int initialCapacity) {
this(initialCapacity, null);
}

/**
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} with
* the specified initial capacity, but with a reported size of 0. This method
* can be used to avoid needing to grow the underlying {@link ByteBank}.
*
* @param name the name of this {@link Location}
*/
public BytesLocation(final int initialCapacity, final String name) {
this.bytes = new ByteArrayByteBank(initialCapacity);
this.name = name;
}

/**
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank}
* that wraps the specified {@link ByteArray}.
*/
public BytesLocation(final ByteArray bytes) {
this(bytes, null);
}

/**
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} that
* wraps the specified {@link ByteArray}.
*
* @param name the name of this Location.
*/
public BytesLocation(final ByteArray bytes, final String name) {
this.bytes = new ByteArrayByteBank(bytes);
this.name = name;
}

/**
Expand All @@ -81,7 +116,19 @@ public BytesLocation(final ByteArray bytes) {
* @param bytes the array to wrap
*/
public BytesLocation(final byte[] bytes) {
this(bytes, null);
}

/**
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} which
* wraps the specified array.
*
* @param bytes the array to wrap
* @param name the name of this Location.
*/
public BytesLocation(final byte[] bytes, final String name) {
this.bytes = new ByteArrayByteBank(bytes);
this.name = name;
}

/**
Expand All @@ -92,11 +139,25 @@ public BytesLocation(final byte[] bytes) {
* @param offset the offset in the bytes array to start copying from
* @param length the number of bytes to copy, starting from the offset
*/
public BytesLocation(final byte[] bytes, final int offset,
final int length)
public BytesLocation(final byte[] bytes, final int offset, final int length) {
this(bytes, offset, length, null);
}

/**
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} with
* the specified initial capacity and the provided data.
*
* @param bytes the bytes to copy into the new {@link BytesLocation}
* @param offset the offset in the bytes array to start copying from
* @param length the number of bytes to copy, starting from the offset
* @param name the name of this Location.
*/
public BytesLocation(final byte[] bytes, final int offset, final int length,
final String name)
{
this.bytes = new ByteArrayByteBank(length);
this.bytes.setBytes(0l, bytes, offset, length);
this.name = name;
}

// -- BytesLocation methods --
Expand All @@ -106,6 +167,11 @@ public ByteBank getByteBank() {
return bytes;
}

@Override
public String getName() {
return name != null ? name : defaultName();
}

// -- Object methods --

@Override
Expand Down
35 changes: 32 additions & 3 deletions src/test/java/org/scijava/io/location/BytesLocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -36,10 +36,12 @@
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.scijava.io.ByteArrayByteBank;
import org.scijava.util.ByteArray;

/**
* Tests {@link BytesLocation}.
*
*
* @author Curtis Rueden
*/
public class BytesLocationTest {
Expand Down Expand Up @@ -72,4 +74,31 @@ public void testBytesOffsetLength() {
assertArrayEquals(expectedDigits, testDigits);
}

/**
* Tests getName()
*/
@Test
public void getNameTest() {

final BytesLocation loc1 = new BytesLocation(0);
assertEquals(loc1.defaultName(), loc1.getName());
assertEquals("Location.defaultName", loc1.defaultName());

final String expectedName = "test.name";
BytesLocation loc2 = new BytesLocation(0, expectedName);
assertEquals(expectedName, loc2.getName());

loc2 = new BytesLocation(new byte[0], expectedName);
assertEquals(expectedName, loc2.getName());

loc2 = new BytesLocation(new ByteArray(), expectedName);
assertEquals(expectedName, loc2.getName());

loc2 = new BytesLocation(new ByteArrayByteBank(), expectedName);
assertEquals(expectedName, loc2.getName());

loc2 = new BytesLocation(new byte[0], 0, 0, expectedName);
assertEquals(expectedName, loc2.getName());
}

}