5

I have a main class -- Simulator -- that uses two other classes -- Producer and Evaluator. Producer produces results, while the Evaluator evaluates those results. The Simulator controls the flow of execution by querying the Producer and then conveying the results to the Evaluator.

The actual implementation of the Producer and Evaluator are known at run-time, at compile time I only know their interfaces. Below I paste the contents of interfaces, example implementations and the Simulator class.

Old code

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Producers produce results. I do not care what is their type, but the values
 * in the map have to be comparable amongst themselves.
 */
interface IProducer {
    public Map<Integer, Comparable> getResults();
}

/**
 * This implementation ranks items in the map by using Strings.
 */
class ProducerA implements IProducer {
    @Override
    public Map<Integer, Comparable> getResults() {
        Map<Integer, Comparable> result = new HashMap<Integer, Comparable>();
        result.put(1, "A");
        result.put(2, "B");
        result.put(3, "B");
        return result;
    }
}

/**
 * This implementation ranks items in the map by using integers.
 */
class ProducerB implements IProducer {
    @Override
    public Map<Integer, Comparable> getResults() {
        Map<Integer, Comparable> result = new HashMap<Integer, Comparable>();
        result.put(1, 10);
        result.put(2, 30);
        result.put(3, 30);

        return result;
    }
}

/**
 * Evaluator evaluates the results against the given groundTruth. All it needs
 * to know about results, is that they are comparable amongst themselves.
 */
interface IEvaluator {
    public double evaluate(Map<Integer, Comparable> results,
            Map<Integer, Double> groundTruth);
}

/**
 * This is example of an evaluator (a metric) -- Kendall's Tau B.
 */
class KendallTauB implements IEvaluator {
    @Override
    public double evaluate(Map<Integer, Comparable> results,
            Map<Integer, Double> groundTruth) {

        int concordant = 0, discordant = 0, tiedRanks = 0, tiedCapabilities = 0;

        for (Entry<Integer, Comparable> rank1 : results.entrySet()) {
            for (Entry<Integer, Comparable> rank2 : results.entrySet()) {
                if (rank1.getKey() < rank2.getKey()) {
                    final Comparable r1 = rank1.getValue();
                    final Comparable r2 = rank2.getValue();
                    final Double c1 = groundTruth.get(rank1.getKey());
                    final Double c2 = groundTruth.get(rank2.getKey());

                    final int rankDiff = r1.compareTo(r2);
                    final int capDiff = c1.compareTo(c2);

                    if (rankDiff * capDiff > 0) {
                        concordant++;
                    } else if (rankDiff * capDiff < 0) {
                        discordant++;
                    } else {
                        if (rankDiff == 0)
                            tiedRanks++;

                        if (capDiff == 0)
                            tiedCapabilities++;
                    }
                }
            }
        }

        final double n = results.size() * (results.size() - 1d) / 2d;

        return (concordant - discordant)
                / Math.sqrt((n - tiedRanks) * (n - tiedCapabilities));
    }
}

/**
 * The simulator class that queries the producer and them conveys results to the
 * evaluator.
 */
public class Simulator {
    public static void main(String[] args) {
        Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
        groundTruth.put(1, 1d);
        groundTruth.put(2, 2d);
        groundTruth.put(3, 3d);

        List<IProducer> producerImplementations = lookUpProducers();
        List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

        IProducer producer = producerImplementations.get(1); // pick a producer
        IEvaluator evaluator = evaluatorImplementations.get(0); // pick an evaluator
        // Notice that this class should NOT know what actually comes from
        // producers (besides that is comparable)
        Map<Integer, Comparable> results = producer.getResults();
        double score = evaluator.evaluate(results, groundTruth);

        System.out.printf("Score is %.2f\n", score);
    }

    // Methods below are for demonstration purposes only. I'm actually using
    // ServiceLoader.load(Clazz) to dynamically discover and load classes that
    // implement these interfaces

    public static List<IProducer> lookUpProducers() {
        List<IProducer> producers = new ArrayList<IProducer>();
        producers.add(new ProducerA());
        producers.add(new ProducerB());

        return producers;
    }

