From daf4ae2b24ed6aa55334cbe3e991de7bdbd253c0 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Thu, 24 Jan 2019 13:26:39 +0100 Subject: [PATCH 1/2] BytesLocation: Add method to set the name for the Location In some instances libarary users want to create a BytesLocation that has a specific name, e.g. so that it is correctly detected by a Service that performs a name based lookup. --- .../scijava/io/location/BytesLocation.java | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/scijava/io/location/BytesLocation.java b/src/main/java/org/scijava/io/location/BytesLocation.java index 43c08e659..41980b0fd 100644 --- a/src/main/java/org/scijava/io/location/BytesLocation.java +++ b/src/main/java/org/scijava/io/location/BytesLocation.java @@ -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; /** @@ -47,6 +46,8 @@ public class BytesLocation extends AbstractLocation { private final ByteBank bytes; + private final String name; + /** * Creates a {@link BytesLocation} backed by the specified * {@link ByteBank}. @@ -54,7 +55,18 @@ public class BytesLocation extends AbstractLocation { * @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; } /** @@ -63,7 +75,19 @@ 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; } /** @@ -71,7 +95,18 @@ public BytesLocation(final int initialCapacity) { * 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; } /** @@ -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; } /** @@ -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 -- @@ -106,6 +167,11 @@ public ByteBank getByteBank() { return bytes; } + @Override + public String getName() { + return name != null ? name : defaultName(); + } + // -- Object methods -- @Override From 80f96e74b2b6cbcf02662ba8c1b94e189fb37baf Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Wed, 23 Jan 2019 15:44:53 +0100 Subject: [PATCH 2/2] BytesLocation: add test for setName() --- .../io/location/BytesLocationTest.java | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/scijava/io/location/BytesLocationTest.java b/src/test/java/org/scijava/io/location/BytesLocationTest.java index f95144ca3..addac1dbb 100644 --- a/src/test/java/org/scijava/io/location/BytesLocationTest.java +++ b/src/test/java/org/scijava/io/location/BytesLocationTest.java @@ -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 @@ -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 { @@ -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()); + } + }