forked from ObjectLayout/ObjectLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointArray.java
More file actions
66 lines (56 loc) · 2.49 KB
/
PointArray.java
File metadata and controls
66 lines (56 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Written by Gil Tene and Martin Thompson, and released to the public domain,
* as explained at http://creativecommons.org/publicdomain/zero/1.0/
*/
import org.ObjectLayout.ConstructionContext;
import org.ObjectLayout.CtorAndArgs;
import org.ObjectLayout.CtorAndArgsProvider;
import org.ObjectLayout.StructuredArray;
import java.lang.reflect.Constructor;
public class PointArray extends StructuredArray<Point> {
// expose copy constructor:
public PointArray(PointArray source) {
super(source);
}
// Default constructor needs to be defined only because we wanted to expose a copy constructor
public PointArray() {
}
// newInstance wrapper for convenience (cleaner/fewer parameters for API user):
public static PointArray newInstance(final long length) {
return StructuredArray.newInstance(PointArray.class, Point.class, length);
}
// newInstance wrapper for convenience (cleaner/fewer parameters for API user):
public static PointArray newInstance(final long length,
final CtorAndArgsProvider<Point> ctorAndArgsProvider) {
return StructuredArray.newInstance(
PointArray.class, Point.class, length, ctorAndArgsProvider);
}
// If you want to support direct construction parameters for elements, with with
// parameter types you know (statically) have a good constructor associated with the,
// here is an example:
public static PointArray newInstance(final long length, final long x, final long y) {
final CtorAndArgs<Point> xy_ctorAndArgs = new CtorAndArgs<Point>(xy_constructor, x, y);
return StructuredArray.newInstance(
PointArray.class,
Point.class,
length,
// This can be a Lambda expression in Java 8:
new CtorAndArgsProvider<Point>() {
@Override
public CtorAndArgs<Point> getForContext(
ConstructionContext<Point> context) throws NoSuchMethodException {
return xy_ctorAndArgs;
}
});
}
static final Constructor<Point> xy_constructor;
static {
try {
@SuppressWarnings("unchecked")
Constructor<Point> constructor = Point.class.getConstructor(long.class, long.class);
xy_constructor = constructor;
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
}
}