Monday, 31 October 2016

This week 17/2016

SmartParam (v.1.1.2) is a powerful framework to parametrise an application.

1. Source of configuration
It is possible to load configuration from text file, database or create it dynamically.  Framework supports directly H2, MySql and Postgesql databases.


example of configuration file:

{
  name: "simpleCase",  inputLevels: 2,  cachable: true,  nullable: true,  arraySeparator: ";",
  levels: [
    {name: "registrationDate", type: "date",   matcher: "between/ie" },     
    {name: "name",                     type: "string" }, 
    {name: "value",                     type: "integer" }, 
    {name: "value2",                   type: "integer" }
  ]
}
registrationDate;name;value;value2
*: 2013-12-01;Adam;20*: 2013-12-01;Monia;5
 


2. Number of input/output parameters
At the beginning it is required to define structure of parameters:
  • name - name of configuration,
  • inputLevels - count of input parameters, which are used to wind exact output parameters,
  • nullable - by default it is false and if any rule match input parameters, it throws exception
  • levels - defines types of input and output parameters.
3. Supported parameters
Framework supports simple types of data, however it is possible to define your own type. There are two ways to do that: do converter or  type holder.
Type holder ex.
@ParamType("localdate")
public class LocalDateType implements Type<LocalDateHolder> {
.....
}
 
public class LocalDateHolder extends AbstractValueHolder {
    private final LocalDate date;
    public LocalDateHolder(LocalDate date) {
        this.date = date;    }
....
} 

4. Matcher
Framework provides all needed matchers but there is possible to create your own matcher.
ex.
@ParamMatcher("mymatcher")
public class MyMatcher implements Matcher {

    @Override    public <T extends ValueHolder> boolean matches(String s, String s1, Type<T> type) {

        return s.toString().equals(s1.toString());    }
}

One of useful matcher is BetweenMatcher. It supports defining a ranges of dates, numbers and defines default value by *

5. Other Notices
Framework presents itself great but project looks to be extinct.

Saturday, 22 October 2016

This week 16/2016

How works a synchronisation of some object in a nutshell.

My goal is to synchronise a block of code by some string which is a parameter of a function.

At the begging I wasn't sure how synchronisation mechanism works. Is it using equals and hash code to compare object or memory address where object is allocated.
So synchronisation doesn't use equal method but memory address where object is allocated to.

So how to synchronise part of code when I have many instances of the same object (ex. string)?

A solution is to use some container for synchronized objects and retrieves instance of  that object to use it to synchronization. In this case the best choose is map where it is possible to find object by key and get its value or if it not exists yet, add it.

So we need some kind of cache but how to manage this cache? We need only object instance when the synchronisation block is executed.
I think the easiest way on a single jvm is to use a weak reference. In this case I create a cache manager with WeakHashMap. The manager has a synchronized method findOrAdd and returns an instance of string object included to map.
What is important the WeakHashMap use weak reference only for key object so it is required to put as a value a WeakReference instance with my object.

Map map = new WeakHashMap();
map.put(myString, new WeakReference(myString));
Now, if instance of myString hasn't any reference to any other object or is not used to synchronisation, it will be removed from memory so removed from map as well.

Saturday, 8 October 2016

This week 15/2016

Protractor framework.

The Protractor (v4.0.x) is perfect tool to testing Angular JS application. The main advantage is that it transparently supports asynchronous tasks. You don't care of timeout for Angular asynchronous operation, Protractor is synchronising itself for you. Big advantage is that framework can make snapshot of screen and save it to ex. PNG file.