Re: [jgrapht-users] getEdgeWeight in weighted Graph
Brought to you by:
barak_naveh,
perfecthash
From: Joris K. <de...@gm...> - 2014-03-23 21:35:11
|
Hi John, Thanks for your effort! A NullPointerException if perfectly fine and indeed a better choice than the UnsupportedArgumentException. br, Joris On Fri, Mar 21, 2014 at 6:14 PM, John Sichi <js...@gm...> wrote: > I'll make the change (but I think for consistency with existing code, > I'll use NullPointerException instead of > UnsupportedArgumentException). > > On Fri, Mar 21, 2014 at 1:02 PM, Alex Moir > <am...@di...> wrote: > > Good catch, I agree. > > > > > > > > From: Javier MV [mailto:jav...@dc...] > > Sent: Friday, March 21, 2014 12:56 PM > > To: jgr...@li... > > Subject: Re: [jgrapht-users] getEdgeWeight in weighted Graph > > > > > > > > +1 > > > > > > > > On Fri, Mar 21, 2014 at 4:01 PM, Georg Hieronimus > > <geo...@go...> wrote: > > > > i strongly agree with your suggestion. > > > > i already had the problem by myself, but forgot to write a pull request > > afterwards .... > > > > It's quiet horrible to find bugs, if you dont know about the current > > behavior. > > > > > > > > On 21 March 2014 19:56, Joris Kinable <de...@gm...> wrote: > > > > Dear, > > > > > > > > In the latest version of jgrapht (and perhaps also in other versions), > the > > getEdgeWeight(E e) function is implemented as follows: > > > > > > > > public double getEdgeWeight(E e) > > > > { > > > > if (e instanceof DefaultWeightedEdge) { > > > > return ((DefaultWeightedEdge) e).getWeight(); > > > > } else { > > > > return WeightedGraph.DEFAULT_EDGE_WEIGHT; > > > > } > > > > } > > > > > > > > > > > > If e==null, i.e. the edge does not exist, then this function will return > > DEFAULT_EDGE_WEIGHT. This is strange as often invoking the getEdgeWeight > > function on a non-existing edge is simply a bug. > > > > > > > > I would suggest to modify the above function to something like: > > > > public double getEdgeWeight(E e) > > > > { > > > > if (e instanceof DefaultWeightedEdge) { > > > > return ((DefaultWeightedEdge) e).getWeight(); > > > > } else if(e==null){ > > > > throw new UnsupportedArgumentException("Edge cannot be null"); > > > > }else{ > > > > return WeightedGraph.DEFAULT_EDGE_WEIGHT; > > > > } > > > > } > > > > > > > > One common error would be something like: > > > > Edge e=graph.getEdge(i,j); //Edge (i,j) is not part of the graph, so > > e==null; > > > > cost += graph.getEdgeWeight(e); //Correct, does not invoke an exception > > > > > > > > > > > > At the very least, the javadoc for the getEdgeWeight function should > mention > > the above pitfall by specify that invoking the getEdgeWeight function on > > null returns WeightedGraph.DEFAULT_EDGE_WEIGHT so it's the user's > problem to > > check whether the edge is not equal to null. > > > > > > > > This is just a suggestion. If there is support for this change, I'll > make a > > change request. > > > > > > > > br, > > > > > > > > Joris > > > > > > > > > ------------------------------------------------------------------------------ > > Learn Graph Databases - Download FREE O'Reilly Book > > "Graph Databases" is the definitive new guide to graph databases and > their > > applications. Written by three acclaimed leaders in the field, > > this first edition is now available. Download your free book today! > > http://p.sf.net/sfu/13534_NeoTech > > _______________________________________________ > > jgrapht-users mailing list > > jgr...@li... > > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > > > > > > > > > > > ------------------------------------------------------------------------------ > > Learn Graph Databases - Download FREE O'Reilly Book > > "Graph Databases" is the definitive new guide to graph databases and > their > > applications. Written by three acclaimed leaders in the field, > > this first edition is now available. Download your free book today! > > http://p.sf.net/sfu/13534_NeoTech > > _______________________________________________ > > jgrapht-users mailing list > > jgr...@li... > > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > > > > > > > > > > -- > > Javier MV > > > > > > > ------------------------------------------------------------------------------ > > Learn Graph Databases - Download FREE O'Reilly Book > > "Graph Databases" is the definitive new guide to graph databases and > their > > applications. Written by three acclaimed leaders in the field, > > this first edition is now available. Download your free book today! > > http://p.sf.net/sfu/13534_NeoTech > > _______________________________________________ > > jgrapht-users mailing list > > jgr...@li... > > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > > > > > ------------------------------------------------------------------------------ > Learn Graph Databases - Download FREE O'Reilly Book > "Graph Databases" is the definitive new guide to graph databases and their > applications. Written by three acclaimed leaders in the field, > this first edition is now available. Download your free book today! > http://p.sf.net/sfu/13534_NeoTech > _______________________________________________ > jgrapht-users mailing list > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > |