forked from ObjectLayout/ObjectLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.java
More file actions
85 lines (72 loc) · 2.74 KB
/
Line.java
File metadata and controls
85 lines (72 loc) · 2.74 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Written by Gil Tene, and released to the public domain,
* as explained at http://creativecommons.org/publicdomain/zero/1.0/
*/
import org.ObjectLayout.Intrinsic;
import org.ObjectLayout.IntrinsicObjects;
import java.lang.reflect.Constructor;
/**
* A simple Line class example with two intrinsic Point end point objects. Demonstrates use of
* {@link org.ObjectLayout.IntrinsicObjectModel}
* members and their initialization, either at field initialization or during construction.
*
* <p>
* Note that the following rules apply to intrinsic object members:
* <ul>
* <li>All intrinsic object members must be private final fields. </li>
* <li>Intrinsic object members should be initialized (either at
* field declaration, initialization code section, or in the containing object's constructor) using
* one of the {@link org.ObjectLayout.IntrinsicObjects#constructWithin} variants</li>
* <li>Intrinsic object members should not be initialized to null</li>
* <li>Intrinsic object members should not be initialized with the value of another intrinsic object member</li>
* <li>Intrinsic object members should be constructed with their containing object as a parameter</li>
* </ul>
*
* Failure to follow these rules can result in error, warnings, or simply in slower execution.
*
*/
public class Line {
/**
* Simple intrinsic object declaration and initialization:
*/
@Intrinsic
private final Point endPoint1 = IntrinsicObjects.constructWithin("endPoint1", this);
/**
* Declaration of an intrinsic object field that will be initialized later during construction or other init code:
*/
@Intrinsic
private final Point endPoint2;
public Line() {
this(0, 0, 0, 0);
}
public Line(final long x1, final long y1, long x2, long y2) {
/**
* Construction-time Initialization of IntrinsicObject:
*/
this.endPoint2 = IntrinsicObjects.constructWithin("endPoint2", this, xy_constructor, x2, y2);
/**
* Access to IntrinsicObject within constructor:
*/
this.endPoint1.set(x1, y1);
}
public Line(final Line source) {
this(source.endPoint1.getX(), source.endPoint1.getY(),
source.endPoint2.getX(), source.endPoint2.getY());
}
public Point getEndPoint1() {
return endPoint1;
}
public Point getEndPoint2() {
return endPoint2;
}
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);
}
}
}