Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

ElizabethCody/named-variables

Repository files navigation

named-variables

This library facilitates mutable access, with built-in parsing, to primitive types (e.g. int, double, boolean, etc.), boxed types (e.g. Integer, Double, Boolean, etc.), enums, and String by allowing names to be assigned to them at runtime. One may find this particularly useful when adding support for custom values to an existing configuration system (e.g. when modding a video game).

Example

import sh.cody.namedvars.*;

public class Example {
   private Scope scope;
   
   // Create variable entirely at runtime, see constructor.
   private Variable<?> colorCode1;

   // Create variable with reflection, implicitly named.
   @GenerateVariable
   private int colorCode2 = 0xff00dd;

   // Create variable with reflection, explicitly named.
   @GenerateVariable("colorCode3")
   private int anotherColorCode = 0xffff00;

   @GenerateVariable
   private boolean debugMode = false;

   public Example() throws VariableScopeException {
      scope = new Scope();
      colorCode1 = scope.create("colorCode1", int.class, 0x00ff00); // Create a variable at runtime whose storage
                                                                    // is managed by named-vars.
      scope.importAll(this); // Adds all variables decorated with @GenerateVariable to the scope.
      namedVarsExample();
   }

   void namedVarsExample() {
      // Uniformly access variables.
      System.out.println(scope.get("colorCode1").get()); // Retrieve native value.
      System.out.println(scope.get("colorCode2"));       // Use smart toString() method.
      scope.get("colorCode3").set(0xffffff);             // Update value.
      scope.get("debugMode").parse("true");              // Update value by parsing from String.      
   }
}

Use

Update your Gradle build to include:

repositories {
    // ... your other repositories ...
    jcenter()
}

dependencies {
   // ... your other dependencies ...
   implementation 'sh.cody:named-variables:1.1.0'
}

Build

$ ./gradlew build

Copyright

Copyright © 2020 - Maxwell Cody <maxwell@cody.sh>

This project is licensed under The MIT License, see LICENSE.txt for more details.

About

Get mutable access to primitives, boxed types, enums, and Strings.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages