Как параметризовать сопоставимый интерфейс?
У меня есть основной класс -Simulator - который использует два других класса -Producer а такжеEvaluator, Производитель производит результаты, а оценщик оценивает эти результаты. Симулятор контролирует ход выполнения, опрашивая Производителя, а затем передавая результаты Оценщику.
Реальная реализация Producer и Evaluator известна во время выполнения, во время компиляции я знаю только их интерфейсы. Ниже я вставляю содержимое интерфейсов, примеров реализации и класса Simulator.
Old codepackage 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;
}
}
Этот код должен скомпилироваться и запустить. Вы должны получить один и тот же результат (0,82) независимо от того, какую реализацию производителя вы выберете.
Компилятор предупреждает меня о том, что не следует использовать дженерики в нескольких местах:
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Все что сказано, Симулятор работает. Теперь я хотел бы избавиться от предупреждений компиляции. Проблема в том, что я понятия не имею, как параметризовать интерфейсы IEvaluator и IProducer и как изменить реализации IProducer и IEvaluator.
У меня есть некоторые ограничения:
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.Есть идеи?
Edited and revised versionИспользуя некоторые идеи из ответов ниже, я дошел до этого этапа, который компилируется и запускается без предупреждений и без необходимости использовать директиву SuppressWarnings. Это очень похоже на то, что предложил Eero, но основной метод немного другой.
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;
}
}
Ключевое различие заключается в основном методе, который в настоящее время выглядит следующим образом.
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);
}
Это работает. Однако, если я изменю код на это:
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);
}
Чертова вещь даже не скомпилируется, говоря: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 >
Это совершенно странно для меня. Этот код работает, если я вызываю функцию calcator.evaluate (provider.getResults (), groundTruth). Тем не менее, если я сначала вызову метод provider.getResults () и сохраню его в переменной, а затем вызову метод оценки с этой переменной (valuator.evaluate (ranks, groundTruth)), я получу ошибку компиляции (независимо от этой переменной & apos) тип s).