CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

How to use Factory from apache.commons?

What are the uses of the java code in: 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?
  3. How can I get a properly functioning 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. 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?

Upvotes: 0

Views: 1761

Answers (2)

Joshua O&#39;Madadhain
Joshua O&#39;Madadhain

Reputation: 2704

You are making this much, much too complicated.

Factory is just an interface for classes that generate objects. It is trivial to implement.

You don't need 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.

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. 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.

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. They're everywhere.

Upvotes: 2

Thilo
Thilo

Reputation: 262714

From the looks of it (i.e. the Javadoc) it is an interface that defines a create method that creates a new instance:

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> ?

Actually, you'd use the Factory<Integer> to instantiate an Integer (not another Factory).

For example

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

Upvotes: 1

Related Questions