简体   繁体   English

如何从apache.commons使用Factory?

[英]How to use Factory from apache.commons?

What are the uses of the java code in: org.apache.commons.collections15.Factory java代码在org.apache.commons.collections15.Factory中的用途是什么

  1. Is there documentation (I cannot find anything useful) 是否有文档(我找不到有用的东西)
  2. How do I use this to instantiate objects of type: Factory<Integer> , Factory<String> in the constructor of the BarabasiAlbertGenerator in the Java Jung graph package? 在Java Jung图形包的BarabasiAlbertGenerator的构造函数中,如何使用它实例化类型为Object的对象: Factory<Integer>Factory<String>
  3. How can I get a properly functioning BarabasiAlbertGenerator. 如何获得功能正常的BarabasiAlbertGenerator。

This is the code I have, and it only outputs a single vertex. 这是我的代码,它仅输出一个顶点。

    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();

I think my issue has something to do with my vertexFactory and edgeFactory. 我认为我的问题与我的vertexFactory和edgeFactory有关。 To me it seems like my vertexFactory can only create vertices with the value 12, and my edgeFactory can only create edges with the value 1. Therefore, the graph will only have 1 vertice with value 12. Is this reasoning accurate? 在我看来,我的vertexFactory只能创建值为12的顶点,而我的edgeFactory只能创建值为1的边。因此,该图形将只有1个值为12的顶点。这是否正确?

You are making this much, much too complicated. 您正在做的事情太复杂了。

Factory is just an interface for classes that generate objects. Factory只是用于生成对象的类的接口。 It is trivial to implement. 实施起来很简单。

You don't need InstantiationFactory. 您不需要InstantiationFactory。 Just write your own. 只需自己编写即可。 For example: 例如:

        Factory<Integer> vertexFactory = 
            new Factory<Integer>() {
                int count;
                public Integer create() {
                    return count++;
            }};

Successive calls to vertexFactory.create() generate a series of Integer objects in increasing order, starting with 0. 连续调用vertexFactory.create()以从0开始的vertexFactory.create()生成一系列Integer对象。

The specific nature of the Factory that you want will depend on what properties (if any) you want the vertex objects to have, but you may not actually care. 您想要的Factory的特定性质将取决于您希望顶点对象具有哪些属性(如果有),但是您实际上可能并不在意。 If you do, and you have (say) a List of objects that you want to use for vertices, then your Factory instance can use that list. 如果这样做,并且您拥有(例如)要用于顶点的对象List ,则Factory实例可以使用该列表。

Any of the JUNG examples that generate graphs ad hoc, or that use graph generators (rather than a static saved graph) will use Factory instances. 任何临时生成图或使用图生成器(而不是静态保存的图)的JUNG示例都将使用Factory实例。 They're everywhere. 他们无处不在。

From the looks of it (ie the Javadoc ) it is an interface that defines a create method that creates a new instance: 从它的外观(即Javadoc )来看,它是一个接口,它定义了一个create新实例的create方法:

java.lang.Object create()

Create a new object. 创建一个新对象。

Returns: a new object 返回:一个新对象


How do I use this to instantiate objects of type: Factory<Integer> , Factory<String> ? 如何使用它实例化类型为Object的对象: Factory<Integer>Factory<String>

Actually, you'd use the Factory<Integer> to instantiate an Integer (not another Factory). 实际上,您将使用Factory<Integer>实例化一个Integer(而不是另一个Factory)。

For example 例如

Factory<Integer> factory = ConstantFactory.getInstance(123);
Integer oneTwoThree = factory.create(); // will give you the Integer "123"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoClassDefFoundError 与 SwallowedExceptionListener 在 apache.commons - NoClassDefFoundError with SwallowedExceptionListener in apache.commons 如何使用来自 apache.commons 的 CSVParser 以任何顺序读取 CSV 列 - How to read in CSV columns in any order using CSVParser from apache.commons 如何使用apache.commons获取属性列表 - How to get list of properties using apache.commons 在apache.commons中将域添加到UrlValidator - Adding domain to UrlValidator in apache.commons FTPClient(apache.commons)没有上传或删除我的文件 - FTPClient (apache.commons) isn't uploading or deleting my files 在不使用apache.commons函数的情况下更新hashMap中的条目 - update an entry in hashMap without using apache.commons functions 如何从Apache Commons Math使用PolynomialSplineFunction - How to use PolynomialSplineFunction from Apache Commons Math 如何使用Apache Commons下载 - How to use apache commons download 如何正确使用 Java 中 Apache 公共数学库中的 ZipfDistribution? - How to use correctly ZipfDistribution from Apache commons math library in Java? 将 class 中的字段设置为同一 class 的新实例会导致 memory 溢出吗? InetValidator class (apache.commons) - Would setting a field in a class as a new instance of the same class cause memory overflow? InetValidator class (apache.commons)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM