Class: JrubyCoercion::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby_coercion/registry.rb

Direct Known Subclasses

JrubyCoercion::RubyToJava::Registry

Constant Summary collapse

REGISTRY_MUTEX =
::Mutex.new
DEFAULT_KEY =
"JRUBY_COERCION_DEFAULT".freeze

Class Method Summary collapse

Class Method Details

.alternative_class(java_type) ⇒ Object

Class Methods



13
14
15
16
17
18
19
# File 'lib/jruby_coercion/registry.rb', line 13

def self.alternative_class(java_type)
  if ::JrubyCoercion.native_type?(java_type)
    return java_type.ruby_class
  else
    return java_type.java_class
  end
end

.converter_registryObject



21
22
23
# File 'lib/jruby_coercion/registry.rb', line 21

def self.converter_registry
  @converter_registry ||= Java::JavaUtilConcurrent::ConcurrentHashMap.new
end

.new_registry_entry_for_type(from_type) ⇒ Object



25
26
27
# File 'lib/jruby_coercion/registry.rb', line 25

def self.new_registry_entry_for_type(from_type)
  raise "new_registry_entry_for_type must be overridden in child classes"
end

.register_converter(from_type, to_type, callable = nil, &blk) ⇒ Object Also known as: register_conversion, register_coercion



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jruby_coercion/registry.rb', line 29

def self.register_converter(from_type, to_type, callable = nil, &blk)
  REGISTRY_MUTEX.synchronize do
    callable ||= blk
    to_type ||= DEFAULT_KEY

    from_type_converter = (converter_registry[from_type] ||= new_registry_entry_for_type(from_type))

    from_type_converter[to_type] = callable
    # Register alternative type java_class/ruby_class
    from_type_converter[self.alternative_class(to_type)] = callable unless to_type == DEFAULT_KEY

    converter_registry[from_type] = from_type_converter
  end
end

.registry_converts_class?(convert_type) ⇒ Boolean Also known as: registry_converts_type?

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/jruby_coercion/registry.rb', line 44

def self.registry_converts_class?(convert_type)
  if !convert_type.nil? && (class_level = converter_registry[convert_type])
    return class_level
  else
    false
  end
end

.registry_converts_class_and_to?(from_type, to_type) ⇒ Boolean Also known as: registry_converts_type_and_to?

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/jruby_coercion/registry.rb', line 52

def self.registry_converts_class_and_to?(from_type, to_type)
  if (class_level = registry_converts_class?(from_type))
    to_type ||= DEFAULT_KEY
    return ::JrubyCoercion::Converter.new(class_level[to_type])
  else
    return ::JrubyCoercion::Converter.new(false)
  end
end