Class LocalStorage

java.lang.Object
com.wyu4.snowberryjam.compiler.LocalStorage
Direct Known Subclasses:
Compiler

public abstract class LocalStorage extends Object
This class stores and handles anything related to compiling and running a source file.
  • Field Details

    • logger

      private static AtomicReference<org.slf4j.Logger> logger
    • inputLogger

      private static final org.slf4j.Logger inputLogger
    • VARIABLES

      private static final HashMap<String,Object> VARIABLES
      Stores variables
    • VARIABLES_COPY

      private static final HashMap<String,Object> VARIABLES_COPY
      A new copy of the variables. This is used during the runtime.
    • THREADS

      private static final List<Thread> THREADS
      Stores threads
    • STACK

      private static final BodyStack STACK
      Stores tasks starting from index 0
    • NAME

      private static final AtomicReference<String> NAME
      Atomically stores the project name
    • DESCRIPTION

      private static final AtomicReference<String> DESCRIPTION
      The description of the project
    • WARN_LISTENERS

      private static final List<BiConsumer<String,String>> WARN_LISTENERS
      The warn listeners
    • ERROR_LISTENERS

      private static final List<BiConsumer<String,String>> ERROR_LISTENERS
      The error listeners
    • INPUT_LISTENERS

      private static final List<Consumer<String>> INPUT_LISTENERS
      The input listeners/subscribers
    • CACHED_VALUES

      private static final List<Cached> CACHED_VALUES
    • VARIABLE_LISTENERS

      private static final HashMap<String,Consumer<Object>> VARIABLE_LISTENERS
      THe variable listeners
    • pointer

      private static final AtomicInteger pointer
      The runtime pointer index
    • running

      private static final AtomicBoolean running
      The running property
    • manualStop

      private static final AtomicBoolean manualStop
      If the runtime was manually stopped prematurely by the user
  • Constructor Details

    • LocalStorage

      public LocalStorage()
  • Method Details

    • getDefaultSource

      public static String getDefaultSource()
      Get a default project template.
      Returns:
      String containing a valid default empty project.
    • getRaw

      public static Object getRaw(String name) throws NullPointerException
      Get the raw value of a stored variable
      Parameters:
      name - The name of a variable
      Returns:
      The value of the variable as an Object
      Throws:
      NullPointerException - If the variable was not created
      See Also:
    • getVariableNames

      public static Set<String> getVariableNames()
      Get all of variable names.
      Returns:
      Set with created variable names
    • copyStack

      public static ExecutableTask[] copyStack()
      Recieve a deep copy of the tasks inside a stack
      Returns:
      An array of the tasks in a stack
      See Also:
    • flush

      public static void flush()
      Flush all the contents of the LocalStorage. This includes STACK, VARIABLES, and THREADS (after interrupting them).
    • createThread

      public static Thread createThread(Runnable run)
      Create a thread and store it in THREADS
      Parameters:
      run - The code to run in the thread
      Returns:
      A Thread object initialized with run
    • setVariable

      public static void setVariable(String name, Object newValue) throws NullPointerException
      Override the contents of a variable in VARIABLES
      Parameters:
      name - The name of the (already created) variable
      newValue - The new value
      Throws:
      NullPointerException - If the variable was not created
      See Also:
    • runStack

      public static void runStack()
      Run STACK
      See Also:
    • stopRun

      public static void stopRun()
      Stop the current run
    • isRunning

      public static boolean isRunning()
      Check if the stack is running
      Returns:
      true if the LocalStorage is running the stack, otherwise false.
    • setName

      protected static void setName(String name)
      Set the project name
      Parameters:
      name - Project name
    • setDescription

      protected static void setDescription(String description)
      Set the description of the project.
      Parameters:
      description - Description of the project
    • createVariable

      protected static void createVariable(String name, Object value) throws IllegalStateException
      Create a variable and store it in VARIABLES
      Parameters:
      name - The name of the variable
      value - The value of the variable
      Throws:
      IllegalStateException - A variable with the same name already exists
    • stackAdd

      protected static void stackAdd(ExecutableTask element)
      Add a task to STACK
      Parameters:
      element - A task
      See Also:
    • increasePointer

      public static void increasePointer()
    • getPointer

      public static int getPointer()
      Get the pointer index.
      Returns:
      The current pointer index.
    • resetPointer

      private static void resetPointer()
      Reset the pointer index to 0, and release all cached data.
    • releaseCache

      private static void releaseCache()
    • getLogger

      private static org.slf4j.Logger getLogger()
      Get the logger with the current project name. Creates a new logger with every name change.
      Returns:
      The current logger
      See Also:
    • sendInput

      public static void sendInput(String input)
      Send a user input.
      Parameters:
      input - String input
    • print

      public static void print(Object message, Object... args)
      Send a print statement to the runtime console.
      Parameters:
      message - Message to send (arguments can be inserted by adding "{}" in the message)
      args - Arguments to append in the message
      See Also:
    • warn

      public static void warn(Object message, Object... args)
      Send a print statement to the runtime console.
      Parameters:
      message - Message to send (arguments can be inserted by adding "{}" in the message)
      args - Arguments to append in the message
      See Also:
    • error

      public static void error(Object error)
      Send an error statement to the development console.
      Parameters:
      error - Message to send (arguments can be inserted by adding "{}" in the message).
      See Also:
    • error

      public static void error(Object error, Exception e)
      Send an error statement and stack trace to the development console.
      Parameters:
      error - Error to send
      e - Exception to send (used to print stack trace)
      See Also:
    • formatMessage

      protected static String formatMessage(Object message, Object... args)
      Format a message by replacing instances of "{}" with a corresponding argument
      Parameters:
      message - Message
      args - Arguments
      Returns:
      Formatted string
      See Also:
      • MessageFormatter.format(String, Object)
    • addPrintListener

      public static void addPrintListener(BiConsumer<String,String> consumer)
      Add a print listener
      Parameters:
      consumer - Consumer to run when something is printed
      See Also:
    • addWarnListener

      public static void addWarnListener(BiConsumer<String,String> consumer)
      Add a warn listener
      Parameters:
      consumer - Consumer to run when something is warned
      See Also:
    • addErrorListener

      public static void addErrorListener(BiConsumer<String,String> consumer)
      Add a error listener
      Parameters:
      consumer - Consumer to run when something is errored
      See Also:
    • addVariableListener

      public static void addVariableListener(String name, Consumer<Object> consumer)
      Add a variable listener. This listener is triggered when the variable with the corresponding name is changed.
      Parameters:
      name - Variable name
      consumer - The consumer to run
      See Also:
    • addInputSubscription

      public static void addInputSubscription(Consumer<String> consumer)
      Add an input subscription. Consumers are removed from the subscription list after they are run.
      Parameters:
      consumer - Consumer to run when user inputs something
    • addCachedValue

      public static void addCachedValue(Cached value)