Skip to content

Commit

Permalink
[jid] Avoid creating a StringBuilder if possible
Browse files Browse the repository at this point in the history
JidCreate.from(String) would previously always invoke
XmppStringUtils.completeJidFrom() which internally creates a new
StringBuilder. This can be avoided.
  • Loading branch information
Flowdalic committed Jan 18, 2024
1 parent 55a98af commit 71e8fd2
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions jxmpp-jid/src/main/java/org/jxmpp/jid/impl/JidCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,31 @@ public static Jid from(String localpart, String domainpart, String resource) thr
* @throws XmppStringprepException if an error occurs.
*/
public static Jid from(String localpart, String domainpart, String resource, JxmppContext context) throws XmppStringprepException {
String jid = XmppStringUtils.completeJidFrom(localpart, domainpart, resource);
return from(localpart, domainpart, resource, jid, context);
}

/**
* Get a {@link Jid} from the given parts.
*
* @param localpart a optional localpart.
* @param domainpart a required domainpart.
* @param resource a optional resourcepart.
* @param jidString the raw String of the as parsed.
* @param context the JXMPP context.
* @return a JID which consists of the given parts.
* @throws XmppStringprepException if an error occurs.
*/
private static Jid from(String localpart, String domainpart, String resource, String jidString, JxmppContext context) throws XmppStringprepException {
// Every JID must come with an domainpart.
if (domainpart.isEmpty()) {
throw XmppStringprepException.MissingDomainpart.from(localpart, resource);
}

String jidString = XmppStringUtils.completeJidFrom(localpart, domainpart, resource);
// The provided jidString must be equal to the assembled parts.
assert jidString.equals(XmppStringUtils.completeJidFrom(localpart, domainpart, resource));

Jid jid;

JidStringAndStringprep jidStringAndStringprep = null;
Expand Down Expand Up @@ -245,7 +264,7 @@ public static Jid from(String jidString, JxmppContext context) throws XmppString
String domainpart = XmppStringUtils.parseDomain(jidString);
String resource = XmppStringUtils.parseResource(jidString);
try {
return from(localpart, domainpart, resource, context);
return from(localpart, domainpart, resource, jidString, context);
} catch (XmppStringprepException e) {
throw new XmppStringprepException(jidString, e);
}
Expand Down

0 comments on commit 71e8fd2

Please sign in to comment.