More marking.
[SquirrelJME.git] / modules / midp-lcdui / src / main / java / cc / squirreljme / runtime / lcdui / scritchui / ObjectTracker.java
blobce7dad2b74fc79572cb9e656aa1a145af1414f11
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.runtime.lcdui.scritchui;
12 import cc.squirreljme.jvm.mle.scritchui.ScritchEventLoopInterface;
13 import cc.squirreljme.runtime.cldc.annotation.SquirrelJMEVendorApi;
15 /**
16 * This keeps track of an object string, if it changes then an update to
17 * a signal end is emitted.
19 * The listener that is called is always in the event loop.
21 * @param <T> The object type.
22 * @param <L> The listener type to use.
23 * @since 2024/07/18
25 @SquirrelJMEVendorApi
26 public abstract class ObjectTracker<T, L>
28 /** The event loop used. */
29 @SquirrelJMEVendorApi
30 protected final ScritchEventLoopInterface loop;
32 /** The current value. */
33 @SquirrelJMEVendorApi
34 volatile T _value;
36 /** The currently attached listener. */
37 @SquirrelJMEVendorApi
38 volatile L _listener;
40 /**
41 * Initializes the object tracker with the given initial text.
43 * @param __loop The event loop interface.
44 * @param __init The initial value to use.
45 * @throws NullPointerException On null arguments.
46 * @since 2024/07/20
48 @SquirrelJMEVendorApi
49 public ObjectTracker(ScritchEventLoopInterface __loop, T __init)
50 throws NullPointerException
52 if (__loop == null)
53 throw new NullPointerException("NARG");
55 this.loop = __loop;
56 this._value = __init;
59 /**
60 * Executes the given listener.
62 * @param __listener The listener to execute.
63 * @param __value The value being set.
64 * @throws NullPointerException If no listener was specified.
65 * @since 2024/07/20
67 @SquirrelJMEVendorApi
68 protected abstract void exec(L __listener, T __value)
69 throws NullPointerException;
71 /**
72 * Connects to the given listener.
74 * @param __listener The listener to connect to.
75 * @throws NullPointerException On null arguments.
76 * @since 2024/07/18
78 @SquirrelJMEVendorApi
79 public final void connect(L __listener)
80 throws NullPointerException
82 if (__listener == null)
83 throw new NullPointerException("NARG");
85 synchronized (this)
87 this._listener = __listener;
90 // Since we connected a listener, we want to make sure it has the
91 // most up-to-date information
92 this.loop.loopExecute(new __ExecObjectTracker__<T, L>(this));
95 /**
96 * Gets the current text.
98 * @return The current text.
99 * @since 2024/07/18
101 @SquirrelJMEVendorApi
102 public final T get()
104 synchronized (this)
106 return this._value;
111 * Sets the given text.
113 * @param __t The text to set.
114 * @since 2024/07/18
116 @SquirrelJMEVendorApi
117 public final void set(T __t)
119 L listener;
120 synchronized (this)
122 this._value = __t;
123 listener = this._listener;
126 // Inform listener of the change?
127 if (listener != null)
128 this.loop.loopExecute(new __ExecObjectTracker__<T, L>(
129 this));