java

Java : chop and chomp

Perl have very handy Functions to clean up trailing garbage in Strings …

  • chop : deletes the last character of a string. Useful when you append delimiter in a loop, so that the last addition has to be cleaned up
  • chomp : removes trailing sequence of the same character. if the character/s at the end do not match the specs nothing is deleted. Useful to cleanup new-lines/s at the end. Normally happens when reading from file.

Here are those useful Functions implemented for Java :

public class Main {
  
  public static <ARG> void say(ARG arg) { System.out.println(arg); }

  //removing trailing characters
  public static String chomp(String str, Character repl) {
    int ix = str.length();
    for (int i=ix-1; i >= 0; i-- ){
      if (str.charAt(i) != repl) break;
      ix = i;
    }
    return str.substring(0,ix);
  }
  
  //default case, removing trailing newlines
  public static String chomp(String str) {
    return chomp(str,'\n');
  }  

  //hardcut  
  public static String chop(String str) {
    return str.substring(0,str.length()-1);
  }
  
  public static void main(String[] args) {
    say(chomp("hi...",'.'));
    say(chomp("hello\n\n",'\n'));
    say(chomp("hello\n\n"));
    say(chop("howdy."));
    say(chomp("hi...++",'.'));      
  }
}

------

hi
hello
hello
howdy
hi...++

Timing code

This one is quick.

Java

Here is how you do it :

import java.lang.Thread;  

public class Main {
  public static void main(String[] args)  throws Exception {
        
    long startTime = System.nanoTime();
    Thread.sleep(3000);
    long endTime = System.nanoTime();
    //divide by 1000000 to get milliseconds.
    int duration = (int) ((endTime - startTime) / (1000000 * 1000));  

    System.out.println("it took: " + duration + " secs");
  }
}

-----

it took: 3 secs

Prolog

?- numlist(1, 100, L).
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...].

?- time(numlist(1, 100, L)).
% 105 inferences, 0.000 CPU in 0.000 seconds (94% CPU, 1126344 Lips)
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...].

Python

For Python look at this post : Timing code execution

Call method by String or Reference

Sometimes you may need to call a method, but you don’t know which one in advance i.e. you want to postpone the decision to runtime.
There are two basic ways to that :

  • Store the method name as a String and build the “call” on the fly when you need it
  • Store a reference to a method in a variable and then use language specific syntax to invoke the method

For example let say you have written multiple standalone tests and you want to run different combination of them based on different needs. In other words you want to parameterize method calls.

Something along the lines :

run_tests -suite=3,5,8
run_tests -suite=2..5

If you have a few combinations you can hard code them, but it is more flexible to “pick” the tests/methods on the fly.

This is just one example of the need for selecting the function/method at the last moment. In most cases you prepare or store the name in a String.

What follows are examples of how to do this in several languages.

Python

There are many different ways to call a function or a method indirectly. Below you can see most of them.

#shortcut of : def say(*a): print(*a)
say = print

#using eval()
eval("say")("hello")
say_hi = "say('hi')"
eval(say_hi)

#call defined functon via locals
locals()["say"]('boo hoo')

#via reference stored in a dict
funs = { 'sayit' : say }
funs['sayit']('sayit')

# ----- and now ... -----

#methods in a string
class StringMethod:
  
  def say(self, *args) : print(*args)
  
  
sm = StringMethod()

#"create" a method
method = getattr(sm,"say")
method("howdy")

#direct assignment
sm_say = sm.say
sm_say('whats sup')

#unbound
sayit = StringMethod.say
sayit(sm,'bye')


-----

hello
hi
boo hoo
sayit
howdy
whats sup
bye

Java

In Java you have to use Reflection. You do it in two steps :

  • First you create a Method out of the String and a description of the parameters
  • Then you call the .invoke() method with the real values

import java.util.*;
import java.lang.reflect.*;


public class MethodReflection {

  public static <ARG> void say(ARG arg) { System.out.println(arg); }

  public void say_hi() { say("hi"); }
  private void sayit(String str) { say(str); }
  
  public void test() throws Exception {
    //get the class that hold the methods you want to call
    Class<?> klass = MethodReflection.class;//Class.forName("MethodReflection");
    
    //get a method w/o arguments
    Method m1 = klass.getMethod("say_hi");// or getDeclaredMethod()
    m1.invoke(this);
    
    //private method requires getDeclaredMethod(), also using one String argument
    Method m2 = klass.getDeclaredMethod("sayit", String.class);
    m2.invoke(this, "hello");
    
    //unknown argument
    Method m3 = klass.getMethod("say", Object.class);
    //first argument null for static method
    m3.invoke(null, "howdy");
    
  }

