# Tag Archives: Snippet

## Pretty Math on Wikipedia

In the days of MathJax, the math images on Wikipedia look inferior by a huge margin. The MathJax guys got us covered; they provide a userscript1 that dynamically injects MathJax into Wikipedia sites2. It may load a little longer, but I think this is worth the wait:

Without MathJax

With MathJax

1. Userscripts are pieces of Javascript your browser executes on top of any website. Firefox users use Greasemonkey, others see here.
2. Make sure to change the @include to http://*.wikipedia.org/wiki/* so the script works on all Wikipedias.

## Spread Out List Items With LaTeX beamer

Everybody tells you that presentation slides should contain only as few pieces of information as possible. If you need more than one word or picture you better have at most three to five items. If you try to do exactly that using beamer for $$\LaTeX$$, this is what you get:

Aesthetics aside, item placement is just bad. You would want the items to fill the available space somewhat. In normal documents, package enumitem allows you to control such things both globally and per environment. It seems to conflict with beamer, though. I headed over to tex.SE and got good answers; Werner suggests to add this to the preamble:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@listI}{\itemsep3\p@}{\itemsep3em}{}{}
\makeatother

With this, your beamer slides do way better:

Note that you can adjust spacing by changing number and unit in \itemsep3em.

## Lazy Deserialization in Java

While coding away on our master’s project, my team and I faced a problem. In our setting, users provide Java byte code to a client application which instruments it and passes the resulting JAR archive to remote servers via RMI. The servers execute this code in separate processes and feed it inputs values fetched from the client, again via RMI. Now, this can be a problem as users may provide their own implementations for those input values. Both client and separately started processes have, of course, access to these implementations, but servers may not.

The solution intended by Sun back then is to set up a code repository servers can access and download the necessary code from. We considered this overkill given that the server does not even care about the value1: after RMI deserializes the input, we immediately serialize it again and pass it directly to its child process. Dynamically loading all classes from the archive at hand felt messy for the same reasons.

So we came up with this pattern which I call lazy deserialization:

import org.apache.commons.lang.SerializationUtils;

class Value<T extends Serializable> implements Serializable {
private final byte[] data;
private transient T value;

Value(T value) {
this.value = value;
this.data = SerializationUtils.serialize(value);
}

T get() {
if ( this.value == null ) {
this.value = (T)SerializationUtils.deserialize(this.data);
}

return this.value;
}
}

As long as servers does not access the wrapped value, everything is fine; as far as they are concerned, they are passing around dressed-up arrays of bytes. In our case, we added several primitive—and therefore trivially serializable—members of the wrapped value to this class in order to enable the server to be able to report back certain things.

I think this pattern can be of use in any situation that involves passing around serialized objects only some nodes are interested in. After all, you might want to avoid deserializing even if you have access to all necessary class files; after all, (de)serializing can take up quite some time you might want to see better spent.

1. Especially so as we have a many-to-many relation between clients and servers, that means many repositories and many loaded classes you do not need.

#!/bin/bash
while [ true ]; do xsendkeys Shift_L; sleep 30; done