pax_global_header 0000666 0000000 0000000 00000000064 14021724606 0014514 g ustar 00root root 0000000 0000000 52 comment=6ffe4d211c682c9bdb836dfa145a941060b536f7
dirgra-0.4/ 0000775 0000000 0000000 00000000000 14021724606 0012627 5 ustar 00root root 0000000 0000000 dirgra-0.4/.gitignore 0000664 0000000 0000000 00000000071 14021724606 0014615 0 ustar 00root root 0000000 0000000 target
/.settings/
/examples/.redcar/
/.redcar/
*~
.idea
dirgra-0.4/LICENSE.txt 0000664 0000000 0000000 00000026642 14021724606 0014464 0 ustar 00root root 0000000 0000000 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.4/MANIFEST.MF 0000664 0000000 0000000 00000000121 14021724606 0014253 0 ustar 00root root 0000000 0000000 Implementation-Title: Dirgra (Simple Directed Graph)
Implementation-Version: 0.1
dirgra-0.4/README.md 0000664 0000000 0000000 00000000176 14021724606 0014112 0 ustar 00root root 0000000 0000000 = Simple Directed Graph Implementation
= Building
```text
% mvn package
```
= Testing
```text
% jruby -Itarget rspec
```
dirgra-0.4/pom.xml 0000664 0000000 0000000 00000007467 14021724606 0014162 0 ustar 00root root 0000000 0000000
4.0.0org.jrubydirgrajar0.4-SNAPSHOTDirgrahttps://github.com/jruby/dirgraSimple Directed Graphorg.sonatype.ossoss-parent7scm:git:https://github.com/jruby/dirgra.gitscm:git:https://github.com/jruby/dirgra.githttp://github.com/jruby/dirgraEPLhttp://www.eclipse.org/legal/epl-v10.htmleneboThomas E. Enebotom.enebo@gmail.comjunitjunit4.13.1testsrctestorg.apache.maven.wagonwagon-webdav-jackrabbit2.1org.apache.maven.pluginsmaven-source-plugin3.2.1attach-sourcesjarorg.apache.maven.pluginsmaven-javadoc-plugin3.0.1attach-javadocsjarorg.apache.maven.pluginsmaven-compiler-plugin3.7.0default-compilemodule-info.java9default-testCompile9base-compilecompile88module-info.javamaven-jar-plugin3.2.0MANIFEST.MFmaven-release-plugin2.5.3
dirgra-0.4/spec/ 0000775 0000000 0000000 00000000000 14021724606 0013561 5 ustar 00root root 0000000 0000000 dirgra-0.4/spec/data_iterator_spec.rb 0000664 0000000 0000000 00000011424 14021724606 0017744 0 ustar 00root root 0000000 0000000 require 'dirgra-0.3.jar'
import 'org.jruby.dirgra.DirectedGraph'
import 'org.jruby.dirgra.DataIterator'
import 'java.util.NoSuchElementException'
require 'vertex_id_helper'
describe "DataIterator" do
let(:graph) { DirectedGraph.new }
let(:one) { VertexID.new(1) }
let(:two) { VertexID.new(2) }
let(:three) { VertexID.new(3) }
let(:four) { VertexID.new(4) }
before do
@edge_count = 0
add_edge(one, two, "foo")
add_edge(two, three, "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(*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(*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(*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(*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
add_edge(four,one,nil)
iterator = DataIterator.new(*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(*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(*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
remove_edge(one,two)
remove_edge(two,three)
# add an edge of type nil
add_edge(four,one,nil)
iterator = DataIterator.new(*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
remove_edge(one,two)
remove_edge(two,three)
# add an edge of type nil
add_edge(four,one,nil)
iterator = DataIterator.new(*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(*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(*edges, "foo", true, false)
expect([one, two]).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(*edges, "foo", false, false)
expect([two, three]).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.to_array, 0, "foo", true, false)
expect { iterator.next }.to raise_error NoSuchElementException
end
it "throws NoSuchElementException for destination data" do
iterator = DataIterator.new(@empty_graph.edges.to_array, 0, "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.to_array, 0, "foo", true, false)
expect { iterator.remove }.to raise_error Java::JavaLang::UnsupportedOperationException
end
end
end
dirgra-0.4/spec/directed_graph_spec.rb 0000664 0000000 0000000 00000005405 14021724606 0020070 0 ustar 00root root 0000000 0000000 require 'dirgra-0.3.jar'
import 'org.jruby.dirgra.DirectedGraph'
require 'vertex_id_helper'
# This is spec for Directed Graph Library
describe "Directed Graph Utility" do
let(:graph) { DirectedGraph.new }
let(:one) { VertexID.new(1) }
let(:two) { VertexID.new(2) }
let(:three) { VertexID.new(3) }
let(:four) { VertexID.new(4) }
let(:five) { VertexID.new(5) }
let(:six) { VertexID.new(6) }
let(:hundred) { VertexID.new(100) }
let(:foo) { VertexID.new("foo") }
let(:bar) { VertexID.new("bar") }
before do
@edge_count = 0
end
it "adds an edge to newly created graph" do
expect(graph.edges.size).to eq 0
add_edge(one,two,'foo')
add_edge(four,five,'bar')
expect(graph.edges.size).to eq 2
end
it "removes an existing edge from a graph" do
add_edge(one,two,'foo')
add_edge(four,five,'bar')
remove_edge(four,five)
expect(graph.edges.size).to eq 1
graph.removeEdge(graph.edges.to_a.last)
expect(graph.edges.size).to eq 0
end
it "does not delete a non-existent edge from the graph" do
remove_edge(two,one)
expect(graph.edges.size).to eq 0
end
it "removes a vertex and its associated edges" do
graph.removeVertexFor(three)
expect(graph.vertices.size).to eq 0
add_edge(one,two,'foo')
add_edge(four,five,'bar')
graph.removeVertexFor(two)
expect(graph.vertices.size).to eq 3
expect(graph.edges.size).to eq 1
end
it "gives vertex for given data" do
add_edge(one,two,'foo')
expect(graph.findOrCreateVertexFor(two).getData()).to eq two
end
it "creates a new vertex if it is not present" do
expect(graph.findOrCreateVertexFor(hundred).getData()).to eq hundred
end
it "finds already existing vertex" do
expect(graph.findVertexFor(hundred)).to eq nil
add_edge(one,two,'foo')
expect(graph.findVertexFor(one).getData()).to eq one
end
it "gives correct size of graph" do
remove_edge(one,two)
expect(graph.size).to eq 0
add_edge(five,six,'baz')
expect(graph.size).to eq 2
add_edge(foo,bar,'baz')
expect(graph.size).to eq 4
end
it "gives all data in the graph" do
expect(graph.allData.size).to eq 0
add_edge(one,two,'baz')
graph.allData.each do |key|
expect(graph.findVertexFor(key)).to_not eq nil
end
graph.removeVertexFor(one)
graph.allData.each do |key|
expect(graph.findVertexFor(key)).to_not eq nil
end
end
it "gives data in the graph in the order in which it was inserted" do
expect(graph.getInorderData.to_a.size).to eq 0
graph.findOrCreateVertexFor(one)
expect(graph.getInorderData.to_a).to eq [one]
add_edge(foo,bar,'baz')
expect(graph.getInorderData.to_a).to eq [one,foo,bar]
graph.removeVertexFor(foo)
expect(graph.getInorderData.to_a).to eq [one,bar]
end
end
dirgra-0.4/spec/edge_spec.rb 0000664 0000000 0000000 00000001427 14021724606 0016030 0 ustar 00root root 0000000 0000000 require 'dirgra-0.3.jar'
import 'org.jruby.dirgra.Edge'
import 'org.jruby.dirgra.Vertex'
import 'org.jruby.dirgra.DirectedGraph'
require 'vertex_id_helper'
describe "Edge" do
let(:graph) { DirectedGraph.new }
let(:foo) { VertexID.new(1) }
let(:bar) { VertexID.new(2) }
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.4/spec/edge_type_iterator_spec.rb 0000664 0000000 0000000 00000010053 14021724606 0020775 0 ustar 00root root 0000000 0000000 $LOAD_PATH.unshift File.dirname(__FILE__) + "/helpers"
require 'dirgra-0.3.jar'
require 'edge_helpers'
require 'vertex_id_helper'
import 'org.jruby.dirgra.DirectedGraph'
import 'org.jruby.dirgra.EdgeTypeIterator'
import 'java.util.NoSuchElementException'
describe "EdgeTypeIterable" do
let(:graph) { DirectedGraph.new }
let(:one) { VertexID.new(1) }
let(:two) { VertexID.new(2) }
let(:three) { VertexID.new(3) }
let(:four) { VertexID.new(4) }
before do
@edge_count = 0
add_edge(one, two, "foo")
add_edge(two, three, "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(*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(*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(*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(*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
add_edge(four,one,nil)
iterator = EdgeTypeIterator.new(*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(*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(*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
remove_edge(one,two)
remove_edge(two,three)
# add an edge of type nil
add_edge(four,one,nil)
iterator = EdgeTypeIterator.new(*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
remove_edge(one,two)
remove_edge(two,three)
# add an edge of type nil
add_edge(four,one,nil)
iterator = EdgeTypeIterator.new(*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(*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(*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().to_array(), 0, "foo", false)
expect { iterator.next }.to raise_error NoSuchElementException
end
end
end
describe "remove" do
it "throws UnsupportedOperationException exception" do
iterator = EdgeTypeIterator.new(*edges, "foo", false)
expect { iterator.remove }.to raise_error Java::JavaLang::UnsupportedOperationException
end
end
end
dirgra-0.4/spec/helpers/ 0000775 0000000 0000000 00000000000 14021724606 0015223 5 ustar 00root root 0000000 0000000 dirgra-0.4/spec/helpers/edge_helpers.rb 0000664 0000000 0000000 00000000641 14021724606 0020177 0 ustar 00root root 0000000 0000000 require '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.4/spec/helpers/vertex_helpers.rb 0000664 0000000 0000000 00000003143 14021724606 0020610 0 ustar 00root root 0000000 0000000 import '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.4/spec/helpers/vertex_id_helper.rb 0000664 0000000 0000000 00000000630 14021724606 0021077 0 ustar 00root root 0000000 0000000
class VertexID
include org.jruby.dirgra.ExplicitVertexID
def initialize(id)
@id = id;
end
def getID
@id
end
def to_s
@id
end
end
def add_edge(vertex1, vertex2, type)
@edge_count += 1
graph.addEdge(vertex1, vertex2, type)
end
def remove_edge(vertex1, vertex2)
@edge_count -= 1
graph.removeEdge(vertex1, vertex2)
end
def edges
[graph.edges.to_array, @edge_count]
end
dirgra-0.4/spec/vertex_spec.rb 0000664 0000000 0000000 00000017357 14021724606 0016452 0 ustar 00root root 0000000 0000000 $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
let(:graph) { DirectedGraph.new }
let(:foo) { VertexID.new(1) }
let(:bar) { VertexID.new(2) }
before do
@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
end
dirgra-0.4/src/ 0000775 0000000 0000000 00000000000 14021724606 0013416 5 ustar 00root root 0000000 0000000 dirgra-0.4/src/module-info.java 0000664 0000000 0000000 00000000122 14021724606 0016472 0 ustar 00root root 0000000 0000000 module org.jruby.dirgra {
requires java.base;
exports org.jruby.dirgra;
} dirgra-0.4/src/org/ 0000775 0000000 0000000 00000000000 14021724606 0014205 5 ustar 00root root 0000000 0000000 dirgra-0.4/src/org/jruby/ 0000775 0000000 0000000 00000000000 14021724606 0015340 5 ustar 00root root 0000000 0000000 dirgra-0.4/src/org/jruby/dirgra/ 0000775 0000000 0000000 00000000000 14021724606 0016610 5 ustar 00root root 0000000 0000000 dirgra-0.4/src/org/jruby/dirgra/DataIterable.java 0000664 0000000 0000000 00000001327 14021724606 0021777 0 ustar 00root root 0000000 0000000 package 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.4/src/org/jruby/dirgra/DataIterator.java 0000664 0000000 0000000 00000004202 14021724606 0022034 0 ustar 00root root 0000000 0000000 package 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.4/src/org/jruby/dirgra/DirectedGraph.java 0000664 0000000 0000000 00000010770 14021724606 0022165 0 ustar 00root root 0000000 0000000 package 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);
}
/**
* Find existing vertex and if it is not present create it.
*
* @param data to find a vertex for
* @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.4/src/org/jruby/dirgra/Edge.java 0000664 0000000 0000000 00000001325 14021724606 0020320 0 ustar 00root root 0000000 0000000 package 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.4/src/org/jruby/dirgra/EdgeTypeIterable.java 0000664 0000000 0000000 00000001431 14021724606 0022630 0 ustar 00root root 0000000 0000000 package 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.4/src/org/jruby/dirgra/EdgeTypeIterator.java 0000664 0000000 0000000 00000004036 14021724606 0022676 0 ustar 00root root 0000000 0000000 package 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.4/src/org/jruby/dirgra/ExplicitVertexID.java 0000664 0000000 0000000 00000000131 14021724606 0022642 0 ustar 00root root 0000000 0000000 package org.jruby.dirgra;
public interface ExplicitVertexID {
public int getID();
}
dirgra-0.4/src/org/jruby/dirgra/Vertex.java 0000664 0000000 0000000 00000024065 14021724606 0020737 0 ustar 00root root 0000000 0000000 package 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;
}
}
}