    public static List<IEvaluator> lookUpEvaluators() {
        List<IEvaluator> evaluators = new ArrayList<IEvaluator>();
        evaluators.add(new KendallTauB());

        return evaluators;
    }
}

This code should compile and run. You should get the same result (0.82) regardless which producer implementation you select.

Compiler warns me about not using generics in several places:

  • In Simulator class, in interfaces IEvaluator and IProducer, and in classes that implement the IProducer interface, I get the following warning, whenever I reference the Comparable interface: Comparable is a raw type. References to generic type Comparable should be parameterized
  • In classes that implement IEvaluator I get the following warning (when calling compareTo() on the values of Map): Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable should be parameterized

All that said, the Simulator works. Now, I'd like to get rid of compile warnings. The problem is, that I have no idea, how to parametrize the interfaces IEvaluator and IProducer, and how to change the implementations of IProducer and IEvaluator.

I have some limitations:

  • I cannot know the type of values in the Map that the producer will return. But I know, that they will all be of the same type and that they will implement the Comparable interface.
  • Similarly, the IEvaluator instance does not need to know anything about results that is evaluating, except that they are of the same type and that they are comparable (IEvaluator implemetations need to be able to call the compareTo() method.).
  • I have to to keep the Simulator class out of this "Comparable" dilemma -- it does not need to know anything about those types (besides being of the same type, which is also comparable). Its job is to simply convey results from Producer to Evaluator.

Any ideas?

Edited and revised version

Using some ideas from answers below, I got to this stage, which compiles and runs without warnings and with no need to use SuppressWarnings directive. This is very similar to what Eero suggested, but the main method is a bit different.

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Producers produce results. I do not care what is their type, but the values
 * in the map have to be comparable amongst themselves.
 */
interface IProducer<T extends Comparable<T>> {
    public Map<Integer, T> getResults();
}

/**
 * This implementation ranks items in the map by using Strings.
 */
class ProducerA implements IProducer<String> {
    @Override
    public Map<Integer, String> getResults() {
        Map<Integer, String> result = new HashMap<Integer, String>();
        result.put(1, "A");
        result.put(2, "B");
        result.put(3, "B");

        return result;
    }
}

/**
 * This implementation ranks items in the map by using integers.
 */
class ProducerB implements IProducer<Integer> {
    @Override
    public Map<Integer, Integer> getResults() {
        Map<Integer, Integer> result = new HashMap<Integer, Integer>();
        result.put(1, 10);
        result.put(2, 30);
        result.put(3, 30);

        return result;
    }
}

/**
 * Evaluator evaluates the results against the given groundTruth. All it needs
 * to know about results, is that they are comparable amongst themselves.
 */
interface IEvaluator {
    public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
            Map<Integer, Double> groundTruth);
}

/**
 * This is example of an evaluator (a metric) -- Kendall's Tau B.
 */
class KendallTauB implements IEvaluator {
    @Override
    public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
            Map<Integer, Double> groundTruth) {
        int concordant = 0, discordant = 0, tiedRanks = 0, tiedCapabilities = 0;

        for (Entry<Integer, T> rank1 : results.entrySet()) {
            for (Entry<Integer, T> rank2 : results.entrySet()) {
                if (rank1.getKey() < rank2.getKey()) {
                    final T r1 = rank1.getValue();
                    final T r2 = rank2.getValue();
                    final Double c1 = groundTruth.get(rank1.getKey());
                    final Double c2 = groundTruth.get(rank2.getKey());

                    final int rankDiff = r1.compareTo(r2);
                    final int capDiff = c1.compareTo(c2);

                    if (rankDiff * capDiff > 0) {
                        concordant++;
                    } else if (rankDiff * capDiff < 0) {
                        discordant++;
                    } else {
                        if (rankDiff == 0)
                            tiedRanks++;

                        if (capDiff == 0)
                            tiedCapabilities++;
                    }
                }
            }
        }

        final double n = results.size() * (results.size() - 1d) / 2d;

        return (concordant - discordant)
                / Math.sqrt((n - tiedRanks) * (n - tiedCapabilities));
    }
}

