[英]NoClassDefFoundError with SwallowedExceptionListener in apache.commons
[英]How to use Factory from apache.commons?
java代碼在org.apache.commons.collections15.Factory
中的用途是什么
Factory<Integer>
和Factory<String>
? 這是我的代碼,它僅輸出一個頂點。
Factory<Graph<String, Integer>> graphFactory = SparseGraph.getFactory();
Integer[] ints = {1};
String[] strs = {"12"};
Class[] typesStr = {String.class};
Class[] typesInt = {int.class};
Factory<String> vertexFactory = InstantiateFactory.getInstance(String.class, typesStr, strs);
Factory<Integer> edgeFactory = InstantiateFactory.getInstance(Integer.class, typesInt, ints);
HashSet<String> seedVertices = new HashSet();
for(int i = 0; i < 10; i++)
{
seedVertices.add("v"+i);
}
BarabasiAlbertGenerator<String, Integer> barabasiGen = new
BarabasiAlbertGenerator<String,Integer>(graphFactory, vertexFactory,
edgeFactory, seedVertices.size(), 1, seedVertices);
Graph g = barabasiGen.create();
我認為我的問題與我的vertexFactory和edgeFactory有關。 在我看來,我的vertexFactory只能創建值為12的頂點,而我的edgeFactory只能創建值為1的邊。因此,該圖形將只有1個值為12的頂點。這是否正確?
您正在做的事情太復雜了。
Factory只是用於生成對象的類的接口。 實施起來很簡單。
您不需要InstantiationFactory。 只需自己編寫即可。 例如:
Factory<Integer> vertexFactory =
new Factory<Integer>() {
int count;
public Integer create() {
return count++;
}};
連續調用vertexFactory.create()
以從0開始的vertexFactory.create()
生成一系列Integer
對象。
您想要的Factory的特定性質將取決於您希望頂點對象具有哪些屬性(如果有),但是您實際上可能並不在意。 如果這樣做,並且您擁有(例如)要用於頂點的對象List
,則Factory
實例可以使用該列表。
任何臨時生成圖或使用圖生成器(而不是靜態保存的圖)的JUNG示例都將使用Factory
實例。 他們無處不在。
從它的外觀(即Javadoc )來看,它是一個接口,它定義了一個create
新實例的create
方法:
java.lang.Object create()
創建一個新對象。
返回:一個新對象
如何使用它實例化類型為Object的對象:
Factory<Integer>
,Factory<String>
?
實際上,您將使用Factory<Integer>
實例化一個Integer(而不是另一個Factory)。
例如
Factory<Integer> factory = ConstantFactory.getInstance(123);
Integer oneTwoThree = factory.create(); // will give you the Integer "123"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.