-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectPointer.java
52 lines (46 loc) · 1.18 KB
/
ObjectPointer.java
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
package objptr;
/**
* This class can be used to enable calling by reference.
*
* Unfortunately, getting the actual address of the pointer in memory is not possible.
*
* @author Johannes Kloimböck
*
* @param <T> the type of the object (e.g. Integer, List)
*/
public class ObjectPointer<T> {
// the "pointer" is actually an array with one element
private Object ptr [] = new Object [1];
/**
* Initializes a pointer to the parameter object.
* @param obj is the object that the pointer is supposed to point to
*/
public ObjectPointer (T obj) {
ptr[0] = obj;
}
/**
* Initializes a null-pointer.
*/
public ObjectPointer () {
ptr[0] = null;
}
/**
* Get the object from the ObjectPointer.
* @return the object to use it separatly from the ObjectPointer
*/
@SuppressWarnings("unchecked")
public T getObject () {
return (T)(ptr [0]);
}
/**
* Modify the current object the ObjectPointer is pointing to.
* @param newObj is the new object that replaces the old one
*/
public void setObject (T newObj) {
ptr [0] = newObj;
}
@Override
public String toString () {
return getObject().toString();
}
}