Skip to content

Shared Features

Oleksandr edited this page Apr 12, 2016 · 1 revision

Shared features

Watch Dog

When comparison is running for huge sets of data, it might be useful to stop comparison by request. To do that, you must provide WatchDog closure. System will check function every iteration. Closure must return Boolean type. Both DiffHelpers support that property.

The most simple implementation can be something like

int WATCH_DOG_FREQUENCY = 15;
Calendar lastTime = Calendar.getInstance();
lastTime.add(Calendar.SECOND, WATCH_DOG_FREQUENCY);

diffHelper.watchDog = {
    Boolean result = false;
    Calendar newTime = Calendar.getInstance();
    if(lastTime.before(newTime)){
        if(/* Your condition */){
            result = true;
        }else{
            newTime.add(Calendar.SECOND, WATCH_DOG_FREQUENCY);
            lastTime = newTime;
        }
    }
    return result;
};

Logging

You can get bit more information about comparison process from built in logging. By default additional logging us turned off.

diffHelper.showErrors = true;

New message will be added to console after every iteration. NOTE! When using "non OrderlySafe" mode message will be logged for every comparison. So please be careful when using this function. You console/log can be filled with messages very fast. In general use that function for debugging or testing other features.

Apply parameters from Map

You can provide all DiffHelper setup parameters/optioms from Map. Provide key-value pairs. Keys are exact property names. Value depends on provided value. NOTE! At the moment, there is no validation of input parameters.

HashMap<String, Object> config = [
    "showErrors" : false,
    "subQueryFromFile" : false,
    "orderlySafeMode": xml2DbParam.orderlySafeMode,
    "orderlySafeArrayMode": xml2DbParam.orderlySafeArrayMode,
    "orderlySafeIncludedMode": xml2DbParam.orderlySafeIncludedMode,
    "ignoredValue": xml2DbParam.ignoredValue,
    "includedNodes": ims
];
diffHelper.setupFromConfig();

Modifications

Also it is possible to modify internal fields according to some pattern. Provide modifications as map of key-function pairs. You can apply modification to all elements at once by using _all key. Closure must accept 1 String parameter. Closure must return String value. Possible modificators:

  • functions from default Groovy API of String class. Function chains can be provided. Split individual function by "." character.
  • path to class>function that contains modifications (closure from file on disk). Key name must contain _path suffix
  • In line Closures also allowed
<!-- XML1 -->
<node>
    <subNode1>sub node value</subNode1>
    <subNode2>subnodevalue</subNode2>
    <subNode3>subNodeValue</subNode3>
</node>

<!-- XML2 -->
<node>
    <subNode1>Sub Node Value</subNode1>
    <subNode2>SUBNODEVALUE</subNode2>
    <subNode3>subnodevalue</subNode3>
</node>
---
diffHelper.modifications1 = [
    'subNode1' : 'test.DummyModifications>capitalizeWordsCleanSpaces', // "test.DummyModifications.capitalizeWordsCleanSpaces()" will be applied to value, then comparison will start
    'subNode2' : 'toUpperCase', // String.toUpperCase() will be applied to value, then comparison will start
    'subNode3' : {String input->
        return input.toLowerCase();
    }
];

Clone this wiki locally