JHarvester

[Work in progress] A handy little app I’m making for myself that makes it easy to track textual information on any given webpage. It uses the page’s URL and the text element’s XPath to perform a harvest using the external htmlunit library. Lets me keep track of things like the price of an item I’m interested in, or a sports score, all without needing to go to the page.

Can handle pages that have AJAX; however, does not support pages that require authentication or pagination.

The code was developed in a test-driven way and employs the MVC design pattern.

Uses the following external libraries:

  • htmlunit by Gargoyle Software, Inc. licensed under Apache License, Version 2.0.
  • GSON by Google, Inc. licensed under Apache License, Version 2.0.

Full code can be found GitHub.

Update: 24 April, 2017

Added a background thread that periodically updates all the entries at their required update frequency.

 

Java 8 – method references

Remember, lambda expressions are anonymous methods and they remove a lot of ceremonial code around the main instruction that the code is conveying.

Now consider the case where a lambda expression is simply calling a method that already exists, and nothing more. In that case, we can simply things even further.

If, say, you have a class SomeClass that has a method someMethod(int a, int b) then the lambda expression

(a, b) -> SomeClass.someMethod(a, b)

can be written using a method reference as just

SomeClass::someMethod

Again, don’t confuse unfamiliar with more complicated. With practice you’ll see when lambda expressions and method references get the point of your code across better than the old way of doing things would.

There’s a little bit more to it, though. There are 4 different usages of method references.

Kind Example
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of a particular object containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor ClassName::new

If you’d like to see more examples of method references check out the official docs here.

Java 8 – lambda expressions

java8

Lambda expression were introduced in Java 8.

Here’s a simple example that illustrates how to use lambda expressions, and how they make your code simpler and more expressive (not to mention background optimizations by the compiler).

Consider this code that iterates over a list to print out its elements.

List<Integer> values = Arrays.asList(1,2,3,4);
for (int i = 0; i < values.size(); i++){
 	System.out.println(values.get(i));
}

What we’re really trying trying to do is print out each element of the array but we’ve had to write a whole bunch of ceremonial code around the that main task of interest to us. This may not bug you much but that’s because it’s familiar. Lambda expressions are (at present) less familiar. But they are simpler. And simpler code, is more readable code. It communicates the essence of what it’s trying to do more readily. Java 8 introduces two new things – internal iteration with forEach(...) on collections and lambda expressions – that can turn the big messy for loop above into something much more to the point.

values.forEach((Integer value) -> System.out.println(value));

or the following, since Java knows than each element in a collection of `Integer`s is going to be an Integer.

values.forEach(value -> System.out.println(value));

Remember how lambda expressions are essentially anonymous methods? In the code above

  • the bit before the -> are the arguments to the method ((Integer value));
  • after the -> comes the body of the method; and
  • the return type is implied based on the method to which this lambda expression is being passed. For example, a forEach(...) call understandably doesn’t need a return type (i.e. void), it just wants to know what to do for each element of the collection instance it’s invoked on.

Based on this great talk by Venkat Subramaniam at SpringOne2GX 2014