/**
 * The simulator class that queries the producer and them conveys results to the
 * evaluator.
 */
public class Main {
    public static void main(String[] args) {
        Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
        groundTruth.put(1, 1d);
        groundTruth.put(2, 2d);
        groundTruth.put(3, 3d);

        List<IProducer<?>> producerImplementations = lookUpProducers();
        List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

        IProducer<?> producer = producerImplementations.get(0);
        IEvaluator evaluator = evaluatorImplementations.get(0);

        // Notice that this class should NOT know what actually comes from
        // producers (besides that is comparable)
        double score = evaluator.evaluate(producer.getResults(), groundTruth);

        System.out.printf("Score is %.2f\n", score);
    }

    // Methods below are for demonstration purposes only. I'm actually using
    // ServiceLoader.load(Clazz) to dynamically discover and load classes that
    // implement these interfaces
    public static List<IProducer<?>> lookUpProducers() {
        List<IProducer<?>> producers = new ArrayList<IProducer<?>>();
        producers.add(new ProducerA());
        producers.add(new ProducerB());

        return producers;
    }

    public static List<IEvaluator> lookUpEvaluators() {
        List<IEvaluator> evaluators = new ArrayList<IEvaluator>();
        evaluators.add(new KendallTauB());

        return evaluators;
    }
}

The key difference seems to be in the main method, which currently looks like this.

    public static void main(String[] args) {
        Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
        groundTruth.put(1, 1d);
        groundTruth.put(2, 2d);
        groundTruth.put(3, 3d);

        List<IProducer<?>> producerImplementations = lookUpProducers();
        List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

        IProducer<?> producer = producerImplementations.get(0);
        IEvaluator evaluator = evaluatorImplementations.get(0);

        // Notice that this class should NOT know what actually comes from
        // producers (besides that is comparable)
        double score = evaluator.evaluate(producer.getResults(), groundTruth);

        System.out.printf("Score is %.2f\n", score);
    }

This works. However, If I change the code to this:

    public static void main(String[] args) {
        Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
        groundTruth.put(1, 1d);
        groundTruth.put(2, 2d);
        groundTruth.put(3, 3d);

        List<IProducer<?>> producerImplementations = lookUpProducers();
        List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

        IProducer<?> producer = producerImplementations.get(0);
        IEvaluator evaluator = evaluatorImplementations.get(0);

        // Notice that this class should NOT know what actually comes from
        // producers (besides that is comparable)

        // Lines below changed
        Map<Integer, ? extends Comparable<?>> ranks = producer.getResults();            
        double score = evaluator.evaluate(ranks, groundTruth);

        System.out.printf("Score is %.2f\n", score);
}

The darn thing won't even compile, saying: Bound mismatch: The generic method evaluate(Map, Map) of type IEvaluator is not applicable for the arguments (Map>, Map). The inferred type capture#3-of ? extends Comparable is not a valid substitute for the bounded parameter >

