Skip to content

Commit daf4ae2

Browse files
committed
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.
1 parent 821f89f commit daf4ae2

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
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

0 commit comments

Comments
 (0)