summaryrefslogtreecommitdiff
path: root/CSMath/src/bezier/Holder.java
blob: d6deba0efd1c5dbffdff839e4dd7ecef836820c7 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package bezier;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

/**
 * Dummy class for holding a value, and being notified when it changes.
 * 
 * Essentially a pointer with change notifications.
 * 
 * @author bjculkin
 *
 * @param <E>
 *            The type of held value.
 */
public class Holder<E> {
	/*
	 * The held value.
	 */
	private E val;

	/*
	 * The listeners to notify.
	 */
	private final List<Consumer<E>> listeners;

	/**
	 * Create a new holder holding nothing.
	 */
	public Holder() {
		listeners = new LinkedList<>();
	}

	/**
	 * Create a new holder holding a specific value.
	 * 
	 * @param val
	 *            The value being held.
	 */
	public Holder(E val) {
		this();

		this.val = val;
	}

	/**
	 * Get the contained value.
	 * 
	 * @return The contained value.
	 */
	public E getVal() {
		return val;
	}

	/**
	 * Set the contained value, and notify listeners.
	 * 
	 * @param val
	 *            The new value.
	 */
	public void setVal(E val) {
		this.val = val;

		/*
		 * Notify listeners.
		 */
		for (Consumer<E> listen : listeners) {
			listen.accept(val);
		}
	}

	/**
	 * Add a listener.
	 * 
	 * @param listen
	 *            The listener to add.
	 */
	public void addHolderListener(Consumer<E> listen) {
		listeners.add(listen);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((val == null) ? 0 : val.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Holder<?> other = (Holder<?>) obj;
		if (val == null) {
			if (other.val != null)
				return false;
		} else if (!val.equals(other.val))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Holder [val=" + val + "]";
	}
}