anonymous classes are not closure

anonymous classes are not closure

tired of doing “for”, lets implement internal iterator for my collection class

import java.text.*;
import java.util.*;


public class closure_test {
    public static void main(final String[] args) {
        test_without_closure();
        test_with_closure2();
        test_with_closure();
    }

    static void test_without_closure() {
        try {
            String  userhome = null;
            TheList lst      = new TheList();

            for (Iterator it = lst.iterator(); it.hasNext();) {
                String child = (String) it.next();

                if (child.indexOf("user.home") != -1) {
                    userhome = child.toString();

                    // no need to make userhome final
                    // and I can break too
                    break;
                }
            }

            System.out.println(userhome);
        } catch (final Throwable t) {
            t.printStackTrace();
        }
    }

    static void test_with_closure() {
        try {
            String  userhome = null;
            TheList lst      = new TheList();
            lst.forEach(new TheList.Closure() {
                    public void run(final Object child) {
                        if (((String) child).indexOf("user.home") != -1) {
                            //userhome=child.toString(); // wont compile
// local variable userhome is accessed from 
// within inner class; needs to be declared final

// if I make userhome then cant assign value to it, 
// see test_with_closure2 for ugly solution

// cant break
                        }
                    }
                });
        } catch (final Throwable t) {
            t.printStackTrace();
        }
    }

    static void test_with_closure2() {
        try {
            final String[] userhome = new String[1];
            TheList        lst      = new TheList();
            lst.forEach(new TheList.Closure() {
                    public void run(final Object child) {
                        if (((String) child).indexOf("user.home") != -1) {
                            userhome[0] = child.toString(); // now compile, its ugly
                                                            // still cant break?
                        }
                    }
                });
            System.out.println(userhome[0]);
        } catch (final Throwable t) {
            t.printStackTrace();
        }
    }
}


class TheList {
    private static final MessageFormat FMT  = new MessageFormat("{0}={1}");
    private final ArrayList            list = new ArrayList();

    public TheList() {
        Properties props = System.getProperties();

        for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
            Map.Entry ent = (Map.Entry) it.next();
            list.add(FMT.format(new Object[] { ent.getKey(), ent.getValue() }));
        }
    }

    public void forEach(final Closure closure) {
        for (Iterator it = list.iterator(); it.hasNext();) {
            closure.run(it.next());
        }
    }

    public Iterator iterator() {
        return list.iterator();
    }

    public static interface Closure {
        public void run(final Object child);
    }
}
anonymous classes are closure? NO
what does non-final do to a local variable that makes it non-accessible from inner class ?
closure implemented using inner class are ugly and difficult to use with all unnecessary method implementation.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s