Skip to content
Advertisement

What’s the meaning of this line of code? And how can I create an object of this class?

I was trying to construct an object of the MTree class (https://github.com/Waikato/moa/blob/master/moa/src/main/java/moa/clusterers/outliers/utils/mtree/MTree.java)

The constructor of MTree looks like this:

JavaScript

The DistanceFunction here is an interface, the code of it is:

JavaScript

And it’s implementation is:

JavaScript

And my first question is what’s the meaning of return new DistanceFunction<Data>() in the method public static <Data> DistanceFunction<Data> cached(final DistanceFunction<Data> distanceFunction) [the method is in the class DistanceFunctions] I am just a beginner of Java and this one is a little bit hard for me to understand.

Also, to create an object of MTree, I should create an object of DistanceFunctions and an object of ComposedSplitFunction(Which is the implementation of SplitFunction interface) and input them as parameter for MTree constructor. But I really don’t know how to do that because in DistanceFunctions class, the constructor is private. So I cannot generate a parameter for the constructor of MTree. What should I do?

New Update: What I want to do is create a Junit Test for MTree, and I believe the first thing I need to do is create an object of MTree.

Advertisement

Answer

Interfaces can have multiple implementations. They just form the general contract implementations need to follow.

The cache implementation here i.e. takes a DistanceFunction as input and guarantees that distance values between A and B (or B and A) are only calculated once and thereafter served from the internal cache map. The generic type of that cache function just guarantees that you can literally pass any type to it. I.e. you could have an implementation that takes in its simplest form just two integers and calculates the difference of these like this:

JavaScript

which is a labmda expression which could be also written a bit more verbose like this

JavaScript

and then use it like that to cache the return value for the provided input parameters:

JavaScript

If you later on have a call like

JavaScript

again or even

JavaScript

The distance value in the above case is not recalculated but its value is returned from the internal cache map as the distance for these parameters was already calculated before. This is especially benefitial if you have plenty of data points but only a limited number of combination of these and calculation is rather expensive.

If you further look into the DistanceFunctions class you’ve provided you will see that it already provides some implementations for i.e. EUCLIDEAN, EUCLIDEAN_INTEGER_LIST and EUCLIDEAN_DOUBLE_LIST implementations which due to their static final nature can be used as constant in your code directly. Here you just need to provide matching input arguments to the calculate(...) method based on the implementation you’ve chosen.

In regards to Waikato’s MTree` initialization, a rough template may look like this:

JavaScript

where the parts outlined by ... need to be defined and implemented by you. The code in that package though provides i.e. a ComposedSplitFunction implementation already that requires PartitionFunction and PromotionFunction as inputs where implementations of these are already available in the PartitionFunctions and PromotionFunctions classes that just work the same way as the DistanceFunction and DistanceFunctions discussed here.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement