indexing description: "Represents a JXTA ID" license: "MIT license (see ../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" deferred class P2P_ID inherit HASHABLE redefine out, is_equal end P2P_STRING_UTILS redefine out, is_equal end feature {NONE} -- Initialization make_from_urn (urn: STRING) is -- Create id from `urn' string require Urn_valid: urn /= Void do parse_urn (trimmed (urn)) if is_valid then build_urn end ensure Parsed: is_valid implies (unique_id /= Void and format /= Void) end make_with_id (an_id: STRING) is -- Create id with given string id require Id_valid: an_id /= Void and not an_id.is_empty Format_valid: format /= Void do if is_unique_id_valid (an_id) then unique_id := an_id is_valid := True build_urn end ensure Id_set: is_valid implies unique_id = an_id Valid_id: is_valid = is_unique_id_valid (an_id) end feature -- Access format: STRING is -- JXTA ID format deferred end unique_id: STRING hash_code: INTEGER is -- Hash code value do if out /= Void then Result := out.hash_code end end feature -- Status report is_valid: BOOLEAN is_unique_id_valid (an_id: STRING): BOOLEAN is -- Is `an_id' valid as a unique id? require Id_existent: an_id /= Void do Result := not an_id.is_empty end feature -- Comparison is_equal (other: like Current): BOOLEAN is -- Is `other' equal to current? do Result := out.is_equal (other.out) end feature -- Output out: STRING -- Cached string representation out_short: STRING -- Cached short string representation (format-unique id) feature {NONE} -- Implementation Colon: CHARACTER is ':' Dash: CHARACTER is '-' Urn_encoding: STRING is "urn" Jxta_namespace: STRING is "jxta" build_urn is -- Build string representation and set `out' require Valid_id: is_valid do out := urn_encoding + colon.out + jxta_namespace + colon.out + format + dash.out + unique_id out_short := format + dash.out + unique_id ensure Cache_valid: out /= Void and out_short /= Void end parse_urn (urn: STRING) is -- Try to parse a JXTA id from `urn'. require Source_valid: urn /= Void local pos, pos_dash: INTEGER parsed_format, parsed_id: STRING do pos := urn_encoding.count + 1 + jxta_namespace.count + 1 if urn.count > pos + 2 then pos_dash := urn.index_of (dash, pos + 2) -- format must be at least one char end if urn.substring (1, pos).as_lower.is_equal (urn_encoding + colon.out + jxta_namespace + colon.out) and pos_dash > 0 then parsed_format := urn.substring (pos + 1, pos_dash - 1) parsed_id := urn.substring (pos_dash + 1, urn.count) if is_unique_id_valid (parsed_id) then if format = Void then set_format (parsed_format) end if equal (format, parsed_format) then unique_id := parsed_id parse_unique_id end end end is_valid := (unique_id /= Void and format /= Void) ensure Status_set: is_valid implies (unique_id /= Void and format /= Void) end set_format (a_format: STRING) is -- The format normally can't be set. require Format_valid: a_format /= Void and not a_format.is_empty do end parse_unique_id is -- Do nothing. Redefine if necessary. do end end