Class: JavaClass::Dependencies::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/javaclass/dependencies/node.rb

Overview

A node in a Graph of dependencies. A node contains a map of dependencies (Edge) to other nodes.

Author

Peter Kofler

Direct Known Subclasses

ClassNode, ClasspathNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, size = 0) ⇒ Node

attr_reader :incoming



13
14
15
16
17
18
# File 'lib/javaclass/dependencies/node.rb', line 13

def initialize(name, size=0)
  @name = name
  @size = size
  @dependencies = Hash.new([])
  ### @incoming = Hash.new([])
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



10
11
12
# File 'lib/javaclass/dependencies/node.rb', line 10

def dependencies
  @dependencies
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/javaclass/dependencies/node.rb', line 8

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/javaclass/dependencies/node.rb', line 9

def size
  @size
end

Instance Method Details

#<=>(other) ⇒ Object



36
37
38
# File 'lib/javaclass/dependencies/node.rb', line 36

def <=>(other)
  @name <=> other.name 
end

#==(other) ⇒ Object



28
29
30
# File 'lib/javaclass/dependencies/node.rb', line 28

def ==(other)
  other.respond_to?(:name) && @name == other.name
end

#add_dependencies(dependencies, providers) ⇒ Object

Add a list dependencies of provider .



57
58
59
60
61
# File 'lib/javaclass/dependencies/node.rb', line 57

def add_dependencies(dependencies, providers)
  dependencies.each do |dependency|
    add_dependency(dependency, providers)
  end
end

#add_dependency(dependency, providers) ⇒ Object

Add a dependency Edge for a list of provider Node.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/javaclass/dependencies/node.rb', line 41

def add_dependency(dependency, providers)
  if providers.size == 0
    # external dependency, skip this
  elsif providers.size == 1
    # add dependency to this provider
    provider = providers[0]
    # unless @dependencies[provider].include? dependency
    @dependencies[provider] = @dependencies[provider] + [dependency]
    ### provider.incoming[self] = provider.incoming[self] + [dependency]
    # end
  else
    warn "dependency to \"#{dependency}\" found more than once: #{providers.join(', ')}"
  end
end

#each_dependency_provider(&block) ⇒ Object

Iterate all providers of dependencies of this Node and call block for each provider with its list of dependencies (Edge).



64
65
66
67
68
# File 'lib/javaclass/dependencies/node.rb', line 64

def each_dependency_provider(&block)
  @dependencies.keys.each do |provider|
    block.call(provider, @dependencies[provider])
  end
end

#each_edge(&block) ⇒ Object

Iterate all dependencies of this Node and call block for each provider with each of its dependencies.



71
72
73
74
75
76
77
# File 'lib/javaclass/dependencies/node.rb', line 71

def each_edge(&block)
  each_dependency_provider do |provider, edges|
    edges.each do |edge|
      block.call(provider, edge)
    end
  end
end

#hashObject



32
33
34
# File 'lib/javaclass/dependencies/node.rb', line 32

def hash
  @name.hash
end

#to_sObject



20
21
22
23
24
25
26
# File 'lib/javaclass/dependencies/node.rb', line 20

def to_s
  if @size > 0
    "#{@name} (#{@size.to_s})"
  else
    @name
  end
end