Module: JRuby::ScalaSupport::Map::Common

Includes:
Common
Included in:
Immutable, Mutable
Defined in:
lib/jruby/scala_support.rb

Instance Method Summary collapse

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(options=nil)
  each_with_object({}) do |(key, value), hash|
    hash[key.as_json(options)] = value.as_json(options)
  end
end

#eachObject 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: ==

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

#keysObject



71
72
73
# File 'lib/jruby/scala_support.rb', line 71

def keys
  @raw.keys.toSeq.from_scala.to_a
end

#to_sObject 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?

Returns:

  • (Boolean)


79
80
81
# File 'lib/jruby/scala_support.rb', line 79

def value?(v)
  values.include?(v)
end

#valuesObject



75
76
77
# File 'lib/jruby/scala_support.rb', line 75

def values
  @raw.values.toSeq.from_scala.to_a
end