pax_global_header00006660000000000000000000000064125363561300014516gustar00rootroot0000000000000052 comment=746623aa1ffa137b7c729e8328bdee6be1a6c892 dirgra-0.3/000077500000000000000000000000001253635613000126305ustar00rootroot00000000000000dirgra-0.3/.gitignore000066400000000000000000000000711253635613000146160ustar00rootroot00000000000000target /.settings/ /examples/.redcar/ /.redcar/ *~ .idea dirgra-0.3/LICENSE.txt000066400000000000000000000266421253635613000144650ustar00rootroot00000000000000 Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT’S ACCEPTANCE OF THIS AGREEMENT. 1. DEFINITIONS "Contribution" means: a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and b) in the case of each subsequent Contributor: i) changes to the Program, and ii) additions to the Program; where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor’s behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program. "Contributor" means any person or entity that distributes the Program. "Licensed Patents " mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. "Program" means the Contributions distributed in accordance with this Agreement. "Recipient" means anyone who receives the Program under this Agreement, including all Contributors. 2. GRANT OF RIGHTS a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient’s responsibility to acquire that license before distributing the Program. d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. 3. REQUIREMENTS A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: a) it complies with the terms and conditions of this Agreement; and b) its license agreement: i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. When the Program is made available in source code form: a) it must be made available under this Agreement; and b) a copy of this Agreement must be included with each copy of the Program. Contributors may not remove or alter any copyright notices contained within the Program. Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. 4. COMMERCIAL DISTRIBUTION Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor’s responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 5. NO WARRANTY EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 6. DISCLAIMER OF LIABILITY EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 7. GENERAL If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient’s patent(s), then such Recipient’s rights granted under Section 2(b) shall terminate as of the date such litigation is filed. All Recipient’s rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient’s rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient’s obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. dirgra-0.3/MANIFEST.MF000066400000000000000000000001211253635613000142540ustar00rootroot00000000000000Implementation-Title: Dirgra (Simple Directed Graph) Implementation-Version: 0.1 dirgra-0.3/README.md000066400000000000000000000001761253635613000141130ustar00rootroot00000000000000= Simple Directed Graph Implementation = Building ```text % mvn package ``` = Testing ```text % jruby -Itarget rspec ``` dirgra-0.3/dirgra.iml000066400000000000000000000015621253635613000146070ustar00rootroot00000000000000 dirgra-0.3/pom.xml000066400000000000000000000055611253635613000141540ustar00rootroot00000000000000 4.0.0 org.jruby dirgra jar 0.3 Dirgra https://github.com/jruby/dirgra Simple Directed Graph org.sonatype.oss oss-parent 7 JIRA http://jira.codehaus.org/browse/JRUBY scm:git:git://github.com/jruby/dirgra.git scm:git:git://github.com/jruby/dirgra.git http://github.com/jruby/dirgra EPL http://www.eclipse.org/legal/epl-v10.html enebo Thomas E. Enebo tom.enebo@gmail.com junit junit 4.10 test src test org.apache.maven.wagon wagon-webdav-jackrabbit 2.1 org.apache.maven.plugins maven-source-plugin attach-sources jar org.apache.maven.plugins maven-javadoc-plugin attach-javadocs jar maven-compiler-plugin 1.6 1.6 maven-jar-plugin MANIFEST.MF dirgra-0.3/spec/000077500000000000000000000000001253635613000135625ustar00rootroot00000000000000dirgra-0.3/spec/data_iterator_spec.rb000066400000000000000000000112361253635613000177460ustar00rootroot00000000000000require 'dirgra-0.3.jar' import 'org.jruby.dirgra.DirectedGraph' import 'org.jruby.dirgra.DataIterator' import 'java.util.NoSuchElementException' describe "DataIterator" do before do @graph = DirectedGraph.new @graph.addEdge(1, 2, "foo") @graph.addEdge(2, 3, "foo") end # hasNext method doesn't use the source or destination of iterator at all # So specs are not written for iterator having source set to false describe "hasNext" do context "edges of given type" do it "returns true if the iterator contains an edge of given type" do iterator = DataIterator.new(@graph.edges(), "foo", true, false) expect(iterator.hasNext).to eq true end it "returns false if the iterator does not contain any edge of given type" do iterator = DataIterator.new(@graph.edges(), "bar", true, false) expect(iterator.hasNext).to eq false end end context "edges not of given type" do it "returns true if the iterator contains an edge not of given type" do iterator = DataIterator.new(@graph.edges(), "bar", true, true) expect(iterator.hasNext).to eq true end it "returns false if the iterator contains an edge of given type" do iterator = DataIterator.new(@graph.edges(), "foo", true, true) expect(iterator.hasNext).to eq false end end context "when iterator type is null" do context "edges of given type" do it "returns true if the iterator contains an edge of type nil" do # add an edge of type nil @graph.addEdge(4,1,nil) iterator = DataIterator.new(@graph.edges(), nil, true, false) expect(iterator.hasNext).to eq true end it "returns false if the iterator does not contain any edge of type nil" do iterator = DataIterator.new(@graph.edges(), nil, true, false) expect(iterator.hasNext).to eq false end end context "edges not of given type" do it "returns true if the iterator contains an edge not of type nil" do iterator = DataIterator.new(@graph.edges(), nil, true, true) expect(iterator.hasNext).to eq true end it "returns false if the iterator contains all edges of type nil" do # remove existing edges not of type nil @graph.removeEdge(1,2) @graph.removeEdge(2,3) # add an edge of type nil @graph.addEdge(4,1,nil) iterator = DataIterator.new(@graph.edges(), nil, true, true) expect(iterator.hasNext).to eq false end end end context "when edge type is nil and iterator type is not nil" do it "returns true if the iterator contains an edge not of type nil" do # remove existing edges not of type nil @graph.removeEdge(1,2) @graph.removeEdge(2,3) # add an edge of type nil @graph.addEdge(4,1,nil) iterator = DataIterator.new(@graph.edges(), "foo", true, true) expect(iterator.hasNext).to eq true end it "returns false if the iterator contains all edges not of type nil" do iterator = DataIterator.new(@graph.edges(), "foo", true, true) expect(iterator.hasNext).to eq false end end end describe "next" do context "when the iterator has next edge" do context "when asked for data of source vertex" do it "returns the data of the source of the edge" do iterator = DataIterator.new(@graph.edges(), "foo", true, false) expect([1, 2]).to include iterator.next end end context "when asked for data of destination vertex" do it "returns the data of the destination of the edge" do iterator = DataIterator.new(@graph.edges(), "foo", false, false) expect([2, 3]).to include iterator.next end end end context "when the iterator does not have next edge" do before do @empty_graph = DirectedGraph.new end it "throws NoSuchElementException for source data" do iterator = DataIterator.new(@empty_graph.edges(), "foo", true, false) expect { iterator.next }.to raise_error NoSuchElementException end it "throws NoSuchElementException for destination data" do iterator = DataIterator.new(@empty_graph.edges(), "foo", false, false) expect { iterator.next }.to raise_error NoSuchElementException end end end describe "remove" do it "throws UnsupportedOperationException exception" do iterator = DataIterator.new(@graph.edges(), "foo", true, false) expect { iterator.remove }.to raise_error Java::JavaLang::UnsupportedOperationException end end end dirgra-0.3/spec/directed_graph_spec.rb000066400000000000000000000045561253635613000200770ustar00rootroot00000000000000require 'dirgra-0.3.jar' import 'org.jruby.dirgra.DirectedGraph' # This is spec for Directed Graph Library describe "Directed Graph Utility" do before do @graph = DirectedGraph.new end it "adds an edge to newly created graph" do @graph.edges.size.should be 0 @graph.addEdge(1,2,'foo') @graph.addEdge(4,5,'bar') @graph.edges.size.should be 2 end it "removes an existing edge from a graph" do @graph.addEdge(1,2,'foo') @graph.addEdge(4,5,'bar') @graph.removeEdge(4,5) @graph.edges.size.should be 1 @graph.removeEdge(@graph.edges.to_a.last) @graph.edges.size.should be 0 end it "does not delete a non-existent edge from the graph" do @graph.removeEdge(2,1) @graph.edges.size.should be 0 end it "removes a vertex and its associated edges" do @graph.removeVertexFor(3) @graph.vertices.size.should be 0 @graph.addEdge(1,2,'foo') @graph.addEdge(4,5,'bar') @graph.removeVertexFor(2) @graph.vertices.size.should be 3 @graph.edges.size.should be 1 end it "gives vertex for given data" do @graph.addEdge(1,2,'foo') @graph.findOrCreateVertexFor(2).getData().should be 2 end it "creates a new vertex if it is not present" do @graph.findOrCreateVertexFor(100).getData().should be 100 end it "finds already existing vertex" do @graph.findVertexFor(100).should be_nil @graph.addEdge(1,2,'foo') @graph.findVertexFor(1).getData().should be 1 end it "gives correct size of graph" do @graph.removeEdge(1,2) @graph.size.should be 0 @graph.addEdge(5,6,'baz') @graph.size.should be 2 @graph.addEdge('foo','bar','baz') @graph.size.should be 4 end it "gives all data in the graph" do @graph.allData.size.should be 0 @graph.addEdge(1,2,'baz') @graph.allData.each do |key| @graph.findVertexFor(key).should_not be_nil end @graph.removeVertexFor(1) @graph.allData.each do |key| @graph.findVertexFor(key).should_not be_nil end end it "gives data in the graph in the order in which it was inserted" do @graph.getInorderData.to_a.size.should be 0 @graph.findOrCreateVertexFor(1) @graph.getInorderData.to_a.should eq [1] @graph.addEdge('foo','bar','baz') @graph.getInorderData.to_a.should eq [1,'foo','bar'] @graph.removeVertexFor('foo') @graph.getInorderData.to_a.should eq [1,'bar'] end end dirgra-0.3/spec/edge_spec.rb000066400000000000000000000013241253635613000160250ustar00rootroot00000000000000require 'dirgra-0.3.jar' import 'org.jruby.dirgra.Edge' import 'org.jruby.dirgra.Vertex' import 'org.jruby.dirgra.DirectedGraph' describe "Edge" do before do @graph = DirectedGraph.new end describe "toString" do context "When edge type is not null" do it "represents edge with type" do edge = Edge.new(Vertex.new(@graph, "foo", 1), Vertex.new(@graph, "bar", 2), "baz") expect(edge.toString).to eq "<1 --> 2> (baz)" end end context "When edge type is null" do it "represents edge without type" do edge = Edge.new(Vertex.new(@graph, "foo", 1), Vertex.new(@graph, "bar", 2), nil) expect(edge.toString).to eq "<1 --> 2>" end end end end dirgra-0.3/spec/edge_type_iterator_spec.rb000066400000000000000000000077161253635613000210120ustar00rootroot00000000000000$LOAD_PATH.unshift File.dirname(__FILE__) + "/helpers" require 'dirgra-0.3.jar' require 'edge_helpers' import 'org.jruby.dirgra.DirectedGraph' import 'org.jruby.dirgra.EdgeTypeIterator' import 'java.util.NoSuchElementException' describe "EdgeTypeIterable" do before do @graph = DirectedGraph.new @graph.addEdge(1, 2, "foo") @graph.addEdge(2, 3, "foo") end describe "hasNext" do context "edges of given type" do it "returns true if the iterator contains an edge of given type" do iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false) expect(iterator.hasNext).to eq true end it "returns false if the iterator does not contain any edge of given type" do iterator = EdgeTypeIterator.new(@graph.edges(), "bar", false) expect(iterator.hasNext).to eq false end end context "edges not of given type" do it "returns true if the iterator contains an edge not of given type" do iterator = EdgeTypeIterator.new(@graph.edges(), "bar", true) expect(iterator.hasNext).to eq true end it "returns false if the iterator contains an edge of given type" do iterator = EdgeTypeIterator.new(@graph.edges(), "foo", true) expect(iterator.hasNext).to eq false end end context "when iterator type is null" do context "edges of given type" do it "returns true if the iterator contains an edge of type nil" do # add an edge of type nil @graph.addEdge(4,1,nil) iterator = EdgeTypeIterator.new(@graph.edges(), nil, false) expect(iterator.hasNext).to eq true end it "returns false if the iterator does not contain any edge of type nil" do iterator = EdgeTypeIterator.new(@graph.edges(), nil, false) expect(iterator.hasNext).to eq false end end context "edges not of given type" do it "returns true if the iterator contains an edge not of type nil" do iterator = EdgeTypeIterator.new(@graph.edges(), nil, true) expect(iterator.hasNext).to eq true end it "returns false if the iterator contains all edges of type nil" do # remove existing edges not of type nil @graph.removeEdge(1,2) @graph.removeEdge(2,3) # add an edge of type nil @graph.addEdge(4,1,nil) iterator = EdgeTypeIterator.new(@graph.edges(), nil, true) expect(iterator.hasNext).to eq false end end end context "when edge type is nil and iterator type is not nil" do it "returns true if the iterator contains an edge not of type nil" do # remove existing edges not of type nil @graph.removeEdge(1,2) @graph.removeEdge(2,3) # add an edge of type nil @graph.addEdge(4,1,nil) iterator = EdgeTypeIterator.new(@graph.edges(), "foo", true) expect(iterator.hasNext).to eq true end it "returns false if the iterator contains all edges not of type nil" do iterator = EdgeTypeIterator.new(@graph.edges(), "foo", true) expect(iterator.hasNext).to eq false end end end describe "next" do context "when the iterator has next edge" do it "returns the next edge" do iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false) expect(iterator.next).to have_type("foo") end end context "when the iterator does not have next edge" do it "throws NoSuchElementException" do empty_graph = DirectedGraph.new iterator = EdgeTypeIterator.new(empty_graph.edges(), "foo", false) expect { iterator.next }.to raise_error NoSuchElementException end end end describe "remove" do it "throws UnsupportedOperationException exception" do iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false) expect { iterator.remove }.to raise_error Java::JavaLang::UnsupportedOperationException end end end dirgra-0.3/spec/helpers/000077500000000000000000000000001253635613000152245ustar00rootroot00000000000000dirgra-0.3/spec/helpers/edge_helpers.rb000066400000000000000000000006411253635613000202000ustar00rootroot00000000000000require 'java' require 'dirgra-0.3.jar' import 'org.jruby.dirgra.Edge' class EdgeType def initialize(method, type) @method = method @edge_type = type end def type @actual.__send__(@method) end def matches?(actual) @actual = actual type == @edge_type end end module HaveType def have_type(type) EdgeType.new(:getType, type) end end class Object include HaveType end dirgra-0.3/spec/helpers/vertex_helpers.rb000066400000000000000000000031431253635613000206110ustar00rootroot00000000000000import 'org.jruby.dirgra.Vertex' class DegreeMatcher def initialize(method, degree) @method = method @value = degree end def degree @actual.__send__(@method) end def matches?(actual) @actual = actual degree == @value end end module HaveInDegree def have_in_degree(degree) DegreeMatcher.new(:inDegree, degree) end end module HaveOutDegree def have_out_degree(degree) DegreeMatcher.new(:outDegree, degree) end end class Object include HaveInDegree include HaveOutDegree end class Vertex def add_edge(options=nil) self.addEdgeTo(options[:to], options[:type]) end def remove_edge(options=nil) self.removeEdgeTo(options[:to]) end def remove_edges(options={}) case options[:direction] when :in self.removeAllIncomingEdges() when :out self.removeAllOutgoingEdges() else self.removeAllEdges() end end def outgoing_edge(options=nil) if options.nil? self.getOutgoingEdge else self.getOutgoingEdgeOfType(options[:type]) end end def incoming_edge(options=nil) if options.nil? self.getIncomingEdge else self.getIncomingEdgeOfType(options[:type]) end end def data(options={}) case options[:direction] when :in if options.keys.include?(:type) self.getIncomingSourceDataOfType(options[:type]) else self.getIncomingSourceData() end when :out if options.keys.include?(:type) self.getOutgoingDestinationDataOfType(options[:type]) else self.getOutgoingDestinationData() end end end end dirgra-0.3/spec/vertex_spec.rb000066400000000000000000000223571253635613000164470ustar00rootroot00000000000000$LOAD_PATH.unshift File.dirname(__FILE__) + "/helpers" require 'dirgra-0.3.jar' require 'vertex_helpers' require 'edge_helpers' import 'org.jruby.dirgra.DirectedGraph' import 'org.jruby.dirgra.Vertex' describe "Vertex" do before do @graph = DirectedGraph.new @source = Vertex.new(@graph, "foo", 1) @dest = Vertex.new(@graph, "bar", 2) end describe "Adding an edge from source to destination" do before do @source.add_edge(:to => @dest) end it "adds outgoing edge to source" do expect(@source).to have_out_degree 1 end it "adds incoming edge to destination" do expect(@dest).to have_in_degree 1 end it "adds the edge to the graph containing source" do expect(@graph.edges()).not_to be nil end it "sets edge type to null if is not provided" do expect(@graph.edges().first).to have_type(nil) end it "sets edge type to the given value if is provided" do @source.remove_edge(:to => @dest) @source.add_edge(:to => @dest, :type => "foobar") expect(@graph.edges.first).to have_type("foobar") end end describe "Removing an outgoing edge from current vertex" do before do @source.add_edge(:to => @dest) end context "Destination of any one of the outgoing edges from the current vertex matched with given destination" do it "removes an edge from outgoing edges of the source vertex" do @source.remove_edge(:to => @dest) expect(@source).to have_out_degree 0 end it "removes an edge from incoming edges of the destination vertex" do @source.remove_edge(:to => @dest) expect(@dest).to have_in_degree 0 end end context "Destination of all of the outgoing edges from the current vertex doesn't match with given destination" do it "returns false" do non_existent_destination = Vertex.new(@graph, "baz", 3) expect(@source.remove_edge(:to => non_existent_destination)).to be false end end end describe "Remove all incoming edges" do before do @interim = Vertex.new(@graph, "interim", 3) @dest.add_edge(:to => @source) @interim.add_edge(:to => @source) end it "removes all incoming edges to the vertex" do @source.remove_edges(:direction => :in) expect(@source).to have_in_degree 0 end end describe "Remove all outgoing edges" do before do @interim = Vertex.new(@graph, "interim", 3) @source.add_edge(:to => @dest) @source.add_edge(:to => @interim) end it "removes all outgoing edges from the vertex" do @source.remove_edges(:direction => :out) expect(@source).to have_out_degree 0 end end describe "Remove all edges" do before do @interim = Vertex.new(@graph, "interim", 3) @source.add_edge(:to => @dest) @source.add_edge(:to => @interim) @dest.add_edge(:to => @source) @interim.add_edge(:to => @source) end it "removes all edges from the vertex" do begin @source.removeAllEdges rescue java.lang.ArrayIndexOutOfBoundsException => e p e.printStackTrace end expect(@source).to have_out_degree 0 expect(@source).to have_in_degree 0 end end describe "getOutGoingEdge" do before do @null_vertex = Vertex.new(@graph, "null", 3) end it "returns first outgoing edge from the vertex not of type 'null'" do @source.add_edge(:to => @dest, :type => "not_null") @source.add_edge(:to => @null_vertex, :type => nil) expect(@source.outgoing_edge).to have_type("not_null") end it "returns null when all outgoing edges from the vertex are of type 'null'" do @source.add_edge(:to => @dest) @source.add_edge(:to => @null_vertex, :type => nil) expect(@source.outgoing_edge).to be nil end end describe "getIncomingEdge" do before do @null_vertex = Vertex.new(@graph, "null", 3) end it "returns first incoming edge to the vertex not of type 'null'" do @source.add_edge(:to => @dest, :type => "not_null") @null_vertex.add_edge(:to => @dest, :type => nil) expect(@dest.incoming_edge).to have_type("not_null") end it "returns null when all incoming edges to the vertex are of type 'null'" do @source.add_edge(:to => @dest) @null_vertex.add_edge(:to => @dest, :type => nil) expect(@dest.incoming_edge).to be nil end end describe "getOutGoingEdgeOfType" do context "when the edge of given type exists" do it "returns first outgoing edge of the given type" do @source.add_edge(:to => @dest, :type => "baz") expect(@source.outgoing_edge(:type => "baz")).to have_type("baz") end end context "when the edge of given type does not exist" do it "returns null" do @source.add_edge(:to => @dest, :type => "foobarbaz") expect(@source.outgoing_edge(:type => "foo-bar-baz")).to be nil end end end describe "getIncomingEdgeOfType" do context "when the edge of given type exists" do it "returns first incoming edge of the given type" do @source.add_edge(:to => @dest, :type => "baz") expect(@dest.incoming_edge(:type => "baz")).to have_type("baz") end end context "when the edge of given type does not exist" do it "returns null" do @source.add_edge(:to => @dest, :type => "foobarbaz") expect(@dest.incoming_edge(:type => "foo-bar-baz")).to be nil end end end describe "getIncomingSourceData" do context "when there is atleast one incoming edge to the current vertex" do it "returns data of the source of that first incoming edge" do @source.add_edge(:to => @dest) expect(@dest.data(:direction => :in)).to eq "foo" end end context "when there is no incoming edge to the current vertex" do it "returns null" do @source.add_edge(:to => @dest) expect(@source.data(:direction => :in)).to be nil end end end describe "getIncomingSourceDataOfType" do context "when there is atleast one incoming edge to the current vertex of the given type" do it "returns data of the source of that first incoming edge of given type" do @source.add_edge(:to => @dest) expect(@dest.data(:direction => :in, :type => nil)).to eq "foo" end end context "when there is no incoming edge to the current vertex of given type" do it "returns null" do @source.add_edge(:to => @dest, :type => "foo") expect(@dest.incoming_edge(:type => nil)).to eq nil end end end describe "getOutgoingDestinationData" do context "when there is atleast one outgoing edge from the current vertex" do it "returns data of the destination of that first outgoing edge" do @source.add_edge(:to => @dest) expect(@source.data(:direction => :out)).to eq "bar" end end context "when there is no outgoing edge from the current vertex" do it "returns null" do @source.add_edge(:to => @dest) expect(@dest.data(:direction => :out)).to be nil end end end describe "getOutgoingDestinationDataOfType" do context "when there is atleast one outgoing edge from the current vertex of the given type" do it "returns data of the source of that first outgoing edge of given type" do @source.add_edge(:to => @dest) expect(@source.data(:direction => :out, :type => nil)).to eq "bar" end end context "when there is no outgoing edge from the current vertex of given type" do it "returns null" do @source.add_edge(:to => @dest, :type => "foo") expect(@source.data(:direction => :out, :type => nil)).to be nil end end end describe "toString" do before do @interim = Vertex.new(@graph, "interim", 3) end context "when vertex has no edges" do it "returns string representation of the vertex" do expect(@source.toString).to eq "foo:\n" end end context "when vertex has only one outgoing edge" do it "returns string representation of the vertex" do @source.add_edge(:to => @dest) expect(@source.toString).to eq "foo:>[2]\n" end end context "when vertex has many outgoing edges" do it "returns string representation of the vertex" do @source.add_edge(:to => @dest) @source.add_edge(:to => @interim) expect(["foo:>[2,3]\n", "foo:>[3,2]\n"]).to include @source.toString end end context "when vertex has only one incoming edge" do it "returns string representation of the vertex" do @source.add_edge(:to => @dest) expect(@dest.toString).to eq "bar:<[1]\n" end end context "when vertex has many incoming edges" do it "returns string representation of the vertex" do @source.add_edge(:to => @dest) @interim.add_edge(:to => @dest) expect(["bar:<[1,3]\n", "bar:<[3,1]\n"]).to include @dest.toString end end context "when vertex has both incoming and outgoing edges" do it "returns string representation of the vertex" do @source.add_edge(:to => @interim) @interim.add_edge(:to => @dest) expect(@interim.toString).to eq "interim:>[2], <[1]\n" end end end end dirgra-0.3/src/000077500000000000000000000000001253635613000134175ustar00rootroot00000000000000dirgra-0.3/src/org/000077500000000000000000000000001253635613000142065ustar00rootroot00000000000000dirgra-0.3/src/org/jruby/000077500000000000000000000000001253635613000153415ustar00rootroot00000000000000dirgra-0.3/src/org/jruby/dirgra/000077500000000000000000000000001253635613000166115ustar00rootroot00000000000000dirgra-0.3/src/org/jruby/dirgra/DataIterable.java000066400000000000000000000013271253635613000220000ustar00rootroot00000000000000package org.jruby.dirgra; import java.util.Collection; import java.util.Iterator; import java.util.Set; public class DataIterable implements Iterable { private Edge[] edges; private int edgesLength; private Object type; private boolean negate; private boolean source; public DataIterable(Edge[] edges, int edgesLength, Object type, boolean source, boolean negate) { this.edges = edges; this.edgesLength = edgesLength; this.type = type; this.negate = negate; this.source = source; } @Override public Iterator iterator() { return new DataIterator(edges, edgesLength, type, source, negate); } } dirgra-0.3/src/org/jruby/dirgra/DataIterator.java000066400000000000000000000042021253635613000220350ustar00rootroot00000000000000package org.jruby.dirgra; import java.util.Iterator; import java.util.NoSuchElementException; public class DataIterator implements Iterator { private Edge[] edges; private int edgesLength; private int edgeIteratorIndex; private Object type; private Edge nextEdge = null; private boolean source; private boolean negate; public DataIterator(Edge[] edges, int edgesLength, Object type, boolean source, boolean negate) { this.edges = edges; this.edgesLength = edgesLength; this.edgeIteratorIndex = 0; this.type = type; this.source = source; this.negate = negate; } @Override public boolean hasNext() { // Multiple hasNext calls with no next...hasNext still true if (nextEdge != null) return true; for (int i = edgeIteratorIndex; i < edgesLength; i++) { Edge edge = edges[i]; Object edgeType = edge.getType(); if (negate) { // When edgeType or type is null compare them directly. Otherwise compare them using equals if ((edgeType != null && !edgeType.equals(type)) || (edgeType == null && edgeType != type)) { nextEdge = edge; edgeIteratorIndex = i + 1; return true; } // When edgeType or type is null compare them directly. Otherwise compare them using equals } else if ((edgeType != null && edgeType.equals(type)) || (edgeType == null && edgeType == type)) { nextEdge = edge; edgeIteratorIndex = i + 1; return true; } } edgeIteratorIndex = edgesLength; return false; } @Override public T next() { if (hasNext()) { Edge tmp = nextEdge; nextEdge = null; return source ? tmp.getSource().getData() : tmp.getDestination().getData(); } throw new NoSuchElementException(); } @Override public void remove() { throw new UnsupportedOperationException("Not supported"); } } dirgra-0.3/src/org/jruby/dirgra/DirectedGraph.java000066400000000000000000000106101253635613000221570ustar00rootroot00000000000000package org.jruby.dirgra; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class DirectedGraph { private static int INITIAL_SIZE = 4; private Map> vertices = new HashMap>(); private Edge[] edges = new Edge[INITIAL_SIZE]; private int edgeLength = 0; private ArrayList inOrderVerticeData = new ArrayList(); int vertexIDCounter = 0; protected Edge[] growEdges(Edge[] array, int realLength) { int newLength = array.length == 0 ? 2 : array.length * 2; Edge[] newEdges = new Edge[newLength]; System.arraycopy(array, 0, newEdges, 0, realLength); return newEdges; } protected Edge[] getEdges() { return edges; } protected Edge addEdge(Edge newEdge) { for (int i = 0; i < edgeLength; i++) { // Edge already added. No repeated edge support. if (edges[i].equals(newEdge)) return newEdge; } if (edgeLength >= edges.length) edges = growEdges(edges, edgeLength); edges[edgeLength++] = newEdge; return newEdge; } public void removeEdge(Edge edge) { int splitPoint = -1; for (int i = 0; i < edgeLength; i++) { if (edges[i].equals(edge)) { splitPoint = i; break; } } if (splitPoint != -1) { Edge edgeToRemove = edges[splitPoint]; if (splitPoint < edgeLength - 1) { // somewhere between index 0 and edgeLength-2 System.arraycopy(edges, splitPoint + 1, edges, splitPoint, edgeLength - 1 - splitPoint); } edges[edgeLength - 1] = null; // do not pin left over edge after shifting edgeLength--; // Remove knowledge of edge from each vertex edge.getSource().removeOutgoingEdge(edgeToRemove); edge.getDestination().removeIncomingEdge(edgeToRemove); } } public Collection> vertices() { return vertices.values(); } public Collection> edges() { return Arrays.asList(Arrays.copyOf(edges, edgeLength)); } public Iterable> edgesOfType(Object type) { return new EdgeTypeIterable(edges, edgeLength, type); } public Collection allData() { return vertices.keySet(); } /** * @return data in the order it was added to this graph. */ public Collection getInorderData() { return inOrderVerticeData; } public void addEdge(T source, T destination, Object type) { findOrCreateVertexFor(source).addEdgeTo(destination, type); } public void removeEdge(T source, T destination) { if (findVertexFor(source) != null) { for (Edge edge: findOrCreateVertexFor(source).getOutgoingEdges()) { if (edge.getDestination().getData() == destination) { findOrCreateVertexFor(source).removeEdgeTo(edge.getDestination()); return; } } } } public Vertex findVertexFor(T data) { return vertices.get(data); } /** * @return vertex for given data. If vertex is not present it creates vertex and returns it. */ public Vertex findOrCreateVertexFor(T data) { Vertex vertex = vertices.get(data); if (vertex != null) return vertex; vertex = new Vertex(this, data, vertexIDCounter++); inOrderVerticeData.add(data); vertices.put(data, vertex); return vertex; } public void removeVertexFor(T data) { if (findVertexFor(data) != null) { Vertex vertex = findOrCreateVertexFor(data); vertices.remove(data); inOrderVerticeData.remove(data); vertex.removeAllEdges(); } } /** * @return the number of vertices in the graph. */ public int size() { return allData().size(); } @Override public String toString() { StringBuilder buf = new StringBuilder(); ArrayList> verts = new ArrayList>(vertices.values()); Collections.sort(verts); for (Vertex vertex: verts) { buf.append(vertex); } return buf.toString(); } } dirgra-0.3/src/org/jruby/dirgra/Edge.java000066400000000000000000000013251253635613000203210ustar00rootroot00000000000000package org.jruby.dirgra; public class Edge { private Vertex source; private Vertex destination; private Object type; public Edge(Vertex source, Vertex destination, Object type) { this.source = source; this.destination = destination; this.type = type; } public Vertex getDestination() { return destination; } public Vertex getSource() { return source; } public Object getType() { return type; } @Override public String toString() { return "<" + source.getID() + " --> " + destination.getID() + ">" + (type == null ? "" : " (" + type + ")"); } } dirgra-0.3/src/org/jruby/dirgra/EdgeTypeIterable.java000066400000000000000000000014311253635613000226310ustar00rootroot00000000000000package org.jruby.dirgra; import java.util.Collection; import java.util.Iterator; import java.util.Set; public class EdgeTypeIterable implements Iterable> { private Edge[] edges; int edgesLength; private Object type; private boolean negate; public EdgeTypeIterable(Edge[] edges, int edgesLength, Object type) { this(edges, edgesLength, type, false); } public EdgeTypeIterable(Edge[] edges, int edgesLength, Object type, boolean negate) { this.edges = edges; this.edgesLength = edgesLength; this.type = type; this.negate = negate; } @Override public Iterator> iterator() { return new EdgeTypeIterator(edges, edgesLength, type, negate); } } dirgra-0.3/src/org/jruby/dirgra/EdgeTypeIterator.java000066400000000000000000000040361253635613000226770ustar00rootroot00000000000000package org.jruby.dirgra; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; public class EdgeTypeIterator implements Iterator> { private Edge[] edges; private int edgesLength; private int edgeIteratorIndex = 0; private Object type; private Edge nextEdge = null; private boolean negate; public EdgeTypeIterator(Edge[] edges, int edgesLength, Object type, boolean negate) { this.edges = edges; this.edgesLength = edgesLength; this.type = type; this.negate = negate; } @Override public boolean hasNext() { // Multiple hasNext calls with no next...hasNext still true if (nextEdge != null) return true; for (int i = edgeIteratorIndex; i < edgesLength; i++) { Edge edge = edges[i]; Object edgeType = edge.getType(); if (negate) { // When edgeType or type is null compare them directly. Otherwise compare them using equals if ((edgeType != null && !edgeType.equals(type)) || (edgeType == null && edgeType != type)) { nextEdge = edge; edgeIteratorIndex = i + 1; return true; } // When edgeType or type is null compare them directly. Otherwise compare them using equals } else if ((edgeType != null && edgeType.equals(type)) || (edgeType == null && edgeType == type)) { nextEdge = edge; edgeIteratorIndex = i + 1; return true; } } edgeIteratorIndex = edgesLength; return false; } @Override public Edge next() { if (hasNext()) { Edge tmp = nextEdge; nextEdge = null; return tmp; } throw new NoSuchElementException(); } @Override public void remove() { throw new UnsupportedOperationException("Not supported"); } } dirgra-0.3/src/org/jruby/dirgra/ExplicitVertexID.java000066400000000000000000000001311253635613000226430ustar00rootroot00000000000000package org.jruby.dirgra; public interface ExplicitVertexID { public int getID(); } dirgra-0.3/src/org/jruby/dirgra/Vertex.java000066400000000000000000000240651253635613000207400ustar00rootroot00000000000000package org.jruby.dirgra; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; /** * */ public class Vertex implements Comparable> { private static final Edge[] EMPTY_EDGE_LIST = new Edge[0]; private DirectedGraph graph; private T data; private Edge[] incoming = EMPTY_EDGE_LIST; private int incomingLength = 0; private Edge[] outgoing = EMPTY_EDGE_LIST; private int outgoingLength = 0; int id; public Vertex(DirectedGraph graph, T data, int id) { this.graph = graph; this.data = data; this.id = id; } public void addEdgeTo(Vertex destination) { addEdgeTo(destination, null); } public void addEdgeTo(Vertex destination, Object type) { Edge newEdge = graph.addEdge(new Edge(this, destination, type)); addOutgoingEdge(newEdge); destination.addIncomingEdge(newEdge); } public void addEdgeTo(T destination) { addEdgeTo(destination, null); } public void addEdgeTo(T destination, Object type) { Vertex destinationVertex = graph.findOrCreateVertexFor(destination); addEdgeTo(destinationVertex, type); } public boolean removeEdgeTo(Vertex destination) { for (int i = 0; i < outgoingLength; i++) { Edge edge = outgoing[i]; if (edge.getDestination() == destination) { graph.removeEdge(edge); // This will remove incoming/outgoing as side-effect. return true; } } return false; } protected void addOutgoingEdge(Edge newEdge) { for (int i = 0; i < outgoingLength; i++) { // Edge already added. No repeated edge support. if (outgoing[i].equals(newEdge)) return; } if (outgoingLength >= outgoing.length) outgoing = graph.growEdges(outgoing, outgoingLength); outgoing[outgoingLength++] = newEdge; } protected void addIncomingEdge(Edge newEdge) { for (int i = 0; i < incomingLength; i++) { // Edge already added. No repeated edge support. if (incoming[i] == newEdge) return; } if (incomingLength >= incoming.length) incoming = graph.growEdges(incoming, incomingLength); incoming[incomingLength++] = newEdge; } protected void removeOutgoingEdge(Edge edge) { int splitIndex = -1; // which index we found the edge at for (int i = 0; i < outgoingLength; i++) { if (outgoing[i].equals(edge)) { splitIndex = i; break; } } if (splitIndex == -1) return; // no edge found if (splitIndex != outgoingLength-1) { // need to shift over all elements one System.arraycopy(outgoing, splitIndex + 1, outgoing, splitIndex, outgoingLength - 1 - splitIndex); } outgoing[outgoingLength-1] = null; // we made list one smaller null last element so it does not leak. outgoingLength--; // list is one smaller } protected void removeIncomingEdge(Edge edge) { int splitIndex = -1; // which index we found the edge at for (int i = 0; i < incomingLength; i++) { if (incoming[i].equals(edge)) { splitIndex = i; break; } } if (splitIndex == -1) return; // no edge found if (splitIndex != incomingLength-1) { // need to shift over all elements one System.arraycopy(incoming, splitIndex + 1, incoming, splitIndex, incomingLength - 1 - splitIndex); } incoming[incomingLength-1] = null; // we made list one smaller null last element so it does not leak. incomingLength--; // list is one smaller } public void removeAllIncomingEdges() { while (incomingLength > 0) { // Each removal will decrement length until none are left graph.removeEdge(incoming[0]); // This will remove incoming/outgoing as side-effect. } incoming = EMPTY_EDGE_LIST; } public void removeAllOutgoingEdges() { while (outgoingLength > 0) { // Each removal will decrement length until none are left graph.removeEdge(outgoing[0]); // This will remove incoming/outgoing as side-effect. } outgoing = EMPTY_EDGE_LIST; } public void removeAllEdges() { removeAllIncomingEdges(); removeAllOutgoingEdges(); } public int inDegree() { return incomingLength; } public int outDegree() { return outgoingLength; } public Iterable> getIncomingEdgesOfType(Object type) { return new EdgeTypeIterable(incoming, incomingLength, type); } public Iterable> getIncomingEdgesNotOfType(Object type) { return new EdgeTypeIterable(incoming, incomingLength, type, true); } public Iterable> getOutgoingEdgesOfType(Object type) { return new EdgeTypeIterable(outgoing, outgoingLength, type); } public T getIncomingSourceData() { Edge edge = getFirstEdge(getIncomingEdges().iterator()); return edge == null ? null : edge.getSource().getData(); } public T getIncomingSourceDataOfType(Object type) { Edge edge = getFirstEdge(getIncomingEdgesOfType(type).iterator()); return edge == null ? null : edge.getSource().getData(); } public Iterable getIncomingSourcesData() { return new DataIterable(incoming, incomingLength, null, true, true); } public Iterable getIncomingSourcesDataOfType(Object type) { return new DataIterable(incoming, incomingLength, type, true, false); } public Iterable getIncomingSourcesDataNotOfType(Object type) { return new DataIterable(incoming, incomingLength, type, true, true); } public Iterable> getOutgoingEdgesNotOfType(Object type) { return new EdgeTypeIterable(outgoing, outgoingLength, type, true); } public Iterable getOutgoingDestinationsData() { return new DataIterable(outgoing, outgoingLength, null, false, true); } public Iterable getOutgoingDestinationsDataOfType(Object type) { return new DataIterable(outgoing, outgoingLength, type, false, false); } public Iterable getOutgoingDestinationsDataNotOfType(Object type) { return new DataIterable(outgoing, outgoingLength, type, false, true); } public T getOutgoingDestinationData() { Edge edge = getFirstEdge(getOutgoingEdges().iterator()); return edge == null ? null : edge.getDestination().getData(); } public T getOutgoingDestinationDataOfType(Object type) { Edge edge = getFirstEdge(getOutgoingEdgesOfType(type).iterator()); return edge == null ? null : edge.getDestination().getData(); } private Edge getFirstEdge(Iterator> iterator) { return iterator.hasNext() ? iterator.next() : null; } public Edge getIncomingEdgeOfType(Object type) { return getFirstEdge(getIncomingEdgesOfType(type).iterator()); } public Edge getOutgoingEdgeOfType(Object type) { return getFirstEdge(getOutgoingEdgesOfType(type).iterator()); } public Edge getIncomingEdge() { return getFirstEdge(getIncomingEdgesNotOfType(null).iterator()); } public Edge getOutgoingEdge() { return getFirstEdge(getOutgoingEdgesNotOfType(null).iterator()); } public Collection> getIncomingEdges() { return Arrays.asList(Arrays.copyOf(incoming, incomingLength)); } public Collection> getOutgoingEdges() { return Arrays.asList(Arrays.copyOf(outgoing, outgoingLength)); } public T getData() { return data; } public int getID() { return data.getID(); } // FIXME: This is pretty ugly...creating massive number of comparators @Override public String toString() { boolean found = false; StringBuilder buf = new StringBuilder(data.toString()); buf.append(":"); Collection> edges = getOutgoingEdges(); int size = edges.size(); if (size > 0) { found = true; buf.append(">["); List> e = new ArrayList>(edges); Collections.sort(e, new DestinationCompare()); for (int i = 0; i < size - 1; i++) { buf.append(e.get(i).getDestination().getID()).append(","); } buf.append(e.get(size - 1).getDestination().getID()).append("]"); } edges = getIncomingEdges(); size = edges.size(); if (size > 0) { if (found) buf.append(", "); buf.append("<["); List> e = new ArrayList>(edges); Collections.sort(e, new SourceCompare()); for (int i = 0; i < size - 1; i++) { buf.append(e.get(i).getSource().getID()).append(","); } buf.append(e.get(size - 1).getSource().getID()).append("]"); } buf.append("\n"); return buf.toString(); } @Override public int compareTo(Vertex that) { if (getID() == that.getID()) return 0; if (getID() < that.getID()) return -1; return 1; } class SourceCompare implements Comparator> { @Override public int compare(Edge o1, Edge o2) { int i1 = o1.getSource().getID(); int i2 = o2.getSource().getID(); if (i1 == i2) return 0; return i1 < i2 ? -1 : 1; } } class DestinationCompare implements Comparator> { @Override public int compare(Edge o1, Edge o2) { int i1 = o1.getDestination().getID(); int i2 = o2.getDestination().getID(); if (i1 == i2) return 0; return i1 < i2 ? -1 : 1; } } }