Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, July 10, 2015

pipelines and functional java

Mr. Fowler has written a nice series on using pipelines instead of iteration for processing collections.

On a similar note, I recently came across this presentation on reactive programming in Java which also notes the benefits of composing operations as pipelines (though with a particular focus on processing sequences of observable events since this is reactive java after all).  If you read through the deck to the last slide, you'll notice a humorous meme which represents more concisely Fowler's main point.

I'm so glad this exists.

Obviously, as shown in my Joinery project, I'm a big fan of this programming style.  It provides a powerful mechanism for expressing the kinds of operations that are required when operating on large amounts of data while yielding a very high signal:noise ratio when reading the code.

Monday, July 12, 2010

a survey of web visualization toolkits

I'm always on the lookout for new tools to create graphs, charts, and other visualizations. It is an obsession that dates back to my days slaving over gnuplot and R scripts (or even *gasp* metapost). Here are some more modern toolkits I've come across which lend themselves to visualizations for the web.

  • Processing has to be one of my favorites, probably because I've been using it the longest.
  • And if Processing isn't web-2.0-enough for you, there is always the Javascript implementation.
  • Protovis - any visualization toolkit that can be used to create an interactive version of Minard’s Napoleon has to get good marks.
  • Google has Chart Tools.
  • Degrafa has been used to create some beautiful visualizations.
  • Or there is the slightly higher-level Axiis if you prefer (which is built on top of Degrafa).
  • Should you be religiously opposed to flash, you might like dygraphs
  • Prefuse is another Java toolkit with some great graph and chart types
  • And Flare (also from the Prefuse guys) you post those interactive visualizations to the web in flash.
Ok everyone, which ones haven't I seen yet?

    Friday, July 9, 2010

    normals - a processing experiment

    As part of a visualization I'm attempting to create, I was doing some experiments in processing. One of the experiments involved distributing points randomly on a sphere, and drawing the normal vectors for those points. Add in a trackball for rotating, panning, and zooming and it ended up being a pretty cool interactive toy by itself.

    Monday, July 5, 2010

    plasmodium protein interaction network visualization

    I was recently finishing my write-up about using processing to create interactive art. In that article I wanted to mention the animation and physics processing libraries by Jeff Traer Bernstein and while looking up the references I was reminded of this visualization. It has always been one of my favorites, so I'm finally getting around to sharing it.

    Sunday, July 4, 2010

    new required reading for programmers

    starting now I'm putting this paper on my required reading list for all programmers I hire:

    http://www.eecs.harvard.edu/~mdw/papers/seda-sosp01.pdf

    thanks to the authors (obviously) and the guys at http://everythingsysadmin.com/ for pointing it out.

    Monday, June 8, 2009

    dependency injection in 100 lines (or less)

    If you intend to develop software in a modular fashion, at some point you will need to swap out one implementation of an interface (or service) for another. Now, the naive approach, to simply find and replace will work for a while. However, at some point you will have too many instances or simply tire of switching back and forth (with all the recompiling that entails, I'm looking at you Java).

    Dependency injection is a type of inversion of control (see Fowler), which means that instead of creating dependencies by having the code you write reference other code (either that you also wrote, or in libraries) directly at compile time, you switch things up and let someone else (the injector) determine how to satisfy your requirements at runtime.

    For my object oriented java class we do a largish (for a class anyway) software project. For example, past projects have included peer to peer file sharing systems, web application server, and email gui's. The point is, something that is too big for a single person or group to complete in the time allotted and something that can be easily split into smaller pieces. We design the solution as a class, write the interfaces, and then each student (or sometimes group) is responsible for implementing one component of the complete system.

    Of course, I supply bytecode solutions (but not the source) for all the components and mock classes for testing. And finally, I needed an easy way to sometimes use my implementations, sometimes use my mockups, and other times use my students solutions. So, given that background, you will find my solution below.

    It is not an enterprise IoC container and it has only been tested in a classroom environment (it may very well cause your computer to burst into flames, you have been warned). However, it might serve as an instructive example precisely because, it is short and to the point.

    import java.io.*;
    import java.lang.reflect.*;
    import java.util.*;
    
    public class ObjectFactory
    {
        private static String CONFIG = "ObjectFactory.properties";
        private static Properties config = new Properties();
    
        public static <T> T create(Class<T> cls)
        {
            Class rcls;
            Constructor<T> cn = null;
            Class<?>[] pt = null;
            Object[] params = null;
            T inst = null;
            String name, real;
    
            if (config.isEmpty()) {
                try {
                    InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG);
                    if (in == null)
                        throw except("file not found");
                    config.load(in);
                } catch (IOException e) {
                    throw except(e.toString());
                }
            }
    
            name = cls.getName();
    
            if (cls.isInterface() && !config.containsKey(name))
                throw except("no configured implementation for %s", name);
    
            real = (String)config.get(name);
            try {
                rcls = real != null ? Class.forName(real) : cls;
    
                for (Constructor<T> c : rcls.getConstructors()) {
                    if (cn == null || c.getParameterTypes().length < pt.length) {
                        cn = c;
                        pt = c.getParameterTypes();
                    }
                }
    
                params = new Object[pt.length];
                for (int i = 0; i < params.length; i++)
                    params[i] = create(pt[i]);
    
                inst = cls.cast(cn.newInstance(params));
            } catch (InvocationTargetException e) {
                throw except("invocation failed for %s is %s, " +
                    "but %s not a subclass of %s", name, real, real, name);
            } catch (ClassCastException e) {
                throw except("the configured implementation of %s is %s, " +
                    "but %s not a subclass of %s", name, real, real, name);
            } catch (ClassNotFoundException e) {
                throw except("the configured implementation of %s is %s, " +
                    "but %s cannot be found", name, real, real);
            } catch (InstantiationException e) {
                throw except("the configured implementation of %s is %s, " +
                    "but a new %s cannot be created", name, real, real);
            } catch (IllegalAccessException e) {
                throw except("the configured implementation of %s is %s, " +
                    "but a new %s cannot be created", name, real, real);
            }
    
            return inst;
        }
    
        private static RuntimeException except(String msg, Object ... args)
        {
            return new RuntimeException(String.format(
                "configuration error in %s: " + msg, CONFIG, args));
        }
    }
    

    Configuration is simple (it's a standard Java properties file), just a file named ObjectFactory.properties, located via the classpath. You just map an interface or abstract class to its concrete implementation. See the example below.

    Message=MockMessage
    Folder=MockFolder
    

    So there you have it, 76 lines, probably could use some comments (and still be under 100). On the other hand, I probably could have squeezed it more too, but this version has pretty reasonable errors.

    Wednesday, July 23, 2008

    introducing jsqueeze

    Not that I want to get into any js framework debates, but I've been using prototype and scriptaculous for my ajax ui toolkit recently. However, I do have to give a nod to the dojo team for their javascript compressor, shrink safe. It seems to me that any javascript compressor/obfuscator must know about the underlying structure of the language if it wants to save every last byte.

    As much as I like the idea of shrink safe, I have to confess that patching the rhino javascript process doesn't seem to most elegant way to harness its power. So, with that in mind, I managed to adapt the shrink safe code to simply use the rhino api to accomplish the same task. I've called by javascript compressor jsqueeze (or jsqz for short).

    Anyway, if your interested and want to give it a spin, you can download the source here.