Skip to main content

Posts

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part III)

... This is typically done with the bind and install methods inherited from AbstractModule.  The install method is fairly straightforward, since it can only accept a parameter of type Module.  You’ll notice the bind method will take parameters of type Class, Key, or TypeLiteral.  The Key and TypeLiteral types are part of the Guice library.  
You can probably guess using the bind method takes more thought than just using the install method with a neatly packaged module.  To ease things, though, the bind method comes with a fairly intuitive Embedded Domain-Specific Language(EDSL).  We will give some examples, which will be helpful since developers typically use bind more often than install.  Both methods, as aforementioned, act on a Binder instance.
Before we get into some examples, let me just show you a simple design approach some developers have found useful.  That is to extend Guice’s AbstractModule with an Application-Specific abstract class that overrides the configure method with an…
Recent posts

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part II)

In the subtitle of this tutorial, we say we are “re-learning” Google Guice because, with the growing adoption of the Scala programming language, it is necessary to re-learn libraries like Guice so that we are able to use them to build not only Java-based but also Scala-based services.  Continuing our discussion, one thing we should point out is that, when we extend the AbstractModule abstract class, we do not override the configure method of Listing 1.1 but a configure method without parameters.  In fact, if we try to override the method from Listing 1.1, you’ll get a complaint from either your compiler or IDE.  No, this is not magic.  It may be worthwhile to take a look at the AbstractModule source to see why this is the case. 
Listing 1.2: AbstractModule abstract class …
public abstract class AbstractModule implements Module {
Binder binder;
public final synchronized void configure(Binder builder) { … }
protected abstract void configure();
protected Binder binder() { .. }
... As we see from Li…

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part I)

Google Guice is used as a lightweight dependency injection framework that further assists developers in modularizing their applications.  Google shared this very useful library with the development community in 2010 in between the Java SE 6 and Java SE 7 releases.  This library is used in some of Java’s (and now Scala’s) most prominent libraries and platforms such as the Simian Army platform shared by Netflix.
We will begin our discussion of Google Guice with its Module interface.  In the Guice developers’ own words, ‘A Guice-based application is ultimately composed of little more than a set of modules and some bootstrapping code.’  We will not be using this interface directly, but it gives us a very good context from which to start.  Instead, we will be extending the abstract class that implements it -- intuitively named AbstractModule.  
If you ever get a chance to look at the Module interface JavaDoc or source code, you’ll see a configure method taking a parameter of type Binder.  
Li…

#processing @Microsoft #office #Excel files with @TheASF POI (part II)

...
     Apache POI's OPCPackage abstract class represents a container that can store multiple data objects.  It is central to the processing of Excel(*.xlsx) files.  We only need to use its static open method to process an InputStream instance.  Further, we can "read" these Excel files via the XSSFWorkbook class.  This class is a high level representation of a SpreadsheetML workbook.  From an XSSFWorkbook, we can get any existing XSSFSheets within the workbook.  Then, we can further subdivide any XSSFSheet into rows and analyze the cell data within the rows.  In general, given certain assumptions in the format of the Excel document, we can extract data as text  from a cell and perform any number of business processes.

     In the Java function code excerpt below, we assume we have an Excel(*.xlsx) file represented as an InputStream.

        @Override
    public Iterator<Row> apply(InputStream inputStream) {

        try(OPCPackage pkg = OPCPackage.open(…

Implementing @ApacheIgnite's cache store (part II)

Apache Ignite’s CacheStore interface is an API for cache persistence storage for read-through and write-through behavior.  When implementing this interface, you choose the type of key and value object alike -- similar to a map.  This follows the pattern established by the CacheLoader and CacheWriter interfaces CacheStore extends of the JSR107 specification.  In many cases, having a specific implementation for each method when implementing this interface may not be necessary, so Apache Ignite has a CacheStoreAdapter for this purpose.
Since Caches so closely resemble Maps, perhaps we should begin our discussion with a cache implementation that is essentially a HashMap store:
public class HashMapStore extends CloudStoreAdapter {
private final Map<Object, Object> map = new HashMap<>();
@Override public void loadCache(IgniteBiInClosure c, Object … args) {
for(Map.Entry e : map.entrySet()) { c.apply(e.getKey(), e.getValues()); }
@Override public Object load(Object key) { Return map.get(k…

@Airbnb's Aerosolve API is a gift to the #ML community! (part II)

...   Airbnb’s Aerosolve #machinelearning API contains a number of Java classes representing standard mathematical models. These classes implement the API’s Model interface -- requiring them to implement the interface’s scoreItem and debugScoreItem methods.
  The purpose of the debugScoreItem method is to provide an explanation as to how the item was scored along with the score.
In order to score an item, a Thrift struct appropriately named FeatureVector is required as input.
If you’re curious, a Thrift struct is similar to a class in OOP minus inheritance.
As a Thrift struct, the FeatureVector has a very simplistic structure as is shown below:
struct FeatureVector {
  1: optional map<string, set<string>> stringFeatures;    2: optional map<string, map<string, double>> floatFeatures;   3: optional map<string, list<double>> denseFeatures; }
What this essentially says is a FeatureVector will have as its core one of these three structures. The key of e…