Skip to content

Commit

Permalink
jxmpp-jid: Add fromOrNull() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowdalic committed Apr 25, 2018
1 parent 9c1a4b2 commit acafc29
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
168 changes: 168 additions & 0 deletions jxmpp-jid/src/main/java/org/jxmpp/jid/impl/JidCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ public static Jid from(String jidString) throws XmppStringprepException {
}
}

/**
* Get a {@link Jid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static Jid fromOrNull(CharSequence cs) {
try {
return from(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #fromUnescaped(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -213,6 +227,20 @@ public static Jid fromUnescaped(String unescapedJidString) throws XmppStringprep
}
}

/**
* Get a {@link Jid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static Jid fromUnescapedOrNull(CharSequence cs) {
try {
return fromUnescaped(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #bareFrom(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -296,6 +324,20 @@ public static BareJid bareFrom(Localpart localpart, Domainpart domain) {
}
}

/**
* Get a {@link BareJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static BareJid bareFromOrNull(CharSequence cs) {
try {
return bareFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #fullFrom(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -397,6 +439,20 @@ public static FullJid fullFrom(Localpart localpart, Domainpart domainpart, Resou
return fullFrom(entityBareFrom(localpart, domainpart), resource);
}

/**
* Get a {@link FullJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static FullJid fullFromOrNull(CharSequence cs) {
try {
return fullFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #entityFrom(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -488,6 +544,20 @@ public static EntityJid entityFromUnescaped(String jidString) throws XmppStringp
return entityFrom(jidString, true);
}

/**
* Get a {@link EntityJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static EntityJid entityFromUnesacpedOrNull(CharSequence cs) {
try {
return entityFromUnescaped(cs.toString());
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Get a {@link EntityJid} representing the given String.
*
Expand Down Expand Up @@ -541,6 +611,20 @@ private static EntityJid entityFrom(String jidString, boolean unescaped) throws
return entityJid;
}

/**
* Get a {@link EntityJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static EntityJid entityFromOrNull(CharSequence cs) {
try {
return entityFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #entityBareFrom(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -650,6 +734,20 @@ public static EntityBareJid entityBareFromUnescaped(String unescapedJidString) t
return bareJid;
}

/**
* Get a {@link EntityBareJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static EntityBareJid entityBareFromUnescapedOrNull(CharSequence cs) {
try {
return entityBareFromUnescaped(cs.toString());
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Get a {@link EntityBareJid} constructed from the given {@link Localpart} and {link DomainBareJid}.
*
Expand All @@ -672,6 +770,20 @@ public static EntityBareJid entityBareFrom(Localpart localpart, Domainpart domai
return new LocalAndDomainpartJid(localpart, domain);
}

/**
* Get a {@link EntityBareJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static EntityBareJid entityBareFromOrNull(CharSequence cs) {
try {
return entityBareFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #entityFullFrom(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -726,6 +838,20 @@ public static EntityFullJid entityFullFrom(String jid) throws XmppStringprepExce
return fullJid;
}

/**
* Get a {@link EntityFullJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static EntityFullJid entityFullFromOrNull(CharSequence cs) {
try {
return entityFullFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #entityFullFromUnescaped(CharSequence)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down Expand Up @@ -784,6 +910,20 @@ public static EntityFullJid entityFullFromUnescaped(String unescapedJidString) t
return fullJid;
}

/**
* Get a {@link EntityFullJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static EntityFullJid entityFullFromUnescapedOrNull(CharSequence cs) {
try {
return entityFullFromUnescaped(cs.toString());
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Get a {@link EntityFullJid} constructed from the given parts.
*
Expand Down Expand Up @@ -912,6 +1052,20 @@ public static DomainBareJid domainBareFrom(Domainpart domainpart) {
return new DomainpartJid(domainpart);
}

/**
* Get a {@link DomainBareJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static DomainBareJid domainBareFromOrNull(CharSequence cs) {
try {
return domainBareFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Deprecated.
*
Expand Down Expand Up @@ -1012,4 +1166,18 @@ public static DomainFullJid domainFullFrom(Domainpart domainpart, Resourcepart r
public static DomainFullJid domainFullFrom(DomainBareJid domainBareJid, Resourcepart resource) {
return new DomainAndResourcepartJid(domainBareJid, resource);
}

/**
* Get a {@link DomainFullJid} from a given {@link CharSequence} or {@code null} if the input does not represent a JID.
*
* @param cs the input {@link CharSequence}
* @return a JID or {@code null}
*/
public static DomainFullJid domainFullFromOrNull(CharSequence cs) {
try {
return domainFullFrom(cs);
} catch (XmppStringprepException e) {
return null;
}
}
}
14 changes: 14 additions & 0 deletions jxmpp-jid/src/main/java/org/jxmpp/jid/parts/Domainpart.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ private Domainpart(String domain) {
super(domain);
}

/**
* Get a {@link Domainpart} from a given {@link CharSequence} or {@code null} if the input is not a valid domainpart.
*
* @param cs the input CharSequence
* @return a Domainpart or {@code null}
*/
public static Domainpart fromOrNull(CharSequence cs) {
try {
return from(cs.toString());
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #from(String)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down
28 changes: 28 additions & 0 deletions jxmpp-jid/src/main/java/org/jxmpp/jid/parts/Localpart.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ public static Localpart fromUnescapedOrThrowUnchecked(CharSequence cs) {
}
}

/**
* Get a {@link Localpart} from a given {@link CharSequence} or {@code null} if the input is not a valid localpart.
*
* @param cs the input CharSequence
* @return a Localpart or {@code null}
*/
public static Localpart formUnescapedOrNull(CharSequence cs) {
try {
return fromUnescaped(cs);
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Get a {@link Localpart} from an unescaped String.
*
Expand All @@ -126,6 +140,20 @@ public static Localpart fromUnescaped(CharSequence unescapedLocalpart) throws Xm
return fromUnescaped(unescapedLocalpart.toString());
}

/**
* Get a {@link Localpart} from a given {@link CharSequence} or {@code null} if the input is not a valid localpart.
*
* @param cs the input CharSequence
* @return a Localpart or {@code null}
*/
public static Localpart fromOrNull(CharSequence cs) {
try {
return from(cs.toString());
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Get the {@link Localpart} representing the input String.
*
Expand Down
14 changes: 14 additions & 0 deletions jxmpp-jid/src/main/java/org/jxmpp/jid/parts/Resourcepart.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ private Resourcepart(String resource) {
super(resource);
}

/**
* Get a {@link Resourcepart} from a given {@link CharSequence} or {@code null} if the input is not a valid resourcepart.
*
* @param cs the input CharSequence
* @return a Resourcepart or {@code null}
*/
public static Resourcepart fromOrNull(CharSequence cs) {
try {
return from(cs.toString());
} catch (XmppStringprepException e) {
return null;
}
}

/**
* Like {@link #from(String)} but does throw an unchecked {@link IllegalArgumentException} instead of a
* {@link XmppStringprepException}.
Expand Down

0 comments on commit acafc29

Please sign in to comment.