Skip to main content
This revision made November 15, 2010 19:23, 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 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 name="java"> public void assertExa<span style="text-decoration:underline;color:green">m</span>ple() { List list = new ArrayList(); assertMethod(new Object(), "text", list); // exception with arg3 list.add(new Object()); ... ResourceHelper work like ResourceBundle, but reads properties files in UTF-8 encoding. <pre name="java"> public void resourceHelperExa<span style="text-decoration:underline;color:green">m</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> ====<span style="text-decoration:line-through;color:red">StringHe</span>ple() { ResourceBundle rb = ResourceHelper.getResourceBundle("com/emsoft/utils/resources/ErrorMessages"); String value = rb.getString("key"); // or ... </pre> ====<span style="text-decoration:underline;color:green">A</span>lp<span style="text-decoration:line-through;color:red">e</span><span style="text-decoration:underline;color:green">hanumCompa</span>r<span style="text-decoration:underline;color:green">ator</span> example:==== <pre name="java"> public <span style="text-decoration:underline;color:green">static </span>r example:==== <pre name="java"> public void <span style="text-decoration:line-through;color:red">str</span>void <span style="text-decoration:underline;color:green">ma</span>in<span style="text-decoration:line-through;color:red">gHelperMethod</span>in(String<span style="text-decoration:underline;color:green">[]</span>(String arg<span style="text-decoration:line-through;color:red">1</span> arg<span style="text-decoration:underline;color:green">s</span>) { <span style="text-decoration:underline;color:green">L</span>) { i<span style="text-decoration:line-through;color:red">f (</span>i<span style="text-decoration:underline;color:green">st<</span>String<span style="text-decoration:line-through;color:red">He</span><span style="text-decoration:underline;color:green">> </span>l<span style="text-decoration:line-through;color:red">p</span><span style="text-decoration:underline;color:green">ist = n</span>e<span style="text-decoration:underline;color:green">w A</span>er<span style="text-decoration:line-through;color:red">.</span>r<span style="text-decoration:underline;color:green">rayL</span>is<span style="text-decoration:line-through;color:red">NullOrEmp</span>t<span style="text-decoration:line-through;color:red">y(a</span>ist<span style="text-decoration:underline;color:green"><St</span>r<span style="text-decoration:underline;color:green">in</span>rg<span style="text-decoration:line-through;color:red">1</span>g<span style="text-decoration:underline;color:green">>(</span>)<span style="text-decoration:line-through;color:red">)</span><span style="text-decoration:underline;color:green">;</span> <span style="text-decoration:underline;color:green">list.add("10b"); </span> <span style="text-decoration:line-through;color:red">re</span> <span style="text-decoration:underline;color:green"> lis</span>t<span style="text-decoration:line-through;color:red">urn</span><span style="text-decoration:underline;color:green">.add("10")</span>; <span style="text-decoration:line-through;color:red">//</span><span style="text-decoration:underline;color:green">list.add("a10"); </span> <span style="text-decoration:line-through;color:red">for</span><span style="text-decoration:line-through;color:red">example,</span> <span style="text-decoration:underline;color:green"> list.</span> a<span style="text-decoration:line-through;color:red">rg</span>a<span style="text-decoration:underline;color:green">dd("</span>1<span style="text-decoration:underline;color:green">df0"); </span>1 <span style="text-decoration:line-through;color:red">==</span> <span style="text-decoration:underline;color:green"> list.add(</span> "1<span style="text-decoration:line-through;color:red">2</span>"1<span style="text-decoration:underline;color:green">0</span>a<span style="text-decoration:underline;color:green">"); li</span>as<span style="text-decoration:line-through;color:red">2</span>s<span style="text-decoration:underline;color:green">t.a</span>d<span style="text-decoration:line-through;color:red">fg</span><span style="text-decoration:underline;color:green">d(</span>"<span style="text-decoration:underline;color:green">3");</span>" <span style="text-decoration:line-through;color:red">L</span> <span style="text-decoration:underline;color:green">l</span>ist<span style="text-decoration:line-through;color:red"><</span><span style="text-decoration:underline;color:green">.add("1"); list.add("2"); </span>S<span style="text-decoration:underline;color:green">ys</span>t<span style="text-decoration:underline;color:green">em.out.p</span>Strin<span style="text-decoration:line-through;color:red">g> chu</span>rin<span style="text-decoration:underline;color:green">tl</span>n<span style="text-decoration:line-through;color:red">k</span><span style="text-decoration:underline;color:green">("before: " + li</span>s<span style="text-decoration:underline;color:green">t); </span>s <span style="text-decoration:line-through;color:red">=</span> <span style="text-decoration:line-through;color:red">S</span> <span style="text-decoration:underline;color:green"> Collec</span>t<span style="text-decoration:underline;color:green">ions.so</span>r<span style="text-decoration:underline;color:green">t(l</span>i<span style="text-decoration:underline;color:green">st, </span>trin<span style="text-decoration:line-through;color:red">gH</span>ne<span style="text-decoration:underline;color:green">w A</span>elp<span style="text-decoration:line-through;color:red">e</span>lp<span style="text-decoration:underline;color:green">hanumCompa</span>r<span style="text-decoration:line-through;color:red">.</span><span style="text-decoration:underline;color:green">ator()); Sy</span>s<span style="text-decoration:underline;color:green">tem.out.</span>sp<span style="text-decoration:line-through;color:red">l</span>p<span style="text-decoration:underline;color:green">r</span>i<span style="text-decoration:underline;color:green">n</span>t<span style="text-decoration:underline;color:green">ln</span>(<span style="text-decoration:underline;color:green">"</span>a<span style="text-decoration:underline;color:green">fte</span>it(ar<span style="text-decoration:line-through;color:red">g1,</span>r<span style="text-decoration:underline;color:green">:</span> "<span style="text-decoration:line-through;color:red">[0-9]</span><span style="text-decoration:underline;color:green"> </span>+<span style="text-decoration:line-through;color:red">"</span><span style="text-decoration:underline;color:green"> list</span>); <span style="text-decoration:underline;color:green">} </pre> Result: <pre> :before:</span> <span style="text-decoration:underline;color:green">[10b,</span><span style="text-decoration:underline;color:green">10,</span><span style="text-decoration:underline;color:green">a10,</span>); <span style="text-decoration:line-through;color:red">//</span> <span style="text-decoration:underline;color:green">1df0,</span><span style="text-decoration:line-through;color:red">chunks</span><span style="text-decoration:underline;color:green">10a,</span><span style="text-decoration:line-through;color:red">==</span><span style="text-decoration:underline;color:green">3,</span><span style="text-decoration:line-through;color:red">"</span>1<span style="text-decoration:line-through;color:red">2"</span>, <span style="text-decoration:line-through;color:red">"</span> 1, <span style="text-decoration:underline;color:green">2] :</span>a<span style="text-decoration:line-through;color:red">s"</span><span style="text-decoration:underline;color:green">fter: [1</span>, <span style="text-decoration:line-through;color:red">"</span><span style="text-decoration:underline;color:green">1df0, </span>2<span style="text-decoration:line-through;color:red">"</span>2, <span style="text-decoration:underline;color:green">3, 10, 10a, 10b, a10] </pre> ====ExceptionHelper example:==== <pre name=</span>, "<span style="text-decoration:line-through;color:red">dfg</span>"<span style="text-decoration:underline;color:green">java</span><span style="text-decoration:underline;color:green">></span> <span style="text-decoration:underline;color:green"> </span> <span style="text-decoration:underline;color:green">public</span> <span style="text-decoration:underline;color:green">void</span><span style="text-decoration:underline;color:green">exceptionHelperExample()</span><span style="text-decoration:underline;color:green">{ </span>" <span style="text-decoration:line-through;color:red">S</span> <span style="text-decoration:underline;color:green"> </span>tr<span style="text-decoration:line-through;color:red">i</span><span style="text-decoration:underline;color:green">y { throw </span>n<span style="text-decoration:line-through;color:red">g</span><span style="text-decoration:underline;color:green">ew</span> <span style="text-decoration:underline;color:green">Ba</span>s<span style="text-decoration:underline;color:green">eRun</span> st<span style="text-decoration:line-through;color:red">r</span>t<span style="text-decoration:underline;color:green">imeException(); </span> <span style="text-decoration:line-through;color:red">=</span> <span style="text-decoration:underline;color:green"> } catch (Exception ex) { </span>S<span style="text-decoration:underline;color:green">ys</span>t<span style="text-decoration:underline;color:green">em.out.p</span> Strin<span style="text-decoration:line-through;color:red">g</span>rin<span style="text-decoration:underline;color:green">tln(Exception</span>Helper.<span style="text-decoration:underline;color:green">ge</span>Helper.t<span style="text-decoration:line-through;color:red">oS</span>t<span style="text-decoration:underline;color:green">Excep</span><span style="text-decoration:line-through;color:red">r</span>ti<span style="text-decoration:underline;color:green">o</span>n<span style="text-decoration:underline;color:green">Messa</span>g<span style="text-decoration:underline;color:green">e</span>ing(<span style="text-decoration:line-through;color:red">chunks</span>(<span style="text-decoration:underline;color:green">ex</span>, <span style="text-decoration:line-through;color:red">"*"</span><span style="text-decoration:underline;color:green">true</span>)<span style="text-decoration:underline;color:green">)</span>); <span style="text-decoration:line-through;color:red">//</span> <span style="text-decoration:line-through;color:red">s</span>; <span style="text-decoration:underline;color:green"> Excep</span>t<span style="text-decoration:underline;color:green">ionHelpe</span>r<span style="text-decoration:underline;color:green">.logAndThrow(ex); </span>tr <span style="text-decoration:line-through;color:red">==</span> <span style="text-decoration:line-through;color:red">"12*as*2*dfg"</span> <span style="text-decoration:underline;color:green"> }</span> } </pre><span style="text-decoration:line-through;color:red"> </span><span>
 
 
Close
loading
Please Confirm
Close