Tuesday 12 June 2018

Content assist for static methods in Eclipse

It's a great time saver to get auto-completion and import generation in Eclipse even for those of us who are fairly quick with the keyboard.  But Eclipse won't search for static class methods without a little bit of help.

The newer version of Eclipse does include more defaults, but a preferences dialog is provided to tell Eclipse that you're likely to want to use some static imports.

To add a class go to Java | Editor | Content Assist | Favourites in preferences:





What's shown here are the defaults in STS 3.9.4.RELEASE.


Wednesday 6 June 2018

Lazy Logging with Log4j2 and Java 8+

Here's a good reason to switch to log4j2 and one I don't see enough people using yet.

At times we are reluctant to use log4j because of the overhead of evaluating the messages even where the log level does not apply to the current setting.  An alternative is to wrap our logging with a guard like so:

1
2
3
if (logger.isDebugEnabled()) {
    logger.debug("Debug something " + something.getInfo());
}

Littering our code with these guards makes our code look like it's more about logging than computing.

Log4j2 supports Java 8 and you can now lazily log in a couple of ways.

Lazily evaluating formatter arguments 

If you're using arguments in your messages you can replace the values with lambda expressions and their evaluation will be delayed 'till it's determined that the message needs to be written.

1
LOG.debug("a string with a parameter {}",  LazyLoggingClass::getString);

In this example getString won't be called unless we're at an appropriate log level to print debug messages.

Lazily evaluating the message argument

It's probably more common to use a string catenation expression in legacy logging.  It's also permitted to use a lambda expression for the message itself, so you can do this:


1
LOG.debug(() -> "Final result is " + getString() );

and getString() will only be called if we are going to log DEBUG messages.

Monday 25 May 2015

MacOS Java Install Notes

Installing Java on MacOS is not as straightforward as one might hope.

While there are package managers available for MacOS (I recommend Homebrew) they typically cannot install Java themselves for licensing reasons.

Instead, install Java from an Oracle download:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

MacOS is a UNIX system under the covers and you can of course manually set a JAVA_HOME environment variable and update it as required when you install a new JDK, but there is some system support on update if you instead add this to your ~/.profile:

export JAVA_HOME=$(/usr/libexec/java_home)
And update your PATH to include:
export PATH=$JAVA_HOME/bin:$PATH

As java_home will return the system configured Java version.
You can set this through the Java preferences dialog, go to System Preferences and choose Java.  You'll see a dialog that looks something like this:

And one that looks like this:
click the "View..." button to see runtimes

runtimes are listed here

Other Notes

If you have multiple Java versions installed you can use the -v 1.x parameter to select a particular version of Java.  So for example if you often have to use Java 6 for backwards compatibility:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)

To see all installed Java versions run:

/usr/libexec/java_home -V

A separate 'tap' for homebrew may allow installation of Java now, albeit with a bit more preparatory setup, see:

http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html


Sunday 23 September 2012

Ask your graphic designer

That's right, ask your graphic designer, not me. :)

There is a bit of a demilitarized zone (or perhaps it really is militarized) between graphic design folks and software developers. Luckily I'm in the latter group but how do I field practical questions from clients about how to dress up their site? For the self serve crowd what follows are some rule-of-thumb guidelines for the types of graphics that may be required for web sites generally and what formats to provide them in.

Pixel Density

Historically web designers have used 72dpi as a rule of thumb to generate artwork that renders well on a computer screen and is not overly large.  However, these days the pixel density on a monitor is in fact closer to 100dpi and we recommend this density for all artwork.

Graphics File Formats

Some of the most common graphics formats are JPEG, GIF and PNG.  GIFs use a lossless compression but are limited to 256 colours, and this format is owned by Unisys and may be required to be licensed in the future.  We recommend that you avoid GIFs for these reasons.  Only use this if you need quick and dirty low resolution animations and have no other options.

JPEGs are lossy but manage up to 16 million colours and may be used for relatively large photographs are images.  


As a rule of thumb prefer PNG, which is patent-free, uses a lossless compression, compresses fairly well unless the image is very large, in which case use a JPEG.

Fonts

The fonts that are displayed in the browser are loaded from the local machine when the browser renders a web page, so if you are using a commercial font it may not render well in the browser. 

While there are methods existing and in development to permit the download of fonts, this can have a significant impact on initial rendering and, more importantly, may entail intellectual property issues that are better avoided.  Restrict the use of commercial fonts to text that is pre-rendered as a graphic and choose a font and font family that is likely to work in a range of browsers.  See http://en.wikipedia.org/wiki/Web_typography and http://www.speaking-in-styles.com/web-typography/Web-Safe-Fonts/ for specifics.

If you have some existing branding or templates for official communications, it probably makes sense to choose fonts that are consistent with those materials.  If you are using commercial fonts in your branding, for example in a business card, ask your designer to tell you what is a similar font or font family based on the list of web safe fonts provided above.


Colours

Regular computer monitors won’t necessarily have high fidelity but simply specify your colours as regular RGB triplets.  (Your graphics designer will know what this means.)

Ideally your graphic designer can provide you with a few colours that can work well together.

Logo
A standalone logo may or may not be used on a site, sometimes it is incorporated in a banner.  A logo that can appear in each page template, generally something that is about 85px high and proportionately wide.  If you have business cards you probably already have a logo in digital form, to reprise the theme: talk to your graphic designer.

Favicon


The favicon is a small graphic that is so named because it was originally intended to decorate a site name in the favourites list in the browser.  These days it may also decorate the URL bar and/or the tab, depending which browser you are using.

While a favicon may be created in different formats and sizes, for broadest support we recommend a 16x16 pixel ICO format file in RGB colour format.

See http://en.wikipedia.org/wiki/Favicon for more information.

Banner


A banner is a graphic that appears at the top of each page and may display your company logo, motto, and puppies/flowers or whatever else is appropriate to your site. Ask your site designer for dimensions.

Other Graphical Content


A picture is still worth a thousand words and it is useful to decorate pages with appropriate graphics or photos that convey the subject of that page.  If you are selling a product these may be photos of the product, if it is a contact page perhaps it is a photo of you or your store.  More evocative images may also be appropriate.  We advise you to take care of intellectual property rights, if you are reusing images that you have found on the web make sure they are licensed for free reuse.  

Your graphics designer should be cognizant of intellectual property issues and will have sources for stock commercial or open sources images, and perhaps their own stock.  So, more simply, once you have determined what content you would like to have in your site consider discussing with your graphic designer whether they have any images that they can license and/or provide to you to choose from to accompany specific pages.