Java 如何参数化可比接口?
我有一个主类--模拟器,它使用另外两个类--生产者和评价者。生产者产生结果,而评价者评估这些结果。模拟器通过查询生产者然后将结果传递给评估者来控制执行流程 Producer和Evaluator的实际实现在运行时已知,在编译时我只知道它们的接口。下面我将粘贴接口、示例实现和模拟器类的内容 旧代码Java 如何参数化可比接口?,java,generics,interface,wildcard,Java,Generics,Interface,Wildcard,我有一个主类--模拟器,它使用另外两个类--生产者和评价者。生产者产生结果,而评价者评估这些结果。模拟器通过查询生产者然后将结果传递给评估者来控制执行流程 Producer和Evaluator的实际实现在运行时已知,在编译时我只知道它们的接口。下面我将粘贴接口、示例实现和模拟器类的内容 旧代码 package.com.test; 导入java.util.ArrayList; 导入java.util.HashMap; 导入java.util.List; 导入java.util.Map; 导入jav
package.com.test;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
/**
*生产者产生结果。我不在乎它们是什么类型,而是它们的值
*在地图上,它们之间必须具有可比性。
*/
接口生产者{
公共映射getResults();
}
/**
*此实现使用字符串对地图中的项目进行排序。
*/
类ProducerA实现IProducer{
@凌驾
公共地图getResults(){
映射结果=新的HashMap();
结果.付诸表决(1,“A”);
结果.付诸表决(2,“B”);
结果.付诸表决(3,“B”);
返回结果;
}
}
/**
*此实现使用整数对映射中的项目进行排序。
*/
类ProducerB实现IProducer{
@凌驾
公共地图getResults(){
映射结果=新的HashMap();
结果:put(1,10);
结果:put(2,30);
结果:put(3,30);
返回结果;
}
}
/**
*评估者根据给定的事实对结果进行评估。它所需要的一切
*要了解结果,就要知道它们之间是可比较的。
*/
接口评估器{
公众双重评估(Map结果,
地图(真相);
}
/**
*这是一个评估器(一个度量)的例子——肯德尔的Tau B。
*/
类KendallTauB实现IEEvaluator{
@凌驾
公众双重评估(Map结果,
地图(真相){
int协调=0,不协调=0,tiedRanks=0,tiedCapabilities=0;
对于(条目rank1:results.entrySet()){
对于(条目rank2:results.entrySet()){
if(rank1.getKey()0){
协和++;
}else if(rankDiff*capDiff<0){
不协调++;
}否则{
如果(rankDiff==0)
tiedRanks++;
如果(capDiff==0)
tiedCapabilities++;
}
}
}
}
最终双n=results.size()*(results.size()-1d)/2d;
返回(一致-不一致)
/数学sqrt((n-tiedRanks)*(n-tiedCapabilities));
}
}
/**
*模拟器类查询生产者并将结果传递给
*评估员。
*/
公共类模拟器{
公共静态void main(字符串[]args){
Map groundTruth=newhashmap();
地面真相。放置(1,1d);
地面真相。放置(2,2d);
地面真相。放置(3,3d);
列出ProducerImplements=lookUpProducers();
List EvaluatorImplements=lookUpEvaluators();
IProducer producer=producerImplementations.get(1);//选择一个生产者
IEEvaluator evaluator=evaluatorImplementations.get(0);//选择一个计算器
//注意,这个类不应该知道实际来自什么
//生产商(除此之外,还具有可比性)
映射结果=producer.getResults();
双倍分数=评估者。评估(结果、事实真相);
System.out.printf(“分数为%.2f\n”,分数);
}
//下面的方法仅用于演示目的。我实际上正在使用
//load(Clazz)动态发现并加载
//实现这些接口
公共静态列表lookuproducers(){
列表生产者=新的ArrayList();
producer.add(新ProducerA());
producers.add(newproducerb());
回报生产者;
}
公共静态列表查找计算器(){
列表计算器=新的ArrayList();
添加(新的KendallTauB());
返回评估员;
}
}
这段代码应该编译并运行。无论选择哪个生产者实现,都应该得到相同的结果(0.82)
编译器在以下几个地方警告我不要使用泛型:
- 在Simulator类中,在接口IEEvaluator和IProducer中,以及在实现IProducer接口的类中,每当我引用可比接口时,我都会收到以下警告:Comparable是原始类型。对泛型类型Compariable的引用应参数化
- 在实现IEEvaluator的类中,我得到以下警告(在对Map的值调用compareTo()时):类型安全:方法compareTo(Object)属于原始类型Comparable。对泛型类型Compariable的引用应参数化
- 我无法知道制作者将返回的映射中的值的类型。但我知道,它们都将是相同的类型,并且它们将实现可比较的接口李>
- 同样,IEva
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; } }
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); }
public interface IProducer<T extends Comparable<? super T>> { public Map<Integer, T> getResults(); }
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()); } }
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>()); } }
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; } }