在Java中是否可以将这两个静态函数组合成C ++模板参数的样式?我已经尝试过在类中传递,但这只是给出了不知道类型参数是什么的错误。对不起,我已经google了一下,但我是Java的新手并且很难搞清楚这个。
import com.univocity.parsers.common.processor.BeanListProcessor;
import com.univocity.parsers.tsv.TsvParser;
import com.univocity.parsers.tsv.TsvParserSettings;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
public class Parsers {
public static List<ThingOne> ParseThingOne(String fName) throws FileNotFoundException
{
BeanListProcessor<ThingOne> rowProcessor = new BeanListProcessor<ThingOne>(ThingOne.class);
TsvParserSettings tsvSet = new TsvParserSettings();
tsvSet.setHeaderExtractionEnabled(true);
tsvSet.setProcessor(rowProcessor);
TsvParser p = new TsvParser(tsvSet);
FileReader f = new FileReader(fName);
p.parse(f);
return rowProcessor.getBeans();
}
public static List<ThingTwo> ParseThingTwo(String fName) throws FileNotFoundException
{
BeanListProcessor<ThingTwo> rowProcessor = new BeanListProcessor<ThingTwo>(ThingTwo.class);
TsvParserSettings tsvSet = new TsvParserSettings();
tsvSet.setHeaderExtractionEnabled(true);
tsvSet.setProcessor(rowProcessor);
TsvParser p = new TsvParser(tsvSet);
FileReader f = new FileReader(fName);
p.parse(f);
return rowProcessor.getBeans();
}
}
答案 0 :(得分:3)
不确定为什么你说通过课程不适合你。这应该有效:
List<ThingOne> l1 = parse(ThingOne.class, "file1");
List<ThingTwo> l2 = parse(ThingTwo.class, "file2");
public static <T> List<T> parse(Class<T> clazz, String fName) throws FileNotFoundException{
BeanListProcessor<T> rowProcessor = new BeanListProcessor<>(clazz);
TsvParserSettings tsvSet = new TsvParserSettings();
tsvSet.setHeaderExtractionEnabled(true);
tsvSet.setProcessor(rowProcessor);
TsvParser p = new TsvParser(tsvSet);
FileReader f = new FileReader(fName);
p.parse(f);
return rowProcessor.getBeans();
}
答案 1 :(得分:1)
如果您创建一个接口或类并使您的两个类继承/扩展它,您可以使用Java Generics。例如,如果您创建一个接口MyInterface
,那么:
ThingOne implements MyInterface
和ThingTwo implements MyInterface
。然后你可以写一个通用函数如下:
public static <T extends MyInterface> List<T> ParseThing(String fName, Class<T> clazz) throws FileNotFoundException
{
BeanListProcessor<T> rowProcessor = new BeanListProcessor<T>(clazz);
TsvParserSettings tsvSet = new TsvParserSettings();
tsvSet.setHeaderExtractionEnabled(true);
tsvSet.setProcessor(rowProcessor);
TsvParser p = new TsvParser(tsvSet);
FileReader f = new FileReader(fName);
p.parse(f);
return rowProcessor.getBeans();
}
答案 2 :(得分:1)
尝试这样的事情:
describe Fastlane do
describe Fastlane::FastFile do
describe "Delete keychain Integration" do
before :each do
allow(File).to receive(:file?).and_return(false)
end
it "works with keychain name found locally" do
allow(FastlaneCore::FastlaneFolder).to receive(:path).and_return(nil)
keychain = File.expand_path('test.keychain')
allow(File).to receive(:file?).and_return(false)
allow(File).to receive(:file?).with(keychain).and_return(true)
result = Fastlane::FastFile.new.parse("lane :test do
delete_keychain ({
name: 'test.keychain',
throw_error:false
})
end").runner.execute(:test)
expect(result).to eq("security delete-keychain #{keychain}")
end
通过类似的东西调用:
public static <T> List<T> parseThing( final String fName, final BeanListProcessor<T> rowProcessor )
throws FileNotFoundException
{
final TsvParserSettings tsvSet = new TsvParserSettings();
tsvSet.setHeaderExtractionEnabled(true);
tsvSet.setProcessor(rowProcessor);
final TsvParser p = new TsvParser(tsvSet);
final FileReader f = new FileReader(fName);
p.parse(f);
return rowProcessor.getBeans();
}