Skip to main content
This revision made November 16, 2010 14:08, by Vladimir Zhukov
« earlier revision revert to this « later revision

Utils Library fo Java 1.6+ (test name elava)

Emsoft-utils is an open source utils library for Java 1.6+. Contains assertion utility for check arguments, collections utility, strings utility, io utility, password utility, observer impementation, exceptions utility, operation templates and other. For full detail see api doc or sources.

Assert example:


    public void assertExample() {
        List list = new ArrayList();
        assertMethod(new Object(), "text", list); // exception with arg3
        list.add(new Object());
        assertMethod(new Object(), "text", list); // success
        assertMethod(new Object(), "", list); // exception with arg2
        assertMethod(null, "text", list); // exception with arg1
    }

    public void assertMethod(Object arg1, String arg2, Collection arg3) {
        Assert.notNull(arg1, "arg1");
        Assert.notNullOrEmpty(arg2, "arg2");
        Assert.notNullOrEmpty(arg3, "arg3");
        // do something
        // ...
    }

Helper example:


    public void helperMethod(Object arg1) {
        Object obj = Helper.getValue(arg1, "defaultValue");
        // do something
        // ...

        // if arg1 is not string then no exception, just returns null
        String str = Helper.asType(arg1, String.class);
        // do something
        // ...
    }

ResourceHelper example:

ResourceHelper work like ResourceBundle, but reads properties files in UTF-8 encoding.


    public void resourceHelperExample() {
        ResourceBundle rb = ResourceHelper.getResourceBundle("com/emsoft/utils/resources/ErrorMessages");
        String value = rb.getString("key");
        // or
        value = ResourceHelper.getString("com/emsoft/utils/resources/ErrorMessages", "key");
    }

StringHelper example:


    public void stringHelperMethod(String arg1) {
        if (StringHelper.isNullOrEmpty(arg1))
            return;
        // for example, arg1 == "12as2dfg"
        List<String> chunks = StringHelper.split(arg1, "[0-9]+");
        // chunks == "12", "as", "2", "dfg"
        String str = StringHelper.toString(chunks, "*");
        // str == "12*as*2*dfg"
    }

AlphanumComparator example:


    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("10b");
        list.add("10");
        list.add("a10");
        list.add("1df0");
        list.add("10a");
        list.add("3");
        list.add("1");
        list.add("2");
        System.out.println("before: " + list);
        Collections.sort(list, new AlphanumComparator());
        System.out.println("after: " + list);
    }

Result:
before: [10b, 10, a10, 1df0, 10a, 3, 1, 2]
after: [1, 1df0, 2, 3, 10, 10a, 10b, a10]

ExceptionHelper example:


    public void exceptionHelperExample() {
        try {
            throw new BaseRuntimeException();
        }
        catch (Exception ex) {
            System.out.println(ExceptionHelper.getExceptionMessage(ex, true));
            ExceptionHelper.logAndThrow(ex);
        }
    }

Difference compared to previous revision
<span> </pre> Result: <pre><span style="text-decoration:line-through;color:red">:</span>before: [10b, 10, a10, 1df0, 10a, 3, 1, 2] <span style="text-decoration:line-through;color:red">:</span>after: [1, 1df0, 2, 3, 10, 10a, 10b, a10] </pre> ====ExceptionHelper example:====<span>
 
 
Close
loading
Please Confirm
Close