Monday 29 December 2014

My Enum Frustration

At work, there is a bit of Legacy around.

The following example of several Enum classes we have, is what is currently frustrating me (and I try actively to change them).

Let's enumerate (oh, look, I made a joke) the issues:
Replacing magic numbers with constants and enums is, of course, one of the Good Thingstm.

But in this example, they seem to have totally missed the point.

Not only have they replaced magic numbers with "magic enums", it's an eyesore to use the underscore in the Enum instances.

When you have to take refuge in the use of underscores to make things work, it is a sure sign that you are doing something seriously wrong.

Monday 22 December 2014

The Terrible Dangers of Autoboxing (Part 3) - Solution

The solution to my previous post regarding the Terrible Dangers of Autoboxing, is written below.

In our case, the type of the second expression is 0L (long) and the third is null but returns from a Long method.

Java assumes that the second and third operands have types that are convertible to numeric types.

In that case, the conditional operator causes binary numeric promotion.2

During binary numeric promotion, it is possible an unboxing is performed3 (on null in our case), which consequently fails with a NullPointerException.

The solution is to replace
return hasSurfaceArea() && getSurfaceInM2() == null ? 0L : getSurfaceInM2();
With:
return hasSurfaceArea() && getSurfaceInM2() == null ? Long.valueOf(0) : getSurfaceInM2();

P.S. Rewriting the conditional expression into an if statement, will also solve it.

Quote

Here is a brilliant quote that every Java programmer needs to remember and understand (especially that last bit):
“Autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.1

References

[1] Java SE Documentation - Autoboxing
http://docs.oracle.com/javase/7/docs/technotes/guides/language/autoboxing.html
[2] Chapter 15.25 Conditional Operator ? :
Java Language Specification
[3] Chapter 5.6.2 Binary Numeric Promotion
Java Language Specification
Java Tutorials - Autoboxing and Unboxing
http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Monday 15 December 2014

The Terrible Dangers of Autoboxing (Part 3)

Part 2 can be found here.


Please explain why the code throws a NullPointerException.

Output:
/opt/tools/java/jdk1.7.0_65/bin/java com.mrbear.autoboxing.Plot
0
3
3
Exception in thread "main" java.lang.NullPointerException
    at com.mrbear.autoboxing.Plot.getSurfaceArea(Plot.java:28)
    at com.mrbear.autoboxing.Plot.main(Plot.java:39)


Process finished with exit code 1
Also, please mention how to fix it.

((Solution))

Wednesday 3 December 2014

REST + Angular = Restangular

JQuery and REST

I have already mentioned calling REST services using JQuery1 in my entry on REST and Error messages at my blog.

At the risk of being redundant, here it is:

Unfortunately, it requires a lot of settings and a lot of DOM manipulation to create the view and make it work as it should.

I was looking for an alternative, something that's easy to use without a lot of boilerplate.

I would prefer something like JSF, but solely on the client side, and which fully supports REST. I found something to my liking in Angular.

Angular

AngularJS is, according to their website2, a Superheroic JavaScript MVW Framework.
“AngularJS is what HTML would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVW, MVVM, MVC, dependency injection and great testability story all implemented with pure client-side JavaScript!”
As far as I can tell, you add Angular specific xml attributes to your html tags, and Angular takes care of the updating of those tags, depending on what's changed. Of course, that's just the tip of the proverbial iceberg.

Angular and REST

The Angular version of the JQuery $.ajax call is called $http3, and is about what you would expect.
Both in JQuery and in Angular, it is a wrapper around the XMLHttpRequest.

This is not really a REST-aware API, to say the least. Therefore Angular also provides a $resource service. This service creates resource objects that makes it easy to communicate with RESTful backends.

It is an optional module in Angular, called ngResource, and needs to be installed.

Restangular and REST

However, I have opted to make use of Restangular4. It seems to be more self-evident how it works, follows the guideline of Angular more closely and seems to abstract away a lot of the boilerplate, for example URLs.

In the example above there are numerous interesting tidbits:
  • In the config call, I set the base url. All consequent calls to REST resources have this url as root.
  • In the config call, I needed to set the "id" of the resource accessed, otherwise Restangular assumes that the "id" is a field called "id". In this case it was "name"5 6.
  • var restBase = Restangular.all("worldattributes") sets a resource (in this case for world attributes).
  • restBase.getList()
            .then(function(worldattributes) 
            {
              // returns a list of worldattributes
              $scope.worldattributes = worldattributes;
              $scope.setFilter();
              $scope.isNew = false;
            });
    This creates a GET XMLHttpRequest for url "/karchangame/resources/administration/worldattributes" and retrieves all objects. It assumes JSON. As you see, I promptly put all info into the Angular scope, so it can be reached in the HTML file (see below).
  • The objects returned by Restangular, are themselves also REST resources, therefore contain methods such as put() which updates it (causing an automatic HTTP PUT request).
  • The call remove is translated to a DELETE XMLHttpRequest.
  • And my update method decides on a POST or PUT depending on wether it is a new or a changed world attribute.
  • The Restangular.copy call is convenient, as it makes a deep copy of the object.
  • Sometimes, the backend REST urls are not standard, in which case you wish to turn from the default, and start making calls with a bit more control. An example of such is visible with the customDELETE in the disown function.
The HTML part of the Angular looks like follows:

Notice that the HTML file looks almost exactly as a simple HTML file would, besides the added Angular attributes and the references to the $scope object (by means of {{ and }}).

References

[1] JQuery.ajax() | JQuery API Documentation
http://api.jquery.com/jquery.ajax/
[2] AngularJS - Main Website
https://angularjs.org/
[3] ngbook - The Complete Book on AngularJS
Ari Lerner
[4] Github - Restangular
https://github.com/mgonto/restangular
[5] Stackoverflow - Restangular put loses part of the url
http://stackoverflow.com/questions/24262949/restangular-put-loses-part-of-the-url
[6] Restangular - GitHub : What if each of my models has a different ID name like CustomerID for Customer?
https://github.com/mgonto/restangular#what-if-each-of-my-models-has-a-different-id-name-like-customerid-for-customer
Foundation 5
http://foundation.zurb.com/