This is totally weird to me. The code works if I invoke the evaluator.evaluate(producer.getResults(), groundTruth). However, if I first call producer.getResults() method, and store it to a variable, and then invoke the evaluate method with that variable (evaluator.evaluate(ranks, groundTruth)), I get the compile error (regardless of that variable's type).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
David
  • 166
  • 8
  • What do you mean by "dynamically loaded"? Is one Simulator used to handle several different instances of IProducer throughout program execution? – Tim Bender Aug 30 '12 at 00:55
  • 1
    As an aside, please provide sample code that compiles, preferably when pasted into just a single file. It was a hassle to invent a Main class myself, and make the other classes non-public members of it; it will be easier for others to answer if they can just copy your code and start working on it. – amalloy Aug 30 '12 at 00:56
  • @TimBender, I edited the question and added a working example that compiles and runs. I commented in the code, what I mean by dynamical loading. – David Aug 30 '12 at 10:05
  • A related question http://stackoverflow.com/questions/12524661/populating-and-sorting-a-list-extends-t-s where Judge Mental stated `Constraints are only allowed where type parameters are declared`. Also, a blog entry http://jazzjuice.blogspot.fi/2012/01/uses-of-wildcard-types-in-java.html – Eero Aaltonen Sep 21 '12 at 07:45

3 Answers3

2

You need to specify what kinds of things the object is willing to compare itself to. Something like:

import java.util.Map;
import java.util.HashMap;

interface IProducer<T extends Comparable<? super T>> {
    public Map<Integer, T> getResults();
}

interface IEvaluator {
    public <T extends Comparable<? super T>> double evaluate(Map<Integer, T> results,
                                                             Map<Integer, Double> groundTruth);
}

public class Main {
  public static void main(String[] args) {
    IProducer<String> producer = null;
    IEvaluator evaluator = null;
    Map<Integer, String> results = producer.getResults();
    double score = evaluator.evaluate(results, new HashMap<Integer, Double>());
  }
}
amalloy
  • 89,153
  • 8
  • 140
  • 205
  • Thanks for your input, but this solution me to specify the Comparable sub-type in the Simulator class. This wont work, because I have no way knowing which type a specific producer implementation uses. – David Aug 30 '12 at 09:57
1

I've posted my answer below. Some notes:

  • The Producers obviously know their own type
  • The Evaluator doesn't know what it's dealing with until the method is called
  • The producerImplementations contains several different types, so you end up with one cast when you actually pick one of them.

Code below:

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Producers produce results. I do not care what their actual type is, but the
 * values in the map have to be comparable amongst themselves.
 */
interface IProducer<T extends Comparable<T>> {
    public Map<Integer, T> getResults();
}

/**
 * This example implementation ranks items in the map by using Strings.
 */
class ProducerA implements IProducer<String> {
    @Override
    public Map<Integer, String> getResults() {
        Map<Integer, String> result = new HashMap<Integer, String>();
        result.put(1, "A");
        result.put(2, "B");
        result.put(3, "B");

        return result;
    }
}

/**
 * This example implementation ranks items in the map by using integers.
 */
class ProducerB implements IProducer<Integer> {
    @Override
    public Map<Integer, Integer> getResults() {
        Map<Integer, Integer> result = new HashMap<Integer, Integer>();
        result.put(1, 10);
        result.put(2, 30);
        result.put(3, 30);

        return result;
    }
}

/**
 * Evaluator evaluates the results against the given groundTruth. All it needs
 * to know about results, is that they are comparable amongst themselves.
 */
interface IEvaluator {
    public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
            Map<Integer, Double> groundTruth);
}

/**
 * This is example of an evaluator, metric Kendall Tau-B. Don't bother with
 * semantics, all that matters is that I want to be able to call
 * r1.compareTo(r2) for every (r1, r2) that appear in Map<Integer, T> results.
 */
class KendallTauB implements IEvaluator {
    @Override
    public <T extends Comparable<T>> double evaluate(Map<Integer, T> results,
            Map<Integer, Double> groundTruth) {
        int concordant = 0, discordant = 0, tiedRanks = 0, tiedCapabilities = 0;

        for (Entry<Integer, T> rank1 : results.entrySet()) {
            for (Entry<Integer, T> rank2 : results.entrySet()) {
                if (rank1.getKey() < rank2.getKey()) {
                    final T r1 = rank1.getValue();
                    final T r2 = rank2.getValue();
                    final Double c1 = groundTruth.get(rank1.getKey());
                    final Double c2 = groundTruth.get(rank2.getKey());

                    final int ranksDiff = r1.compareTo(r2);
                    final int actualDiff = c1.compareTo(c2);

                    if (ranksDiff * actualDiff > 0) {
                        concordant++;
                    } else if (ranksDiff * actualDiff < 0) {
                        discordant++;
                    } else {
                        if (ranksDiff == 0)
                            tiedRanks++;

                        if (actualDiff == 0)
                            tiedCapabilities++;
                    }
                }
            }
        }

        final double n = results.size() * (results.size() - 1d) / 2d;

        return (concordant - discordant)
                / Math.sqrt((n - tiedRanks) * (n - tiedCapabilities));
    }
}

/**
 * The simulator class that queries the producer and them conveys results to the
 * evaluator.
 */
