/** * Given an element in this list, check if the weight has changed (since the element was added to * the graph or was last validated, whichever is more recent), and if so, change the current * mapping of a weight to the element or remove the element from the set of unweighted elements. * * @param element The graph element. * @return True if the weight associated with the element has changed as determined by the equals * method. */ public boolean changeWeight(Element element) { boolean weightValueHasChanged = false; boolean found = false; Object newWeight = element.hasWeight() ? element.getWeight() : null; if (_unweightedSet.contains(element)) { weightValueHasChanged = (newWeight != null); if (weightValueHasChanged) { _unweightedSet.remove(element); registerWeight(element); } } else { // Find the weight that was previously associated with this // element, if there was one. Iterator weights = _weightMap.keySet().iterator(); Object nextWeight = null; List nextList = null; while (weights.hasNext() && !found) { nextWeight = weights.next(); nextList = (List) _weightMap.get(nextWeight); found = nextList.contains(element); } if (found) { // Note that the weight can change without the weight // comparison here changing (if the change does not affect // comparison under the equals method). weightValueHasChanged = !nextWeight.equals(newWeight); if (weightValueHasChanged) { nextList.remove(element); if (nextList.size() == 0) { _weightMap.remove(nextWeight); } registerWeight(element); } } else { // FIXME: use an internal error exception here. throw new RuntimeException( "Internal error: the specified " + _descriptor + " is neither unweighted nor associated " + "with a weight." + GraphException.elementDump(element, _graph)); } } return weightValueHasChanged; }
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { if (name.equals("graph")) { System.out.println("Creating graph " + attributes.getValue("name")); graphContener = new GraphContener(attributes.getValue("name")); graphContener.setFilePath(filePath); } else if (name.equals("v")) { try { graphContener.addVertex(attributes.getValue("id")); } catch (GraphException e) { e.printStackTrace(); } } else if (name.equals("e")) { try { graphContener.addEdge( graphContener.getVertex(attributes.getValue("from")), graphContener.getVertex(attributes.getValue("to"))); } catch (GraphException e) { e.printStackTrace(); } } else if (name.equals("coord")) { try { graphContener.setHasCoordinates(true); graphContener .getVertex(attributes.getValue("id")) .setX(Integer.parseInt(attributes.getValue("x"))); graphContener .getVertex(attributes.getValue("id")) .setY(Integer.parseInt(attributes.getValue("y"))); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("Unknown tag : " + name); } }