indexing description: "Creates IDs" license: "MIT license (see ../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" class P2P_ID_CREATOR inherit P2P_STRING_UTILS P2P_UUID_TOOLS create make feature {NONE} -- Initialization make is -- Create id creator ;) do create custom_idformat_creators.make_default end feature -- Access null_id: P2P_NULL_ID is -- JXTA Null id indexing once_status: global once create Result.make end worldgroup_id: P2P_WORLDGROUP_ID is -- World group id indexing once_status: global once create Result.make end netgroup_id: P2P_NETGROUP_ID is -- Net group id indexing once_status: global once create Result.make end feature -- Element change extend_custom_creator (a_format: STRING; a_creator: FUNCTION [ANY, TUPLE [P2P_ID], P2P_ID]) is -- Register `a_creator' for ids with format `a_format' require Format_valid: a_format /= Void and not a_format.is_empty Creator_valid: a_creator /= Void do custom_idformat_creators.force (a_creator, a_format) end prune_custom_creator (a_format: STRING) is -- Unregister creator for ids with format `a_format' require Format_valid: a_format /= Void do custom_idformat_creators.remove (a_format) end prune_all_custom_creators is -- Unregister all custom creators do custom_idformat_creators.wipe_out end feature -- Basic operations create_from_urn (urn: STRING): P2P_ID is -- Parse `urn' and try to create an id require Urn_valid: urn /= Void local genid: P2P_GENERIC_ID creator: FUNCTION [ANY, TUPLE [P2P_ID], P2P_ID] do -- first create a generic id create genid.make_from_urn (urn) if genid.is_valid then -- custom ids (higher priority than standard ids) if custom_idformat_creators.has (genid.format) then -- call responsible creator for given format creator := custom_idformat_creators.item (genid.format) creator.call ([genid]) Result := creator.last_result end -- now try standard id classes unless Result is set already if Result = Void and genid.format.is_equal ("uuid") and genid.unique_id.count >= 2 then -- UUID format: detect type and try to instantiate specific id inspect hex_to_natural_8 (genid.unique_id.item (genid.unique_id.count - 1), genid.unique_id.item (genid.unique_id.count)) when {P2P_CODAT_ID}.flag_id_type then create {P2P_CODAT_ID} Result.make_from_urn (urn) when {P2P_PEERGROUP_UUID}.flag_id_type then create {P2P_PEERGROUP_UUID} Result.make_from_urn (urn) when {P2P_PEER_ID}.flag_id_type then create {P2P_PEER_ID} Result.make_from_urn (urn) when {P2P_PIPE_ID}.flag_id_type then create {P2P_PIPE_ID} Result.make_from_urn (urn) when {P2P_MODULE_CLASS_ID}.flag_id_type then create {P2P_MODULE_CLASS_ID} Result.make_from_urn (urn) when {P2P_MODULE_SPECIFICATION_ID}.flag_id_type then create {P2P_MODULE_SPECIFICATION_ID} Result.make_from_urn (urn) end elseif Result = Void and genid.format.is_equal ("jxta") then if genid.unique_id.is_equal (null_id.unique_id) then Result := null_id elseif genid.unique_id.is_equal (netgroup_id.unique_id) then Result := netgroup_id elseif genid.unique_id.is_equal (worldgroup_id.unique_id) then Result := worldgroup_id end end if Result = Void then Result := genid elseif Result /= Void and not Result.is_valid then Result := Void end end ensure Result_valid: Result /= Void implies Result.is_valid end create_peergroup_id (urn: STRING): P2P_PEERGROUP_ID is -- Parse `urn' and create peer group id require Urn_valid: urn /= Void do Result ?= create_from_urn (urn) ensure Result_valid: Result /= Void implies Result.is_valid end feature {NONE} -- Implementation custom_idformat_creators: DS_HASH_TABLE [FUNCTION [ANY, TUPLE [P2P_ID], P2P_ID], STRING] end