Class Setting<T>

java.lang.Object
com.j3d.gen.settings.Setting<T>
Type Parameters:
T - The type of the setting.
All Implemented Interfaces:
EventEmitterInterface, SettingsChild
Direct Known Subclasses:
BooleanSetting, ComplexSetting, DoubleSetting, EnumSetting, IntSetting, StringSetting

public class Setting<T> extends Object implements SettingsChild, EventEmitterInterface
A generic class representing a single setting.

This class is used to store a single setting, along with its description. It can be used for any type of setting, by specifying the type parameter <T>.

A setting also serializes into a key value pair. (although this is usually with dot syntax) So the setting CameraProperties.movementSpeed serializes into:


     settings.cameraProperties.movementSpeed = 5.0
     
and alot more logic that's readable in serialize() deserialize(ArrayList) provided by SettingsChild and also found in SettingsParent

Author:
Lehlogonolo Poole
See Also:
  • Field Details

    • value

      private T value
    • description

      private final String description
    • defaultValue

      private final T defaultValue
    • name

      private final String name
    • registered

      private ArrayList<EventListener> registered
    • callback

      private Function<T,Void> callback
  • Constructor Details

    • Setting

      public Setting(String name, T value, String description)
      Constructs a new Setting with the given name, initial value, and description. The initial value also serves as the default value.
      Parameters:
      name - The name of the setting.
      value - The initial value of the setting.
      description - A brief description of what the setting controls.
  • Method Details

    • valueToString

      public String valueToString(T value)
      Converts the setting's value to its string representation.
      Parameters:
      value - The value to convert to a string.
      Returns:
      The string representation of the value.
    • fromString

      public T fromString(String str)
      Converts a string representation back to the setting's type.
      Parameters:
      str - The string to convert.
      Returns:
      The value of type T parsed from the string.
      Throws:
      ClassCastException - if the string cannot be cast to type T (for the default implementation).
    • serialize

      public ArrayList<String> serialize()
      Serializes the current setting into a list of strings. For a single setting, this typically returns a list containing one string in the format "settingName = settingValue".
      Specified by:
      serialize in interface SettingsChild
      Returns:
      An ArrayList of strings representing the serialized setting.
    • deserialize

      public void deserialize(ArrayList<String> leftover)
      Deserializes the setting's value from a list of strings. It searches for a string containing the serialized name of this setting, parses its value, and updates the setting. If no match is found or parsing fails, the setting is reset to its default value.
      Specified by:
      deserialize in interface SettingsChild
      Parameters:
      leftover - An ArrayList of strings from which to deserialize the setting.
    • getName

      public String getName()
      Returns the name of this setting.
      Specified by:
      getName in interface SettingsChild
      Returns:
      The name of the setting.
    • getValue

      public T getValue()
      Returns the current value of this setting. If the current value is null, the default value is returned.
      Returns:
      The current value of the setting.
    • setValue

      public void setValue(T value)
      Sets the value of this setting and broadcasts an update event.
      Parameters:
      value - The new value to set.
    • setValueNoBroadcast

      public T setValueNoBroadcast(T value)
      Sets the value of this setting without broadcasting a specific SETTING_CODE_UPDATED event.
      Parameters:
      value - The new value to set.
      Returns:
      The old value of the setting before the update.
    • getDefaultValue

      public T getDefaultValue()
      Returns the default value of this setting.
      Returns:
      The default value.
    • getDescription

      public String getDescription()
      Returns the description of this setting.
      Specified by:
      getDescription in interface SettingsChild
      Returns:
      The description string.
    • panel

      public <R extends Component> R panel()
      Returns the panel associated with this setting for display and interaction.
      Specified by:
      panel in interface SettingsChild
      Type Parameters:
      R - The type of the UI component, which must extend Component.
      Returns:
      A UI component representing this setting, or null if not implemented by a subclass.
    • attachListener

      public void attachListener(EventListener event)
      Attaches an EventListener to this setting to receive updates.
      Specified by:
      attachListener in interface EventEmitterInterface
      Parameters:
      event - The listener to attach.
    • detachListener

      public void detachListener(EventListener event)
      Detaches an EventListener from this setting.
      Specified by:
      detachListener in interface EventEmitterInterface
      Parameters:
      event - The listener to detach.
    • detachAll

      public void detachAll()
      Detaches all registered EventListeners from this setting.
      Specified by:
      detachAll in interface EventEmitterInterface
    • broadcast

      public <K> void broadcast(EventType eventType, EventPayload<K> properties)
      Broadcasts an event of a specific type with associated properties to all registered listeners.
      Specified by:
      broadcast in interface EventEmitterInterface
      Type Parameters:
      K - The emitter of the payload.
      Parameters:
      eventType - The type of the event to broadcast.
      properties - The payload containing event-specific data.
    • isAttached

      public boolean isAttached(EventListener e)
      Checks if a specific EventListener is currently attached to this setting.
      Specified by:
      isAttached in interface EventEmitterInterface
      Parameters:
      e - The listener to check.
      Returns:
      true if the listener is attached, false otherwise.
    • onSetValue

      public Setting<T> onSetValue(Function<T,Void> callback)
      Registers a callback function to be executed whenever the setting's value is changed. The callback receives the new value as an argument.
      Parameters:
      callback - The Function to be called on value change.
      Returns:
      This Setting instance, allowing for method chaining.