  public static void main(String[] args) throws Exception {
    MethodReflection mc = new MethodReflection(); 
    mc.test();
  }
}

-----

hi
hello
howdy

JavaScript

say = console.log

eval("say('hi')")

sayit = "say"
window[sayit]('sayit')

-----

hi
sayit

Padding values …

Here is an example of how to pad values with fill characters :

public class Main {
  
  public static <T> String pad(T str, int n, String right_pad, String filler) {
    return String.format("%1$" + right_pad + n + "s", str).replaceAll(" ", filler);
  }
  
  public static <T> String pad_left(T str, int n, String filler) {
    return pad(str, n, "", filler);
  }  
  public static <T> String pad_right(T str, int n, String filler) {
    return pad(str, n, "-", filler);
  }  
  
  public static <T> String pad_left(T str, int n) {
    return pad(str, n, "", " ");
  }  
  public static <T> String pad_right(T str, int n) {
    return pad(str, n, "-", " ");
  }  
  
  public static void main(String[] args) {
      System.out.println("pad(5,3)     : " + pad_left(5,3));
      System.out.println("pad(5,3,\"0\") : " + pad_left(5,3,"0"));
      System.out.println("pad(5,3,\"-\") : " + pad_right(5,3,"-"));
  }
}

-------

pad(5,3)     :   5
pad(5,3,"0") : 005
pad(5,3,"-") : 5--

Calculating date difference

Here is a quick way to calculate the difference between two dates counted in days.
You can easily modify it to use different TimeUnit.

import java.util.*;
import java.util.concurrent.TimeUnit;

public class Main {
  
  public static int days_diff(Date from, Date to) {
    long time_diff = Math.abs(to.getTime() - from.getTime() );
    int days = (int) TimeUnit.DAYS.convert(time_diff, TimeUnit.MILLISECONDS);
    return days;
  }

  public static void main(String[] args) {
      Date now = new Date();
      //use year - 1900, and month counting starts from Zero
      Date past = new Date(2022 - 1900, 3 - 1,1);
      
      System.out.println("now: " + past);
      System.out.println("now: " + now);
      System.out.println("Days difference : " + days_diff(past,now));
  }
}

-----------------------

now: Tue Mar 01 00:00:00 UTC 2022
now: Wed Mar 23 14:24:54 UTC 2022
Days difference : 22

Managing nested structures in Java

Handling nested structures in Java is kind of a nightmare. For this reason I created EasyLoL.

LoL is abbreviation of List-of-List, a general way of calling nested structures.

Imagine having to build structure like that in pure Java, how many keystroke energy you need to waste. No more just use EasyLoL and you will have the last laugh …

a : >
  b : >
    c : ccc
    d : ddd
  f : >
    c : fff2
    e : [abra,cadabra]
bcd : bcd
abc : abc
structs : >
  list : [v1,v2,v3]
  hash : >
    k1 : v1
    k2 : v2
    k3 : v3

The module extends HashMap and builds on top of it.

It provides fetch(), set() and delete() methods that use a dotted syntax to access and instantiate an element anywhere in the hierarchy by autovivifing all the intermediary elements if necessary.

autovivify : is the automatic creation of new arrays and hashes as required every time an undefined value is dereferenced.

The module also support a quick way to create and insert on the fly a HashMap or ArrayList element, you do that by encoding it as string, pre-pending it with special character, so that the module can figure out which one do you want to create.
Here is an example :

//hash in a string
e.set("structs.hash", ">k1:v1,k2:v2,k3:v3");
// list in a string
e.set("structs.list", "]v1,v2,v3");	

Below you can see the class and examples of how to use it.
Also you can also use dump() function I described in a previous post to pretty print the data structure.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// Easy way to Create and Access hierarchical structures
public class EasyLoL extends HashMap<String, Object> {
	
		public static <ARG> void say(ARG arg) { System.out.println(arg); }

		//extract value by going trough the hierarchy
		private Object _by_key(String[] keys, HashMap val) {
			Object rv = null;
			if ( val.containsKey(keys[0])) { rv = val.get(keys[0]); }
			else {
				//log.debug("! by_key> no such key : " + keys[0]);
				return null; 
			}
			// dig deeper, (skim the first element of String keys)
			if (rv instanceof HashMap && keys.length > 1 ) return _by_key(Arrays.copyOfRange(keys, 1, keys.length), (HashMap) rv);
			if (keys.length > 1) return null; //still more keys, but no more data that deep 
			return rv;
		}
		
