Simple Guava ForwardingCache I use when developing value loaders

Guava caches are great, but developing with them can be troublesome.

One solution is wrapping the underlying cache using a simple implementation of the ForwardingCache class:

Instead of using a Cache directly from a CacheBuilder, you can wrap it with this class and toggle debug mode.

Note: if you’re using a LoadingCache, you’ll want to derive from the ForwardingLoadingCache instead and override the get(K key) and getAll(Iterable<? extends K> keys) methods as well.

Helpful utility for searching a Java exception chain for a particular exception type

Sometimes I want to search a chain of exceptions (caught->cause1->cause2…) for an exception of a particular type. This isn’t a difficult problem, but having a simple utility makes it easier:

It’s pretty easy to use:

How to silence ANTLR4’s error messages

By default a org.antlr.v4.runtime.Parser instance, as a subclass of org.antlr.v4.runtime.Recognizer, will have a org.antlr.v4.runtime.ConsoleErrorListener attached to it. Error messages will be piped through this listener and out to System.err, where they will be lost forever.

You can silence this output by calling removeErrorListeners() before parsing, and if you want to capture the error messages for parsing and display, you can add a custom BaseErrorListener implementation.

Here’s a simple one that keeps every error in a List:

You would add it to a Parser using the addErrorListener method.

Here’s a complete example:

Keycode constants for the Logitech R800 Professional Presentation

If you’re using an R800 on the mac for a custom application along with my CSTEventManager library, the following keycodes can be used. Don’t forget that you should configure the R800 as an ISO keyboard.

Code Number Description
116 The Previous Slide button
121 The Next Slide button
96 Start/Stop Presentation toggle button
47 Blank screen toggle button

Adding NOT-MODIFIED (304) behavior to AFNetworking 2

After reading this stackoverflow post, I thought that I would simplify it by subclassing AFHTTPRequestOperationManager and wrap all of this stuff in a new method that I called GET:parameters:success:failure:unmodified:

According to this other stackoverflow post, NSURLCache needs a little bit of time to warm up, so I added a class initializer. From your AppDelegate’s application:didFinishLaunchingWithOptions: method you can call this early:

Using this new method is simple:

Hopefully this will be useful for you. If you’re worried about a nil unmodified or success block, you can prefix those calls with if (unmodified) unmodified(operation,responseObject) and if (success) success(operation,responseObject).

A little macro magic for handling NSError** within a method

I don’t like checking if the caller passed me a nil NSError** in my own methods, especially if the implementation is somewhat lengthy, so I’ve put together a very basic C macro that declares an autoreleasing NSError* to use as temporary storage in case of a null NSError** parameter from the caller:

Imagine that I have a method named foo that takes a single NSString* and an optional NSError**:

Now I don’t need to check if I’ve been passed a valid NSError** pointer, because it will be assigned to the local tempError variable if it was nil.

Suppressing Tomcat error messages for all applications on a host

Sometimes I write web applications for Tomcat where I don’t want to report any errors beyond the HTTP status code. The quickest method for disabling those messages is configuring a Valve on the host section of the server.xml configuration file.

If you subclass the org.apache.catalina.valves.ErrorReportValve class (it’s in your Tomcat’s lib/catalina.jar) and override the report method like this:

The client still receives the HTTP status code and anything already buffered, but it hides all of the automatically generated Tomcat error information.

To install it, just JAR up the class and put it in your Tomcat’s lib folder, then add the errorReportValveClass attribute to your Tomcat’s server.xml file.

iOS 6, Base Localization, and UISwitch

I just thought I’d share something I ran across today with iOS 6 and UISwitch controls.

If you put a UISwitch in a XIB and expect the text inside of the UISwitch to appear in the right language, iOS 6 will look in your app’s (current region name).lproj for a file named (same as xib).strings.

For example, if I have a UI defined in SettingsViewController.xib, and that UI has a UISwitch, and I want a Japanese user to see that switch’s on/off in Japanese, I need to make a file named ja.lproj/SettingsViewController.strings. The strings file doesn’t need to have anything in it, but it can’t be zero length. The easiest thing to do is just throw in something like:

And then convert that file to a binary plist using the plutil command-line tool:

Note: don’t forget the ‘1’ in binary1. More information on man plutil.

If you’re developing exclusively within XCode 5, then I’d suggest just localizing any XIBs and let XCode do the magic for you.