Git Product home page Git Product logo

util's Introduction

Util

A collection of classes and static functions with the purpose to simplify the work with java objects and data strcutures including List, Set, Map and convenience functions to cast an Object to a specific type including int, float, double, long, boolean, List, Map, and others.

The following is work-in-progress and uses processing based code samples.

sojamo.util.Common

Some usage examples of the static functions from the sojamo.util.Common class.

Common.i()

An example of how to use the i() function to convert a Object into a primitive int. Furhtermore there are implementations available for float, double, long, char, boolean, etc.

	import sojamo.util.*;
	import static sojamo.util.Common.*;
	
	Object o = new Integer(100);
	int i = i(o);
	println(i);
	

Common.toMap()

An example of how to use the toMap() function to convert a sequence of object into key-value pairs stored inside a Map. The Map returned by toMap() is a LinkedHashMap().

	import java.util.*;
	import sojamo.util.*;
	import static sojamo.util.Common.*;
	
	// create and fill Map m
	Map m = toMap( "a", 1, "b", 2, "c", "three", "d", 4.2);
	
	// request and cast values from Map m
	int v0 = i(m,"a");
	int v1 = i(m,"b");	
	String v2 = s(m,"c");	
	float v3 = f(m,"d");	
	println(v0,v1,v2,v3); 
	

Common.toList()

A convenience function to convert a sequence of parameters into a List.

	import java.util.*;
	import sojamo.util.*;
	import static sojamo.util.Common.*;
	
	List l = toList(1,"two",3.3,5,6);
	int v0 = i(l,0);
	String v1 = s(l,1);
	println(v0,v1);
	

Common.setValuesFor()

With setValuesFor() you can use the key-value pairs of a Map to set and change the state of Object variables.

	import java.util.*;
	import java.util.Map.Entry;
	import sojamo.util.*;
	import static sojamo.util.Common.*;

	void setup() {
	  // create and fill Map m
	  Map m = toMap( "a", 1, "b", 2, "c", "three", "d", 4.2);
	  println(m);
	  
	  // instantiate a new Test Object
	  Test t = new Test();
	  // set variables based on Map m's entrySet
	  setValuesFor(t,m);
	  println(t);
	  
	  Map m1 = toMap();
	  setValuesFor(m1,m);
	  println(m1);
	  
	}

	class Test {
	  float a;
	  float b;
	  String c;
	  float d;
	  String toString() {
	    return "Test: "+a+" "+b+" "+c+" "+d;
	  }
	}

util's People

Contributors

sojamo avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.