		//helper method for LoL objects access
		public Object by_key(String key) {
			String[] keys = key.split("\\.");
			return _by_key(keys, this);
		}
		
		//Getter methods ....
		public Object find(String key) { return key.contains(".") ? by_key(key) : get(key); }
			
		public String gets(String key) {
			Object obj = find(key);
			if (obj instanceof String) return (String) obj;
			if (obj == null) return null;
			return obj.toString();
	 	}
		
		public void delete(String key) {
			int i = key.lastIndexOf('.') ;
			if (i == -1) return;
			String upto = key.substring(0,i);
			String last = key.substring(i+1);
			EasyLoL prev = fetch(upto);
			prev.remove(last);
		}
		
		public <T> T fetch(String key) {
			Object obj = find(key);
			if (obj == null) return null;
			return (T) obj;
	 	}
		
		public ArrayList by_keys(String ... keys) {
			ArrayList<String> lst = new ArrayList<>();
			for (String key : keys) lst.add(gets(key));
			return lst;
		}
		
		//converts string to Hash
		public HashMap<String,String> str2lol(String str) {
			HashMap<String,String> hash = new HashMap<String,String>();
			String[] kvs = str.split("[,:]");
			for (int i=0; i < kvs.length; i += 2) hash.put(kvs[i], kvs[i+1]);
			return hash;
		}
		
		//handles setting complex types
		private <T> void insert(EasyLoL lol, String k, T value) {
			//hash encoded in string
			if (value instanceof String) {
				if (((String) value).startsWith(">")) {
					HashMap<String,String> val = str2lol( ((String) value).substring(1) );
					lol.put(k, val);
					return;
				} else if (((String) value).startsWith("]")) {
					String val = ((String) value).substring(1);
					lol.put(k, Arrays.asList(val.split(",")));
					return;
				}
			} 
			lol.put(k, value);			
		}
		
		public <T> void set(String key, T value) throws Exception { this.set(key, value, true); }
		
		public <T> void set(String key, T value, boolean silent) throws Exception {
			
			//no hierarchy
//			if (! (key.contains(".") && this.containsKey(key))) {
//				this.put(key, value);
//			}
			
			String[] keys = key.split("\\.");
			//start point
			EasyLoL lol = this;
			int pos = 0;//are we at a leaf 
			//incrementally vivify the keys
			for (String k : keys) {
				if (lol.containsKey(k)) {
					if (lol.get(k) instanceof Map) {
						lol = (EasyLoL) lol.get(k);//extend
					} else {
						if (pos >= keys.length-1) {
							insert(LoL,k,value);//overwrite
							return;
						}	
						if (silent) return;
						throw new Exception("using : " + key + " ! Can't overwrite the structure at sub-key : " + k);
					}
				} else {//autovivify
					if (pos < keys.length-1) {//Intermediary keys
						LoL.put(k, new EasyLoL());
						//switch/move one level down
						LoL = (EasyLoL) LoL.get(k);
					} else {//tree leaf, set the value
						insert(LoL,k,value);
					}
				}
				pos++;
			}
	 	}
		
		
	public static void main(String[] args) throws Exception {
		EasyLoL e = new EasyLoL();
		e.set("abc", "abc");
		//Hierarchical set
		e.set("a.b.c", "ccc");
		e.set("a.b.d", "ddd");

		e.set("a.f.c", "fff");
		//overwrite
		e.set("a.f.c", "fff2");
		//conflict : structure already in place
		e.set("a.f.c.x", "fffx");//silent
		//e.set("a.f.c.x", "fffx",false);//throw error
		
		//setting java structure
		ArrayList ary = new ArrayList() {{
			add("abra");
			add("cadabra");
		}};
		e.set("a.f.e", ary);
		
		e.set("bcd", "bcd");
		//hash in a string
		e.set("structs.hash", ">k1:v1,k2:v2,k3:v3");
		// list in a string
		e.set("structs.list", "]v1,v2,v3");
		
		say("------------------------------------------");
		say("a.b : " + e.fetch("a.b"));
		say("a.b.c : " + e.fetch("a.b.c"));
		say("structs.list : " + e.fetch("structs.list"));
		say("structs.hash : " + e.fetch("structs.hash"));
//		e.delete("a.b");
		say("==========================================");
//		say(utils.dump(e));
	}

}

