Sha256: 18a590ba1c8db958ed914e1acf073e27aa892ba4a9000a07738436e0196afb55

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require "jruby-coercion/version"
require 'thread'
require "java"

module Jruby
  module Coercion

    module Coercable
      def to_java(java_type = nil)
        return super if java_type.nil?
        converter = ::Jruby::Coercion::Registry.registry_converts_class_and_to?(self.class, java_type)

        if converter
          return converter.call(self, java_type)
        else
          super
        end
      end
    end

    class Registry
      class << self
        attr_reader :converter_registry
      end

      @converter_registry ||= Java::JavaUtilConcurrent::ConcurrentHashMap.new

      def self.register_converter(ruby_type, java_type, callable = nil, &blk)
        Thread.exclusive do 
          callable ||= blk
          ruby_type_converter = (@converter_registry[ruby_type] ||= 
                                 ::Java::JavaUtilConcurrent::ConcurrentHashMap.new)

          ruby_type_converter[java_type] = callable
          @converter_registry[ruby_type] = ruby_type_converter
        end
      end

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

      def self.registry_converts_class_and_to?(ruby_type, java_type)
        if (!java_type.nil? && 
            (class_level = registry_converts_class?(ruby_type)) &&
            (converter = class_level[java_type]))

          return converter
        else
          false
        end
      end

    end
  end
end

require 'jruby-coercion/numeric'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jruby-coercion-0.0.1-java lib/jruby-coercion.rb