简体   繁体   中英

How to use custom types in Java generics

I have the below interfaces and classes for Hypergraph implementations from JUNG library. I extended interface Hypergraph to create interface ISimpleHypergraph to include few new methods and then create a new class SimpleHypergraph by extending class SetHypergraph and implementing ISimpleHypergraph.

I also created custom SimpleV and SimpleH types which have id and weight fields. Now how can I implement few methods in SimpleHypergraph utilising the id fields? Inside SimpleHypergraph SimpleV and SimpleH are not recognizable. Any suggestions or even better way to do this?

Note that, interface Hypergraph and class SetHypergraph are part of the JUNG libray.


public interface Hypergraph<V, H> {
    // Other definitions
}

public interface ISimpleHypergraph<V, H> extends Hypergraph<V, H> {
    H get(int id);
    H get(Set<V> vSet);

    // Other definitions
}

public class SetHypergraph<V, H> implements Hypergraph<V, H> {
    protected Map<H, Set<V>> edges;
    // Other fields

    public SetHypergraph() {
        edges = new HashMap<H, Set<V>>();        
    }

    // Other methods
}

public class SimpleHypergraph<V, H> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H> {

    public H get(int id) {
        // How to use SimpleH.id and SimpleV.id here to get the 
        // searched Key entry from the Map<H, Set<V>> edges
    }

    public H get(Set<V> vSet) {
        // How to use SimpleH.id and SimpleV.id here to get the 
        // searched Key entry from the Map<H, Set<V>> edges
    }
}

public class SimpleV {

    public int id;
    public int weight;

    public SimpleV(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    // Other methods
}

public class SimpleH {

    public int id;
    public int weight;

    public SimpleH(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    // Other methods
}

Create an interface like:

public interface HasId {
    int getId()
}

and change declaration

public class SimpleHypergraph<V, H> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H>

to

public class SimpleHypergraph<V extends HasId, H extends HasId> extends SetHypergraph<V, H> implements ISimpleHypergraph<V, H>

Make SimpleV and SimpleH implement HasId interface

from now on you should be able get id inside SimpleHypergraph

public interface Hypergraph<V, H> {
    // Other definitions
}

...

public class SetHypergraph<V, H> implements Hypergraph<V, H> {
    protected Map<H, Set<V>> edges;
    // Other fields

    public SetHypergraph() {
        edges = new HashMap<H, Set<V>>();        
    }

    // Other methods
}

...

public interface SimpleHypergraph<V extends SimpleV, H extends SimpleH> extends Hypergraph<V, H> {
    H get(int id);
    H get(Set<V> vSet);
}


...

public class SimpleHypergraphImpl<V extends SimpleV, H extends SimpleH> extends SetHypergraph<V, H> implements SimpleHypergraph<V, H> {

    public H get(int id) {
        // your code
        return null;
    }

    public H get(Set<V> vSet) {
        // your code (V is at least SimpleV, so you can use its accessible properties/methods here
        return null;
    }

    // example of usage
    public static void main(String[] args) {
        SimpleHypergraph<SimpleV, SimpleH> simpleHyperGraph = new SimpleHypergraphImpl<SimpleV, SimpleH>();
        Set<SimpleV> set = new HashSet<SimpleV>();
        set.add(new SimpleV(1,1));
        set.add(new ComplicatedV(1000,1000));
        SimpleH h = simpleHyperGraph.get(0);
        h = simpleHyperGraph.get(set);
    }
}


...

public class SimpleV {

    private int id;
    private int weight;

    public SimpleV(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    // Other methods
}

...

public class SimpleH {

    private int id;
    private int weight;

    public SimpleH(int id, int weight) {
        this.id = id;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    // Other methods
}

public class ComplicatedV extends SimpleV {

    public ComplicatedV(int id, int weight) {
        super(id, weight);
    }
}

Avoid using public / protected access modifiers for class properties. Use getters and setters instead.

You may make your interface SimpleHypergraph generic but I think it's redundant in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM