Module: JRuby::ScalaSupport::Map::Common
Instance Method Summary collapse
- #[](key) ⇒ Object
- #as_json(options = nil) ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #eql?(other) ⇒ Boolean (also: #==)
- #has_key?(key) ⇒ Boolean (also: #key?, #member?, #include?)
- #key(val) ⇒ Object
- #keys ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #value?(v) ⇒ Boolean (also: #has_value?)
- #values ⇒ Object
Instance Method Details
#[](key) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/jruby/scala_support.rb', line 62 def [](key) value = @raw.get(key) if value == None nil else value.get.from_scala end end |
#as_json(options = nil) ⇒ Object
127 128 129 130 131 |
# File 'lib/jruby/scala_support.rb', line 127 def as_json(=nil) each_with_object({}) do |(key, value), hash| hash[key.as_json()] = value.as_json() end end |
#each ⇒ Object Also known as: each_pair
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/jruby/scala_support.rb', line 98 def each if block_given? @raw.foreach do |tuple| yield tuple._1.from_scala, tuple._2.from_scala end else iterator = @raw.iterator Enumerator.new do |yielder| while iterator.hasNext tuple = iterator.next yielder << [tuple._1.from_scala, tuple._2.from_scala] end end end end |
#eql?(other) ⇒ Boolean Also known as: ==
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/jruby/scala_support.rb', line 133 def eql?(other) return true if self.equal? other unless other.kind_of? Hash return false unless other.respond_to? :to_hash return other.eql?(self) end return false unless other.size == size other.each do |k,v| return false unless (entry = real_map.send(:findEntry,k)) && (entry.key.from_scala.eql?(k)) && (item = entry.value) # Order of the comparison matters! We must compare our value with # the other Hash's value and not the other way around. return false unless item.eql?(v) end true end |
#has_key?(key) ⇒ Boolean Also known as: key?, member?, include?
85 86 87 |
# File 'lib/jruby/scala_support.rb', line 85 def has_key?(key) @raw.contains(key) end |
#key(val) ⇒ Object
93 94 95 96 |
# File 'lib/jruby/scala_support.rb', line 93 def key(val) kv = find {|_,v| v == val } kv && kv.first end |
#keys ⇒ Object
71 72 73 |
# File 'lib/jruby/scala_support.rb', line 71 def keys @raw.keys.toSeq.from_scala.to_a end |
#to_s ⇒ Object Also known as: inspect
117 118 119 120 121 122 123 |
# File 'lib/jruby/scala_support.rb', line 117 def to_s first = true each_with_object("{") do |(key, value), str| first ? first = false : str << ", " str << "#{key.inspect}=>#{value.inspect}" end << "}" end |
#value?(v) ⇒ Boolean Also known as: has_value?
79 80 81 |
# File 'lib/jruby/scala_support.rb', line 79 def value?(v) values.include?(v) end |
#values ⇒ Object
75 76 77 |
# File 'lib/jruby/scala_support.rb', line 75 def values @raw.values.toSeq.from_scala.to_a end |