Skip to content

Commit bb7a7da

Browse files
author
Lloyd Watkin
committed
Add additional isGlobalId() method to GlobalItemIDImpl
1 parent 9cfcc8a commit bb7a7da

3 files changed

Lines changed: 75 additions & 39 deletions

File tree

src/main/java/org/buddycloud/channelserver/pubsub/model/GlobalItemID.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@
33
import org.xmpp.packet.JID;
44

55
/**
6-
* Pointer to a globally identifiable node item, using a service JID, node ID and itemID.
7-
* The Item is unique within the Node
8-
* The Node is unique within the Service
9-
* The Service is unique within the network.
6+
* Pointer to a globally identifiable node item, using a service JID, node ID
7+
* and itemID. The Item is unique within the Node The Node is unique within the
8+
* Service The Service is unique within the network.
109
*/
1110
public interface GlobalItemID {
1211
/**
1312
* Gets the JID of service which hosts the node (and item)
13+
*
1414
* @return the JID of the service.
1515
*/
1616
public JID getService();
17-
17+
1818
/**
1919
* Gets the NodeID of the node containing the item
20+
*
2021
* @return the NodeID of the node.
2122
*/
2223
public String getNodeID();
23-
24+
2425
/**
2526
* Gets the ItemID of the item within the Node
27+
*
2628
* @return the ItemID if the item.
2729
*/
2830
public String getItemID();

src/main/java/org/buddycloud/channelserver/pubsub/model/impl/GlobalItemIDImpl.java

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
*/
1212
public class GlobalItemIDImpl implements GlobalItemID {
1313

14-
private static final Pattern STRING_PATTERN = Pattern.compile("^tag:([^,]+),([^,]+),([^,]+)$");
15-
14+
private static final Pattern STRING_PATTERN = Pattern
15+
.compile("^tag:([^,]+),([^,]+),([^,]+)$");
16+
1617
/**
1718
* Creates a new global item ID
18-
* @param service the JID of the pubsub service hosting the node
19-
* @param nodeID the id of the node containing the item
20-
* @param itemID the id of the item
19+
*
20+
* @param service
21+
* the JID of the pubsub service hosting the node
22+
* @param nodeID
23+
* the id of the node containing the item
24+
* @param itemID
25+
* the id of the item
2126
*/
2227
public GlobalItemIDImpl(JID service, String nodeID, String itemID) {
2328
this.service = service;
@@ -28,26 +33,26 @@ public GlobalItemIDImpl(JID service, String nodeID, String itemID) {
2833
private JID service;
2934
private String nodeID;
3035
private String itemID;
31-
36+
3237
@Override
3338
public JID getService() {
3439
return service;
3540
}
36-
41+
3742
@Override
3843
public String getNodeID() {
3944
return nodeID;
4045
}
41-
46+
4247
@Override
4348
public String getItemID() {
4449
return itemID;
4550
}
46-
51+
4752
@Override
4853
public String toString() {
4954
StringBuilder builder = new StringBuilder();
50-
55+
5156
if (service != null) {
5257
builder.append("tag:");
5358
builder.append(service.toString());
@@ -56,40 +61,46 @@ public String toString() {
5661
builder.append(nodeID);
5762
builder.append(",");
5863
builder.append(itemID);
59-
64+
6065
return builder.toString();
6166
}
62-
67+
6368
/**
64-
* Creates a new {@link GlobalItemID} from a tag string which looks a bit like:
65-
* <blockquote><code>tag:pubsub.server.com,a/node/id,an/item/id</code></blockquote>
66-
* @param str The tag string
69+
* Creates a new {@link GlobalItemID} from a tag string which looks a bit
70+
* like: <blockquote><code>tag:pubsub.server.com,a/node/id,an/item/id</code>
71+
* </blockquote>
72+
*
73+
* @param str
74+
* The tag string
6775
* @return the {@link GlobalItemID}
6876
*/
6977
public static GlobalItemID fromString(final String str) {
7078
Matcher matcher = STRING_PATTERN.matcher(str);
71-
72-
if(!matcher.matches()) {
73-
throw new IllegalArgumentException(str + " is not a valid GlobalItemID String. Expected something like 'tag:example.com,node,item'.");
79+
80+
if (!matcher.matches()) {
81+
throw new IllegalArgumentException(
82+
str
83+
+ " is not a valid GlobalItemID String. Expected something like 'tag:example.com,node,item'.");
7484
}
75-
85+
7686
JID service = new JID(matcher.group(1));
77-
87+
7888
// Handle an issue with certain clients
79-
if((service.getNode() != null) && service.getNode().equals("null")) {
89+
if ((service.getNode() != null) && service.getNode().equals("null")) {
8090
service = new JID(null, service.getDomain(), service.getResource());
8191
}
82-
92+
8393
return new GlobalItemIDImpl(service, matcher.group(2), matcher.group(3));
8494
}
85-
95+
8696
/**
8797
*
8898
*/
8999
public static GlobalItemID fromBuddycloudString(String itemId) {
90100
String[] splittedItemId = itemId.split(",");
91101
if (splittedItemId.length < 2) {
92-
throw new IllegalArgumentException("Illegal format for buddycloud global id");
102+
throw new IllegalArgumentException(
103+
"Illegal format for buddycloud global id");
93104
}
94105
if (splittedItemId.length == 3) {
95106
return fromString(itemId);
@@ -108,7 +119,7 @@ public static String toLocalId(String itemId) {
108119
}
109120
return fromBuddycloudString(itemId).getItemID();
110121
}
111-
122+
112123
@Override
113124
public int hashCode() {
114125
final int prime = 31;
@@ -145,4 +156,13 @@ public boolean equals(Object obj) {
145156
return false;
146157
return true;
147158
}
159+
160+
public static boolean isGlobalId(String id) {
161+
Matcher matcher = STRING_PATTERN.matcher(id);
162+
163+
if (!matcher.matches()) {
164+
return false;
165+
}
166+
return true;
167+
}
148168
}

src/test/java/org/buddycloud/channelserver/pubsub/model/impl/GlobalItemIDImplTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.buddycloud.channelserver.pubsub.model.impl;
22

33
import static org.junit.Assert.*;
4+
import junit.framework.Assert;
45

56
import org.buddycloud.channelserver.pubsub.model.GlobalItemID;
67
import org.junit.After;
@@ -19,9 +20,10 @@ public void testFromStringWithValidStrings() {
1920
new JID("test@pubsub.server.com"),
2021
"some kind of RANDOM node ID", "4545nnn342300-dsvads=f/fdsafh");
2122
}
22-
23+
2324
/**
24-
* This is to handle a case with existing buddycloud clients which send the service as <code>"null@&lt;server&gt;"</code>
25+
* This is to handle a case with existing buddycloud clients which send the
26+
* service as <code>"null@&lt;server&gt;"</code>
2527
*/
2628
@Test
2729
public void testFromStringWithNullNodeInService() {
@@ -30,23 +32,34 @@ public void testFromStringWithNullNodeInService() {
3032
"this-in-an-item");
3133
}
3234

33-
@Test(expected=IllegalArgumentException.class)
35+
@Test(expected = IllegalArgumentException.class)
3436
public void testFromStringWithInvalidString() {
3537
GlobalItemIDImpl.fromString("this isn't valid");
3638
}
3739

38-
@Test(expected=IllegalArgumentException.class)
40+
@Test(expected = IllegalArgumentException.class)
3941
public void testFromStringWithMissingTag() {
40-
GlobalItemIDImpl.fromString("pubsub.server.com,this/is/a/node,this-in-an-item");
42+
GlobalItemIDImpl
43+
.fromString("pubsub.server.com,this/is/a/node,this-in-an-item");
4144
}
4245

4346
@Test
4447
public void checkEqualsABit() {
45-
GlobalItemID itemID = new GlobalItemIDImpl(new JID("denmark.lit"), "node", "item-id");
46-
GlobalItemID itemID2 = new GlobalItemIDImpl(new JID("denmark.lit"), "node", "item-id");
47-
48+
GlobalItemID itemID = new GlobalItemIDImpl(new JID("denmark.lit"),
49+
"node", "item-id");
50+
GlobalItemID itemID2 = new GlobalItemIDImpl(new JID("denmark.lit"),
51+
"node", "item-id");
52+
4853
assertEquals("Equals isn't working!", itemID, itemID2);
4954
}
55+
56+
@Test
57+
public void canTestForGlobalIdType() throws Exception {
58+
GlobalItemIDImpl id = new GlobalItemIDImpl(new JID("denmark.lit"),
59+
"node", "item-id");
60+
assertTrue(GlobalItemIDImpl.isGlobalId(id.toString()));
61+
assertFalse(GlobalItemIDImpl.isGlobalId(id.getItemID()));
62+
}
5063

5164
private void checkValid(String input, JID service, String nodeID,
5265
String itemID) {
@@ -56,4 +69,5 @@ private void checkValid(String input, JID service, String nodeID,
5669
assertEquals("Incorrect service", nodeID, result.getNodeID());
5770
assertEquals("Incorrect service", itemID, result.getItemID());
5871
}
72+
5973
}

0 commit comments

Comments
 (0)