class Universa::Ref
A reference to any Java-object that can call its methods like usual methods:
key = umi.instantiate "PrivateKey", 2048 # this returns Ref address = key.getPublicKey().getShortAddress().toString()
Notice that all methods called after key
are java methods of PrivateKey
, PublicKey
and KeyAddress
Java classes, whose references are created on-the-fly automatically (and will be reclaimed by GC on both ends soon).
Instances are unique¶ ↑
What means, if some calls will return the same Java object instance, it will be returned as the same {Ref} instance.
Reference
equality (== and !=)¶ ↑
References are equal if they refer the same objects OR their are equals
- the java.equals() is called to compare different referenced objects. Therefore, to compare two references use:
-
`==`: returns true if referencs are to the same object or different objects that where left.equals(right)
returns true.
-
`!=`: same as !(left == right)
-
`===`: thest that references refer to exactly same object instance. E.g. it is possible that
`left == right && !()left === right) - different objects which `equals()`.
Reference
lifespan
Each {Ref} has an allocated object in the remote side which is retained until explicetly freed by the proper UMI
call. {Ref} class rakes care of it: when the ruby Ref
instance is garbage collected, its remote counterpart will shortly receive drop command preventing memory leakage.
Arguments¶ ↑
you can use basic ruby objects as arguments: strings, numbers, arrays, hashes, and {Ref} instances too. Note that:
-
binary string (Encoding::Binary) are converted to byte[] iin Java side
-
utf8 strings are passed as strings.
Return value¶ ↑
Will be deep-converted to corresponding ruby objects: hashes, arrays, sets, numbers, strings and {Ref} instances as need. It is, generally, inverse of converting arguments covered above.
Constants
- LOCAL_METHODS
Public Class Methods
Create new reference. Do not call it directly: the {UMI} instance will do it in a correct order. @param [UMI] umi instance to bind to @param [Hash] ref UMI
reference structure
# File lib/universa/umi.rb, line 414 def initialize(umi, ref) @umi, @ref = umi, ref @id = ref.id end
Public Instance Methods
Checks
that references are euqal: either both point to the same remote object or respective remote objects are reported equals by the remote +equals()+ call.
# File lib/universa/umi.rb, line 462 def ==(other) (other.is_a?(Ref) || other.is_a?(RemoteAdapter)) && other._umi == @umi && (other._remote_id == @id || other.equals(self)) end
Equal references. Both point to the same remote object. Note that it should never happen with {UMI} class as it do cache non-recycled references and share them between calls.
# File lib/universa/umi.rb, line 469 def ===(other) other.is_a?(Ref) && other._umi == @umi && other._remote_id == @id end
name of the remote class (provided by remote)
# File lib/universa/umi.rb, line 430 def _remote_class_name @ref.className end
@return [Object] remote object id. Could be of any type actually.
# File lib/universa/umi.rb, line 425 def _remote_id @id end
@return [UMI] interface that this reference is bound to (and created by)
# File lib/universa/umi.rb, line 420 def _umi @umi end
short data label for instance
# File lib/universa/umi.rb, line 456 def inspect "<UMI:Ref:#{@umi.__id__}:#{@ref.className}:#{@id}>" end
Internal use only. Call remote method as needed. This is where all the magick comes from: it call remote method instead of the local one, exactly like it is local.
# File lib/universa/umi.rb, line 441 def method_missing(method_name, *args, &block) if method_name[0] == '_' super else @umi.invoke self, method_name, *args end end
Internal use only. Allow processing remote commands as local calls
# File lib/universa/umi.rb, line 435 def respond_to_missing?(method_name, include_private = false) method_name[0] == '_' || LOCAL_METHODS.include?(method_name) ? super : true end