Strona główna > Java > Clojure way in java

Clojure way in java

A shorter way to building anonymous class in java, that I described here, isn’t more readable. But it gives a chance writing in Java in more clojure way. It is an experiment to check how difficult it could be to write in that way.
An example (do something with array of string):

String[] tab = new String[] {"short", "Long Long Long Long String", "other long string"};
printFilteredStrings(tab);

private static void printFilteredStrings(String[] tab) {
    forEach.eval(
        sort.eval(
            getFliteredList.eval(
                populateListFromTab.eval(
                    tab,
                    new ArrayList<String>()),
                myFliter),
            myComparator),
        printIt);
}


Rest of the code:

    static {
        AnonymousClassCreator.createStaticObjectForAllFields(ClosureTest.class);
    }

    static Function printIt;
    static Function printIt2;
    static Function forEach;
    static Function getFliteredList;
    static Function myFliter;
    static Function populateListFromTab;
    static Function myComparator;
    static Function sort;

    static private void printIt(Object item) {
        System.out.println("printIt : " + item);
    }
    static private void printIt2(Object item) {
        System.out.println("item value : '" + item +"'");
    }

    static private <T> void forEach(List<T> list, Function fn) {
        for (T item : list) {
            fn.eval(item);
        }
    }

    static private <T> List<T> sort(List<T> list, final Function fn) {
       Collections.sort(list, new Comparator<T>() {
        public int compare(T o1, T o2) {
            return (Integer)fn.eval(o1, o2);
        }
       });
       return list;
    }

    static private <T> List<T> getFliteredList(List<T> list, Function filter) {
        List<T> listNew = new ArrayList<T>();
        for (T item : list) {
            if ((Boolean)filter.eval(item)) {
                listNew.add(item);
            }
        }
        return listNew;
    }

    static private <T> List<T> populateListFromTab(T[] tab, List<T> list) {
        for (T item : tab) {
            list.add(item);
        }
        return list;
    }

    static private boolean myFliter(String item) {
        return item.length() > 10;
    }

    static int myComparator(Object o1, Object o2) {
        return (o1.toString().length() - o2.toString().length());
    }

AnonymousClassCreator was in previous post. I change Function and FunctionImpl, now eval returns value.

public class FunctionImpl extends BaseAnonymousClass implements Function {

    public static void registerCreator() {
        registerCreator(Function.class, new Creator() {
            public Object create(Object target, Method method) {
                return new FunctionImpl(target, method);
            }
        });
    }

    private FunctionImpl(Object target, Method method) {
        super(target, method);
    }

    public <T> Object eval(T t) {
       return _eval(new Object[] { t });

    }

    public <T> Object eval(T... t) {
        return _eval(t);
    }
}

It isn’t more readable, it could be written more in old way, without using too much methods as objects:

       forEach(
            sort(
                getFliteredList(
                    populateListFromTab(
                        tab,
                        new ArrayList<String>()),
                    myFliter),
                myComparator),
            printIt);

But useing „method as object” I can change code in that way (how it will be printed):

        String[] tab = new String[] {"short", "Long Long Long Long String", "other long string"};
        printFilteredStrings(tab);
        printIt = printIt2;
        printFilteredStrings(tab);
About these ads
Kategorie:Java Tagi:
  1. Brak komentarzy.
  1. No trackbacks yet.

Dodaj komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

WordPress.com Logo

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Twitter picture

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

Follow

Otrzymuj każdy nowy wpis na swoją skrzynkę e-mail.

%d bloggers like this: