Skip to main content
This revision made November 15, 2010 17:27, by Vladimir Zhukov

Utils Library fo Java 1.5+ (test name elava)

Emsoft-utils is an open source utils library for Java 1.5+. 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 java doc or sources.

Assert example:


    public void assertExaple() {
        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 resourceHelperExaple() {
        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"
    }

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"
    }

Difference compared to previous revision
<span> <pre name="java"> public void <span style="text-decoration:underline;color:green">ass</span>e<span style="text-decoration:underline;color:green">rtE</span>= Utils Library fo Java 1.5+ (test name elava)= '''Emsoft-utils''' is an open source utils library for Java 1.5+. 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 java doc or sources. ... <pre name="java"> public void exaple<span style="text-decoration:line-through;color:red">s</span>() { List list = new ArrayList(); <span style="text-decoration:line-through;color:red">m</span>xaple() { List list = new ArrayList(); <span style="text-decoration:underline;color:green">ass</span>e<span style="text-decoration:underline;color:green">r</span>t<span style="text-decoration:underline;color:green">Met</span>ethod(new Object(), "text", list); // exception with arg3 list.add(new Object()); <span style="text-decoration:line-through;color:red">m</span>hod(new Object(), "text", list); // exception with arg3 list.add(new Object()); <span style="text-decoration:underline;color:green">ass</span>e<span style="text-decoration:underline;color:green">r</span>t<span style="text-decoration:underline;color:green">Met</span>ethod(new Object(), "text", list); // success <span style="text-decoration:line-through;color:red">m</span>hod(new Object(), "text", list); // success <span style="text-decoration:underline;color:green">ass</span>e<span style="text-decoration:underline;color:green">r</span>t<span style="text-decoration:underline;color:green">Met</span>ethod(new Object(), "", list); // exception with arg2 <span style="text-decoration:line-through;color:red">m</span>hod(new Object(), "", list); // exception with arg2 <span style="text-decoration:underline;color:green">ass</span>e<span style="text-decoration:underline;color:green">r</span>t<span style="text-decoration:underline;color:green">Met</span>ethod(null, "text", list); // exception with arg1 } public void <span style="text-decoration:line-through;color:red">m</span>hod(null, "text", list); // exception with arg1 } public void <span style="text-decoration:underline;color:green">ass</span>e<span style="text-decoration:underline;color:green">r</span>t<span style="text-decoration:underline;color:green">Met</span>hod(Object arg1, String arg2, Collection arg3) { Assert.notNull(arg1, "arg1"); Assert.notNullOrEmpty(arg2, "arg2"); Assert.notNullOrEmpty(arg3, "arg3"); ... // ... } </pre><span style="text-decoration:underline;color:green"> ====Helper</span> <span style="text-decoration:underline;color:green">example:==== <pre name="java"> 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 // ... } </pre> ====ResourceHelper example:==== ResourceHelper work like ResourceBundle, but reads properties files in UTF-8 encoding. <pre name="java"> public void resourceHelperExaple() { ResourceBundle rb = ResourceHelper.getResourceBundle("com/emsoft/utils/resources/ErrorMessages"); String value = rb.getString("key"); // or value = ResourceHelper.getString("com/emsoft/utils/resources/ErrorMessages", "key"); } </pre> ====StringHelper example:==== <pre name="java"> 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" } </pre> ====StringHelper example:==== <pre name="java"> 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" } </pre> </span><span>
 
 
Close
loading
Please Confirm
Close