------------------------------------------
a.b : {c=ccc, d=ddd}
a.b.c : ccc
structs.list : [v1, v2, v3]
structs.hash : {k1=v1, k2=v2, k3=v3}
==========================================
a : >
  b : >
    c : ccc
    d : ddd
  f : >
    c : fff2
    e : [abra,cadabra]
bcd : bcd
abc : abc
structs : >
  list : [v1,v2,v3]
  hash : >
    k1 : v1
    k2 : v2
    k3 : v3

Random generation

With this post I’m starting a series where I will show you how to implement a functionality on the same topic in multiple languages.

All such posts will be marked with a tag : (multi-language)

We will start with generating random numbers and strings.

JavaScript

function rand_int(min,max=null) {
	if (max == null) { max = min; min = 0 }
	return Math.round(Math.random() * max) + min
}
const az = 'abcdefgijhklmnopqrstuvwxyzABCDEFGIJHKLMNOPQRSTUVWXYZ'
function rand_string(count,chars=az) {
	return Array.from({length:count}, (_,x) => chars[rand_int(52)]).join('')
}
function rand_nums(min,max,count) {
	return Array.from({length:count}, (_,x) => rand_int(min,max))
}
function rand_numstr(min,max,count) {return rand_nums(min,max,count).join('')}

Java

public static int rand(int max) {
	Random r = new Random();
	return r.nextInt(max);
}

public static String rand_numstr(int len) {
	StringBuilder str = new StringBuilder();
	for (int i = 0; i < len; i++) str.append(rand(10));
	return str.toString();
}

public static String rand_string(String characters, int len) {
    Random rng = new Random();
    char[] text = new char[len];
    for (int i = 0; i < len; i++) {
        text[i] = characters.charAt(rng.nextInt(characters.length()));
    }
    return new String(text);
}

public static String rand_string(int len) {
	return rand_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ",len);
}

Python

import numpy as np
import string

def rand_int(min,max,count=1):
   return np.random.randint(min,max,count)
   
def rand_int2(min,max,count=1):#non-repeatable
   return np.random.choice(range(min,max),count,replace=False)
   
def rand_str(count,opt='num'):
  chars = ''
  if opt == 'lower' : chars = string.ascii_lowercase
  elif opt == 'upper' : chars = string.ascii_uppercase
  elif opt == 'str' : chars = string.ascii_letters
  elif opt == 'all' : chars = string.digits + string.ascii_letters
  else : chars = string.digits  
  return ''.join([chars[rand_int(0,len(chars))[0]] for _ in range(count) ])
   
print(rand_int(0,100,10))
print(rand_int2(0,10,10))   
print(rand_str(10))   
print(rand_str(10,'lower'))   
print(rand_str(10,'upper'))   
print(rand_str(10,'str'))
print(rand_str(10,'all'))   

------

[24 50  6 69 69 89 78 24 32  3]
[2 3 0 9 7 6 8 1 5 4]
2123149924
tzuqwomaqv
VWXNTLMUDO
wIbOSUostQ
RDwgVu9QCH

Decorator-Placeholder pattern …

I had a dilemma … first let me explain the structure I have.
Class Page is the fulcrum of a hierarchy, which is then inherited to create tests. Every Page process Elements.

The goal is somehow in any Test to be able to write code that can be executed Before or After processing an element !!

The twist is that another programmer can create different Page class to support a new type of test. But I have to preserve this capability across all cases.

Here is the hierarchy :

  • ProtoPage
    • Page
      • TestPage
      • TestPage2
    • PageType2
      • TestPageType2
    • PageType3
  • Elements

First I tried to use Callbacks, but soon abandoned the idea. Then I thought about using Reflection, but still too complicated. Then it hit me ! The solution was staring me in the face.

I could simply put the before_element() and after_element() in the Page class and allow Elements to use them. That will work but then I have to communicate with the other programmers to keep the methods in their versions of Page.
It was good thing that Page already inherit ProtoPage, so I put the methods there. Now they are always available.

The second step was to pass the page object when I create Elements, so that Elements can call the methods.
But I already had that for other reasons, so nothing to do here.

And that’s it, problem solved !! Before&After methods go in ProtoPage and Elements can access them via the page object.

Below is a sketch of the code.

If you are wondering why before&after are not Abstract methods, the reason is they are not required but are optional. They are PLACEHOLDER methods, so I’m calling this placeholder pattern.

the code

public class ProtoPage {
  //placeholders
  public void before_element() {};
  public void after_element() {};
  
}

