Module: JactiveSupport::JavaExtensions::Map::Hash

Included in:
javajava::util::Map
Defined in:
lib/jactive_support/java_ext/map/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other_mod) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/jactive_support/java_ext/map/hash.rb', line 5

def self.included(other_mod)
  other_mod.send :alias_method, :has_key?, :contains_key?
  other_mod.send :alias_method, :key?, :has_key?
  other_mod.send :alias_method, :include?, :has_key?
  other_mod.send :alias_method, :member?, :has_key?
  other_mod.send :alias_method, :has_value?, :contains_value?
  other_mod.send :alias_method, :value?, :has_value?
  other_mod.send :alias_method, :update, :merge!
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/jactive_support/java_ext/map/hash.rb', line 15

def [](key)
  get(key)
end

#[]=(key, val) ⇒ Object



19
20
21
# File 'lib/jactive_support/java_ext/map/hash.rb', line 19

def []=(key,val)
  put(key,val)
end

#contains_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/jactive_support/java_ext/map/hash.rb', line 37

def contains_key?(key)
  containsKey(key)
end

#contains_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/jactive_support/java_ext/map/hash.rb', line 41

def contains_value?(value)
  containsValue(value)
end

#delete(key) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/jactive_support/java_ext/map/hash.rb', line 66

def delete(key)
  if block_given? && !key?(key)
    yield key
  else
    remove(key)
  end
end

#delete_if(&block) ⇒ Object



49
50
51
52
53
54
# File 'lib/jactive_support/java_ext/map/hash.rb', line 49

def delete_if(&block)
  entry_set.iterate do |it, entry|
    it.remove if yield(entry.key, entry.value)
  end
  self
end

#dupObject



160
161
162
# File 'lib/jactive_support/java_ext/map/hash.rb', line 160

def dup
  clone
end

#eachObject



23
24
25
26
27
28
# File 'lib/jactive_support/java_ext/map/hash.rb', line 23

def each
  entry_set.each do |e|
    yield [e.key, e.value]
  end
  self
end

#each_key(&block) ⇒ Object



56
57
58
59
# File 'lib/jactive_support/java_ext/map/hash.rb', line 56

def each_key(&block)
  key_set.each(&block)
  self
end

#each_pairObject



30
31
32
33
34
35
# File 'lib/jactive_support/java_ext/map/hash.rb', line 30

def each_pair
  entry_set.each do |e|
    yield e.key, e.value
  end
  self
end

#each_value(&block) ⇒ Object



61
62
63
64
# File 'lib/jactive_support/java_ext/map/hash.rb', line 61

def each_value(&block)
  values.each(&block)
  self
end

#fetch(key, default = nil) {|key| ... } ⇒ Object

Yields:

  • (key)

Raises:

  • (IndexError)


74
75
76
77
78
79
# File 'lib/jactive_support/java_ext/map/hash.rb', line 74

def fetch(key,default=nil)
  return get(key) if key?(key)
  raise IndexError unless default || block_given?
  return default if default
  yield key
end

#index(value) ⇒ Object



81
82
83
84
85
# File 'lib/jactive_support/java_ext/map/hash.rb', line 81

def index(value)
  ret = entry_set.find {|entry| entry.value == value}
  return nil unless ret
  ret.key
end

#invertObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jactive_support/java_ext/map/hash.rb', line 87

def invert
  ret = nil
  begin
    ret = dup
    ret.clear
    ret
  rescue
    ret = ::Hash.new
  end
  each_pair {|key,value| ret[value] = key}
  ret
end

#keysObject



100
101
102
# File 'lib/jactive_support/java_ext/map/hash.rb', line 100

def keys
  key_set.to_a
end

#lengthObject



45
46
47
# File 'lib/jactive_support/java_ext/map/hash.rb', line 45

def length
  size
end

#merge(other_hash, &block) ⇒ Object



104
105
106
# File 'lib/jactive_support/java_ext/map/hash.rb', line 104

def merge(other_hash,&block)
  dup.merge!(other_hash,&block)
end

#merge!(other_hash) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jactive_support/java_ext/map/hash.rb', line 108

def merge!(other_hash)
  if block_given?
    other_hash.each_pair do |key, newval|
      if has_key?(key)
        oldval = get(key)
        newval = yield(key,oldval,newval)
      end
      put(key, newval)
    end
  else
    if other_hash.respond_to? :to_java_map
      other_hash = other_hash.to_java_map
    elsif other_hash.respond_to? :to_hash
      other_hash = other_hash.to_hash
    end
    put_all(other_hash)
  end
  self
end

#reject(&block) ⇒ Object



134
135
136
# File 'lib/jactive_support/java_ext/map/hash.rb', line 134

def reject(&block)
  dup.delete_if(&block)
end

#reject!Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/jactive_support/java_ext/map/hash.rb', line 138

def reject!
  ret = nil
  entry_set.iterate do |it, entry|
    if yield(entry.key, entry.value)
      it.remove
      ret = self
    end
  end
  ret
end

#replace(other_hash) ⇒ Object



149
150
151
152
# File 'lib/jactive_support/java_ext/map/hash.rb', line 149

def replace(other_hash)
  clear
  merge!(other_hash)
end

#to_hashObject



128
129
130
131
132
# File 'lib/jactive_support/java_ext/map/hash.rb', line 128

def to_hash
  ret = ::Hash.new
  each_pair {|key,value| ret[key] = value}
  ret
end

#values_at(*keys) ⇒ Object



154
155
156
157
158
# File 'lib/jactive_support/java_ext/map/hash.rb', line 154

def values_at(*keys)
  keys.inject([]) do |memo,key|
    memo.push(get(key))
  end
end