Skip to content

Commit

Permalink
[jid] Add Jid.isStrictParentOf(Jid)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowdalic committed Oct 24, 2024
1 parent 0d474a0 commit b313e12
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 6 deletions.
67 changes: 66 additions & 1 deletion jxmpp-jid/src/main/java/org/jxmpp/jid/Jid.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2020 Florian Schmaus
* Copyright © 2014-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -382,6 +382,71 @@ public interface Jid extends Comparable<Jid>, CharSequence, Serializable {
*/
boolean isParentOf(DomainFullJid domainFullJid);

/**
* Check if this JID is the strict parent of another JID. In other words, all parts of this JID must
* exist on the other JID, and match this JID's values. Furthermore, and this is what makes this
* method different from {@link #isParentOf(Jid)}, the other JID must have one additional part,
* that this JID does not have. The <b>parent of</b> relation is defined, under the
* precondition that the JID parts (localpart, domainpart and resourcepart) are equal, as follows:
* <pre>
* | this JID | other JID | result |
* |---------------------+---------------------+--------|
* | dom.example | dom.example | false | (different from isParentOf)
* | dom.example | dom.example/res | true |
* | dom.example | [email protected] | true |
* | dom.example | [email protected]/res | true |
* | dom.example/res | dom.exmple | false |
* | dom.example/res | dom.example/res | false | (different from isParentOf)
* | dom.example/res | [email protected] | false |
* | dom.example/res | [email protected]/res | false |
* | [email protected] | dom.example | false |
* | [email protected] | dom.example/res | false |
* | [email protected] | [email protected] | false | (different from isParentOf)
* | [email protected] | [email protected]/res | true |
* | [email protected]/res | dom.example | false |
* | [email protected]/res | dom.example/res | false |
* | [email protected]/res | [email protected] | false |
* | [email protected]/res | [email protected]/res | false | (different from isParentOf)
* </pre>
*
* @param jid
* the other JID to compare with
* @return true if this JID is a parent of the given JID.
*/
boolean isStrictParentOf(Jid jid);

/**
* See {@link #isParentOf(Jid)}.
*
* @param bareJid the bare JID.
* @return true if this JID is a parent of the given JID.
*/
boolean isStrictParentOf(EntityBareJid bareJid);

/**
* See {@link #isStrictParentOf(Jid)}.
*
* @param fullJid the full JID.
* @return true if this JID is a parent of the given JID.
*/
boolean isStrictParentOf(EntityFullJid fullJid);

/**
* See {@link #isStrictParentOf(Jid)}.
*
* @param domainBareJid the domain bare JID.
* @return true if this JID is a parent of the given JID.
*/
boolean isStrictParentOf(DomainBareJid domainBareJid);

/**
* See {@link #isStrictParentOf(Jid)}.
*
* @param domainFullJid the domain full JID.
* @return true if this JID is a parent of the given JID.
*/
boolean isStrictParentOf(DomainFullJid domainFullJid);

/**
* Return the downcasted instance of this Jid. This method is unsafe, make sure to check that this is actually of the type of are casting to.
*
Expand Down
18 changes: 18 additions & 0 deletions jxmpp-jid/src/main/java/org/jxmpp/jid/impl/AbstractJid.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ public final boolean isParentOf(Jid jid) {
return isParentOf(jid.asDomainBareJid());
}

@Override
public final boolean isStrictParentOf(Jid jid) {
EntityFullJid fullJid = jid.asEntityFullJidIfPossible();
if (fullJid != null) {
return isStrictParentOf(fullJid);
}
EntityBareJid bareJid = jid.asEntityBareJidIfPossible();
if (bareJid != null) {
return isStrictParentOf(bareJid);
}
DomainFullJid domainFullJid = jid.asDomainFullJidIfPossible();
if (domainFullJid != null) {
return isStrictParentOf(domainFullJid);
}

return isStrictParentOf(jid.asDomainBareJid());
}

@Override
public final int hashCode() {
return toString().hashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2019 Florian Schmaus
* Copyright © 2014-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,6 +115,27 @@ public boolean isParentOf(DomainFullJid domainFullJid) {
return domainBareJid.equals(domainFullJid.getDomain()) && resource.equals(domainFullJid.getResourcepart());
}

@Override
public boolean isStrictParentOf(EntityBareJid bareJid) {
return false;
}

@Override
public boolean isStrictParentOf(EntityFullJid fullJid) {
return false;
}

@Override
public boolean isStrictParentOf(DomainBareJid domainBareJid) {
return false;
}

@Override
public boolean isStrictParentOf(DomainFullJid domainFullJid) {
// A DomainFullJid can never be the strict parent of another DomainFullJid.
return false;
}

@Override
public Resourcepart getResourcepart() {
return resource;
Expand Down
22 changes: 21 additions & 1 deletion jxmpp-jid/src/main/java/org/jxmpp/jid/impl/DomainpartJid.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2021 Florian Schmaus
* Copyright © 2014-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,6 +114,26 @@ public boolean isParentOf(DomainFullJid domainFullJid) {
return domain.equals(domainFullJid.getDomain());
}

@Override
public boolean isStrictParentOf(EntityBareJid bareJid) {
return isParentOf(bareJid);
}

@Override
public boolean isStrictParentOf(EntityFullJid fullJid) {
return isParentOf(fullJid);
}

@Override
public boolean isStrictParentOf(DomainBareJid domainBareJid) {
return false;
}

@Override
public boolean isStrictParentOf(DomainFullJid domainFullJid) {
return isParentOf(domainFullJid);
}

@Override
public BareJid asBareJid() {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2019 Florian Schmaus
* Copyright © 2014-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -110,6 +110,26 @@ public boolean isParentOf(DomainFullJid domainFullJid) {
return false;
}

@Override
public boolean isStrictParentOf(EntityBareJid bareJid) {
return false;
}

@Override
public boolean isStrictParentOf(EntityFullJid fullJid) {
return isParentOf(fullJid);
}

@Override
public boolean isStrictParentOf(DomainBareJid domainBareJid) {
return false;
}

@Override
public boolean isStrictParentOf(DomainFullJid domainFullJid) {
return false;
}

@Override
public DomainBareJid asDomainBareJid() {
return domainBareJid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2019 Florian Schmaus
* Copyright © 2014-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -128,6 +128,26 @@ public boolean isParentOf(DomainFullJid domainFullJid) {
return false;
}

@Override
public boolean isStrictParentOf(EntityBareJid bareJid) {
return false;
}

@Override
public boolean isStrictParentOf(EntityFullJid fullJid) {
return false;
}

@Override
public boolean isStrictParentOf(DomainBareJid domainBareJid) {
return false;
}

@Override
public boolean isStrictParentOf(DomainFullJid domainFullJid) {
return false;
}

@Override
public DomainBareJid asDomainBareJid() {
return bareJid.asDomainBareJid();
Expand Down
30 changes: 29 additions & 1 deletion jxmpp-jid/src/test/java/org/jxmpp/jid/JidTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014 Florian Schmaus
* Copyright © 2014-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,34 @@ public void testJidIsParentOf() throws XmppStringprepException {
assertTrue(fullJid.isParentOf(fullJid));
}

@Test
public void testJidIsStrictParentOf() throws XmppStringprepException {
final Jid domainBareJid = JidCreate.from("dom.example");
final Jid domainFullJid = JidCreate.from("dom.example/res");
final Jid bareJid = JidCreate.from("[email protected]");
final Jid fullJid = JidCreate.from("[email protected]/res");

assertFalse(domainBareJid.isStrictParentOf(domainBareJid)); // different from isParentOf
assertTrue(domainBareJid.isStrictParentOf(domainFullJid));
assertTrue(domainBareJid.isStrictParentOf(bareJid));
assertTrue(domainBareJid.isStrictParentOf(fullJid));

assertFalse(domainFullJid.isStrictParentOf(domainBareJid));
assertFalse(domainFullJid.isStrictParentOf(domainFullJid)); // different from isParentOf
assertFalse(domainFullJid.isStrictParentOf(bareJid));
assertFalse(domainFullJid.isStrictParentOf(fullJid));

assertFalse(bareJid.isStrictParentOf(domainBareJid));
assertFalse(bareJid.isStrictParentOf(domainFullJid));
assertFalse(bareJid.isStrictParentOf(bareJid)); // different from isParentOf
assertTrue(bareJid.isStrictParentOf(fullJid));

assertFalse(fullJid.isStrictParentOf(domainBareJid));
assertFalse(fullJid.isStrictParentOf(domainFullJid));
assertFalse(fullJid.isStrictParentOf(bareJid));
assertFalse(fullJid.isStrictParentOf(fullJid)); // different from isParentOf
}

@Test
public void stripFinalDot() throws XmppStringprepException {
String domain = "foo.bar.";
Expand Down

0 comments on commit b313e12

Please sign in to comment.