public class Page extends ProtoPage {
  
  public void process() {
    Elements es = new Elements(this);
    es.process();
  }
}

public class Elements {
  
  public ProtoPage page;
  
  Elements(Page page) {
    this.page = page;
  }
  
  public String[] all() {
    return new String[] {"el1", "el2", "el3" };
  }
  
  public void process() {
    for (String el : this.all()) {
    page.before_element();
    System.out.println("> process elements : " + el);
    page.after_element();
    }
  }
  
}

public class TestPage extends Page {
  @Override
  public void before_element() {
    System.out.println("> before element ...");
  };
  @Override
  public void after_element() {
      System.out.println("> after element ...");
  };
  
}
public class Test {
public static void 
         main(String[] args) {

 System.out.println("Hello World!");

 TestPage tp = new TestPage();
 tp.process();

 }
}
Hello World!
> before element ...
> process elements : el1
> after element ...
> before element ...
> process elements : el2
> after element ...
> before element ...
> process elements : el3
> after element ...

Java: pretty print data structures

Java lacks functionality to pretty print data structures. Let’s remedy that situation.

To make that work we will use code from some other posts : [Is scalar], [join], [List to Map].

It is simple recursive function : dump()

Below you can see an example usage.

You can also experiment with the code here.

import java.util.*;
import java.util.Map.Entry;

class Dump {
  
  	//the biggest invention in Java-world since sliced-bread ;)
	public static <ARG> void say(ARG arg) { System.out.println(arg); }

        //joins Iterable or Collection into a String
	public static <T> String join(String delimiter, Iterable<T> lst) {
		StringBuilder str = new StringBuilder();
		Iterator<T> it = lst.iterator();
		while ( it.hasNext() ) {
			str.append(it.next().toString());
			if (it.hasNext()) str.append(delimiter);
		}
		return str.toString();
	}

  	//for Classes check if the value is SCALAR
	public static boolean is_scalar(Object var) {
	    if( (var instanceof Number) || (var instanceof String) || (var instanceof Boolean)) return true;
	    return false;
	}


	public static HashMap<Integer, ?> list2map(List lst) {
		HashMap map = new HashMap();
		for (int i=0; i < lst.size(); i++) {
			map.put(i, lst.get(i));
		}
		return map;
	}	
	
	public static String dump(List lst, String... offset) {
		return dump(list2map(lst), offset);
	}
	
	// Pretty print a LoL structure, for debugging purposes
	public static String dump(Map m, String... offset) {
		if (m == null) return "";
	    StringBuilder rv = new StringBuilder();
	    String delta = offset.length == 0 ? "" : offset[0];
	    for( Entry e : (Set<Map.Entry>) m.entrySet() ) {
	        rv.append( delta + e.getKey() + " : " );
	        Object value = e.getValue();
	        if( value instanceof Map ) rv.append( ">\n" + dump((Map) value, delta + "  ") ); 
	        if( value instanceof Collection ) rv.append( "[" + join(",", (Collection) value) + "]\n" );
	        if( is_scalar(value) ) rv.append( value +"\n" );
	    } 
	    return rv.toString();
	}
	
	public static void main(String[] args) {
		  
      List lst = Arrays.asList(1,2,3,4);
      HashMap lvl2 = new HashMap() {{
       put("key2-1", "value2-1");
       put("key2-2", "value2-2");
      }};
      
      
      HashMap map = new HashMap() {{
       put("key", "value");
       put("lvl2", lvl2);
       put("lst",lst);
      }};
      
      
      say(dump(map));
	}
	
}	

here is the output :

lvl2 : >
  key2-2 : value2-2
  key2-1 : value2-1
lst : [1,2,3,4]
key : value

List to Map

Here is and easy way to convert List to a Map where the index is used as key :

import java.util.*;

public class Test {
  
 public static <ARG> void say(ARG arg) { System.out.println(arg); }

 public static HashMap<Integer, ?> list2map(List lst) {
    HashMap map = new HashMap();
	for (int i=0; i < lst.size(); i++) {
		map.put(i, lst.get(i));
	}
	return map;
  }

  public static void main(String[] args) {
      List a = Arrays.asList( 1,2,3,4 );
      say(a);
      say(list2map(a));
  }
}

Output:

[1, 2, 3, 4]
{0=1, 1=2, 2=3, 3=4}

You can directly test it (the code already setup for you) here in OneCompiler editor : https://onecompiler.com/java/3xh55parb