Wednesday

Java Fun Continued - Groovy FTW

Here's the equivalent Groovy for my previous Java abomination that prints the Java wrapper methods for me:

new File("src/main/java/Foo.java").text.
replaceAll(/(?sm)\s*public\s+(\S+)\s+(\S+)\((.*?)\)\s*\{/) {
s,ret,name,params ->
print """
public ${ret} ${name}(${params}) {
"""
if("void" != ret) {
print "return "
}
print "foo.${name}("
// -1 hack avoids index out of bounds when params is ''
print params.trim().split(/\s*,\s*/).collect {
it.split(/\s+/)[-1]
}.join(',')
print """);
}
"""
}

I could change the prints to concatenation and reformat the whole thing as a single line, so it only counts as one statement. Technically I cheated a little because this version doesn't look up the methods by reflection, but it doesn't make a difference in the result, and wouldn't significantly shorten the Java code. Definitely more readable than the Java. The JavaScript is a little messier since you don't have multi-line string literals, GStrings, and closures are function(it){ return it.foo; } instead of { it.foo }.

Java Fun: When all you have is a hammer...

I ought to be ashamed, but I was bored and lazy, so I thought I'd test my somewhat dormant Java skills. Can you tell me (without running it) what this code does?

Method[] methods = Foo.class.getMethods();
String source = FileUtils.readFully(new FileReader(
"src/main/java/"
+ Foo.class.getCanonicalName().replaceAll(
"\\.", "/") + ".java"));
PrintStream out = System.out;
for (Method method : methods) {
Matcher matcher = Pattern.compile(
"public (\\S+?) " + method.getName() + "\\((.*?)\\)",
Pattern.DOTALL | Pattern.MULTILINE).matcher(source);
if (matcher.find()) {
String ret = matcher.group(1);
String params = matcher.group(2);
out.printf(" public %s %s(", ret, method.getName());
out.print(params);
out.print(") {\n ");
if (!"void".equals(ret)) {
out.print("return ");
}
out.print("foo.");
out.print(method.getName());
out.print('(');
if (!StringUtils.isEmpty(params)) {
out.print(StringUtils.join(CollectionUtils.collect(
Arrays.asList(params.split(",\\s*")),
new Transformer() {
public Object transform(Object input) {
return ((String) input).split(" ")[1];
}
}), ','));
}
out.println(");\n }\n");
}
}

I'll post the equivalent Groovy (and maybe JavaScript) later. I imagine I can do it in a single statement function. I don't miss Java very much.

New (to me) Bike: Issues letting go

My friend John is letting me borrow his fancy Specialized bike that he doesn't ride anymore. I'd say it's about a third of the weight of Old Creaky, my bike that I built at the Yellow Bike Project, and is definitely nicer in every way, but I'm having some problems letting go.

Firstly, this thing is a true road bike, everything is built for speed. The problem is that the tires have little to no tread on them (by design) so if it's remotely wet or gravelly, I make myself slow way down because I'm afraid this thing will slip out from under me. Old Creaky had some decent tread, but not too much. Just something I need to get used to I supposed.

Secondly, the shifters are those neat kind that are built into the brakes, so naturally they're indexed. I've been riding for the past year with friction shifters, so I'm used to being able to go from 5th back to 1st in about 2 seconds just by pushing a little lever down. With these indexed things, I have to click, click, click, and then I'm not sure where I am because they don't have any sort of indicator, so I'm constantly looking down to see where the chain is. I suppose I'll get used to it, I just hope I don't get hit by a car in the meantime.

Lastly, John is about 6'5", and although I'm 6'3" Old Creaky was not built for a 6'3" person, while John's bike is built for John, so I'm bent over a lot further than my back is used to. I'm sure I'll get used to it too.

So, three things to beware of when you get a new bike. I wouldn't keep riding it if I didn't think it was a net positive, but I also never thought any of these things through. I'm working on my fear of change.