Skip to content

Commit f329e5b

Browse files
authored
Merge pull request #349 from scijava/add-name-to-bytes-location
BytesLocation: Add method to set the name for the Location
2 parents 821f89f + 80f96e7 commit f329e5b

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

src/main/java/org/scijava/io/location/BytesLocation.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import org.scijava.io.ByteArrayByteBank;
3636
import org.scijava.io.ByteBank;
37-
import org.scijava.io.handle.DataHandle;
3837
import org.scijava.util.ByteArray;
3938

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

4847
private final ByteBank bytes;
4948

49+
private final String name;
50+
5051
/**
5152
* Creates a {@link BytesLocation} backed by the specified
5253
* {@link ByteBank}.
5354
*
5455
* @param bytes the {@link ByteBank} that will back this {@link Location}
5556
*/
5657
public BytesLocation(final ByteBank bytes) {
58+
this(bytes, null);
59+
}
60+
61+
/**
62+
* Creates a {@link BytesLocation} backed by the specified {@link ByteBank}.
63+
*
64+
* @param bytes the {@link ByteBank} that will back this {@link Location}
65+
* @param name the name of this {@link Location}
66+
*/
67+
public BytesLocation(final ByteBank bytes, final String name) {
5768
this.bytes = bytes;
69+
this.name = name;
5870
}
5971

6072
/**
@@ -63,15 +75,38 @@ public BytesLocation(final ByteBank bytes) {
6375
* can be used to avoid needing to grow the underlying {@link ByteBank}.
6476
*/
6577
public BytesLocation(final int initialCapacity) {
78+
this(initialCapacity, null);
79+
}
80+
81+
/**
82+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} with
83+
* the specified initial capacity, but with a reported size of 0. This method
84+
* can be used to avoid needing to grow the underlying {@link ByteBank}.
85+
*
86+
* @param name the name of this {@link Location}
87+
*/
88+
public BytesLocation(final int initialCapacity, final String name) {
6689
this.bytes = new ByteArrayByteBank(initialCapacity);
90+
this.name = name;
6791
}
6892

6993
/**
7094
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank}
7195
* that wraps the specified {@link ByteArray}.
7296
*/
7397
public BytesLocation(final ByteArray bytes) {
98+
this(bytes, null);
99+
}
100+
101+
/**
102+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} that
103+
* wraps the specified {@link ByteArray}.
104+
*
105+
* @param name the name of this Location.
106+
*/
107+
public BytesLocation(final ByteArray bytes, final String name) {
74108
this.bytes = new ByteArrayByteBank(bytes);
109+
this.name = name;
75110
}
76111

77112
/**
@@ -81,7 +116,19 @@ public BytesLocation(final ByteArray bytes) {
81116
* @param bytes the array to wrap
82117
*/
83118
public BytesLocation(final byte[] bytes) {
119+
this(bytes, null);
120+
}
121+
122+
/**
123+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} which
124+
* wraps the specified array.
125+
*
126+
* @param bytes the array to wrap
127+
* @param name the name of this Location.
128+
*/
129+
public BytesLocation(final byte[] bytes, final String name) {
84130
this.bytes = new ByteArrayByteBank(bytes);
131+
this.name = name;
85132
}
86133

87134
/**
@@ -92,11 +139,25 @@ public BytesLocation(final byte[] bytes) {
92139
* @param offset the offset in the bytes array to start copying from
93140
* @param length the number of bytes to copy, starting from the offset
94141
*/
95-
public BytesLocation(final byte[] bytes, final int offset,
96-
final int length)
142+
public BytesLocation(final byte[] bytes, final int offset, final int length) {
143+
this(bytes, offset, length, null);
144+
}
145+
146+
/**
147+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} with
148+
* the specified initial capacity and the provided data.
149+
*
150+
* @param bytes the bytes to copy into the new {@link BytesLocation}
151+
* @param offset the offset in the bytes array to start copying from
152+
* @param length the number of bytes to copy, starting from the offset
153+
* @param name the name of this Location.
154+
*/
155+
public BytesLocation(final byte[] bytes, final int offset, final int length,
156+
final String name)
97157
{
98158
this.bytes = new ByteArrayByteBank(length);
99159
this.bytes.setBytes(0l, bytes, offset, length);
160+
this.name = name;
100161
}
101162

102163
// -- BytesLocation methods --
@@ -106,6 +167,11 @@ public ByteBank getByteBank() {
106167
return bytes;
107168
}
108169

170+
@Override
171+
public String getName() {
172+
return name != null ? name : defaultName();
173+
}
174+
109175
// -- Object methods --
110176

111177
@Override

src/test/java/org/scijava/io/location/BytesLocationTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* %%
1010
* Redistribution and use in source and binary forms, with or without
1111
* modification, are permitted provided that the following conditions are met:
12-
*
12+
*
1313
* 1. Redistributions of source code must retain the above copyright notice,
1414
* this list of conditions and the following disclaimer.
1515
* 2. Redistributions in binary form must reproduce the above copyright notice,
1616
* this list of conditions and the following disclaimer in the documentation
1717
* and/or other materials provided with the distribution.
18-
*
18+
*
1919
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2020
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2121
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -36,10 +36,12 @@
3636
import static org.junit.Assert.assertEquals;
3737

3838
import org.junit.Test;
39+
import org.scijava.io.ByteArrayByteBank;
40+
import org.scijava.util.ByteArray;
3941

4042
/**
4143
* Tests {@link BytesLocation}.
42-
*
44+
*
4345
* @author Curtis Rueden
4446
*/
4547
public class BytesLocationTest {
@@ -72,4 +74,31 @@ public void testBytesOffsetLength() {
7274
assertArrayEquals(expectedDigits, testDigits);
7375
}
7476

77+
/**
78+
* Tests getName()
79+
*/
80+
@Test
81+
public void getNameTest() {
82+
83+
final BytesLocation loc1 = new BytesLocation(0);
84+
assertEquals(loc1.defaultName(), loc1.getName());
85+
assertEquals("Location.defaultName", loc1.defaultName());
86+
87+
final String expectedName = "test.name";
88+
BytesLocation loc2 = new BytesLocation(0, expectedName);
89+
assertEquals(expectedName, loc2.getName());
90+
91+
loc2 = new BytesLocation(new byte[0], expectedName);
92+
assertEquals(expectedName, loc2.getName());
93+
94+
loc2 = new BytesLocation(new ByteArray(), expectedName);
95+
assertEquals(expectedName, loc2.getName());
96+
97+
loc2 = new BytesLocation(new ByteArrayByteBank(), expectedName);
98+
assertEquals(expectedName, loc2.getName());
99+
100+
loc2 = new BytesLocation(new byte[0], 0, 0, expectedName);
101+
assertEquals(expectedName, loc2.getName());
102+
}
103+
75104
}

0 commit comments

Comments
 (0)