public class Simulator {
    public static void main(String[] args) {
        // example of a ground truth
        Map<Integer, Double> groundTruth = new HashMap<Integer, Double>();
        groundTruth.put(1, 1d);
        groundTruth.put(2, 2d);
        groundTruth.put(3, 3d);

        // dynamically load producers
        List<IProducer<?>> producerImplementations = lookUpProducers();

        // dynamically load evaluators
        List<IEvaluator> evaluatorImplementations = lookUpEvaluators();

        // pick a producer
        IProducer<?> producer = producerImplementations.get(0);

        // pick an evaluator
        IEvaluator evaluator = evaluatorImplementations.get(0);

        // evaluate the result against the ground truth
        double score = evaluator.evaluate(producer.getResults(), groundTruth);

        System.out.printf("Score is %.2f\n", score);
    }

    // Methods below are for demonstration purposes only. I'm actually using
    // ServiceLoader.load(Clazz) to dynamically discover and load classes that
    // implement interfaces IProducer and IEvaluator
    public static List<IProducer<?>> lookUpProducers() {
        List<IProducer<?>> producers = new ArrayList<IProducer<?>>();
        producers.add(new ProducerA());
        producers.add(new ProducerB());

        return producers;
    }

    public static List<IEvaluator> lookUpEvaluators() {
        List<IEvaluator> evaluators = new ArrayList<IEvaluator>();
        evaluators.add(new KendallTauB());

        return evaluators;
    }
}
David
  • 166
  • 8
Eero Aaltonen
  • 4,239
  • 1
  • 29
  • 41
  • Thanks Eero, but this still needs one SuppressWarning directive, which is why I think we are almost there. Check my edited question above. – David Aug 31 '12 at 11:01
  • @David I see 'edited yesterday'. What edits are you referring to? – Eero Aaltonen Aug 31 '12 at 11:07
  • I'm currently working on it, give me a couple of more minutes ;) – David Aug 31 '12 at 11:09
  • @David it seems that in principle the cast could be replaced by a wildcard capture. However I can't get it to work. I see others have had problems as well http://rgrig.blogspot.fi/2009/07/type-inference-in-java-for-generics.html. Does it really matter though? – Eero Aaltonen Aug 31 '12 at 11:59
  • And if you want dynamic loading and type inference, you could just use jython :) – Eero Aaltonen Aug 31 '12 at 12:05
  • Since this was the most useful response, I'll modify it to contain the working code and then flag it as the answer. However, I still do not know why there is no way to define a type of variable that will be able to hold the result of producer.getResults(). It seems as there exist a function, whose return type cannot be expressed. – David Sep 03 '12 at 22:31
  • @David: One last comment. You can call and store `producer.getResults()` in a private static submethod, and it will work fine. I do no know why the compiler chokes on it in the main method. – Eero Aaltonen Sep 04 '12 at 07:15
0

Are the warnings simply asking you to do something like this?

public interface IProducer<T extends Comparable<? super T>> {
    public Map<Integer, T> getResults();
}

Whenever I implement Comparable (or extend Comparator) I always do something like this:

public class Dog implements Comparable<Dog> {

    private String breed;

    public String getBreed() {
        return breed;
    }

    public void setBreed(String s) {
        breed = s;
    }

    public int compareTo(Dog d) {
        return breed.compareTo(d.getBreed());
    }

}

Note that there's no need to use an object in compareTo when Comparable is parameterized.

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • This doesn't seem right -- more likely `public interface IProducer> { public Map getResults(); }`. The way you've written it, the _caller_ could ask for whatever `Comparable` type -- even if the `IProducer` has never heard of that type. – Louis Wasserman Aug 30 '12 at 00:46
  • `>` isn't right either (though it's better). See my answer, and also the javadocs for something like http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29 - you need to support, eg, an Integer that is Comparable, not just Comparable. – amalloy Aug 30 '12 at 00:52
  • Thanks guys! That's why I wrote something _like_ this... I knew my syntax would be off a bit. My main interest was showing the second half of my answer as an example for the OP. – jahroy Aug 30 '12 at 00:53