summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/isode/stroke/base/IDGenerator.java32
-rw-r--r--src/com/isode/stroke/base/SimpleIDGenerator.java56
-rw-r--r--test/com/isode/stroke/base/IDGeneratorTest.java49
-rw-r--r--test/com/isode/stroke/base/SimpleIDGeneratorTest.java49
4 files changed, 186 insertions, 0 deletions
diff --git a/src/com/isode/stroke/base/IDGenerator.java b/src/com/isode/stroke/base/IDGenerator.java
new file mode 100644
index 0000000..d08b8fa
--- /dev/null
+++ b/src/com/isode/stroke/base/IDGenerator.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010-2011 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.base;
+
+import java.util.UUID;
+
+public class IDGenerator {
+
+ /**
+ * IDGenerator();
+ */
+ public IDGenerator() {
+
+ }
+
+ /**
+ * Randomly generates a UUID.
+ * @return String representation of the UUID, which will never be null.
+ */
+ public static String generateID() {
+ return UUID.randomUUID().toString();
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/base/SimpleIDGenerator.java b/src/com/isode/stroke/base/SimpleIDGenerator.java
new file mode 100644
index 0000000..4242a87
--- /dev/null
+++ b/src/com/isode/stroke/base/SimpleIDGenerator.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2010-2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.base;
+
+import com.isode.stroke.base.IDGenerator;
+
+/**
+ * @brief The SimpleIDGenerator class implements a IDGenerator generating consecutive ID strings from
+ * the lower case latin alphabet.
+ */
+public class SimpleIDGenerator extends IDGenerator {
+
+ private static String currentID;
+
+ /**
+ * Constructor
+ */
+ public SimpleIDGenerator() {
+ currentID = "";
+ }
+
+ /**
+ * Simply generates a UUID.
+ * @return a String which will never be null or empty.
+ */
+ public static String generateID() {
+ boolean carry = true;
+ int i = 0;
+ char[] char_currentID = currentID.toCharArray();
+ while (carry && i < char_currentID.length) {
+ char c = char_currentID[i];
+ if (c >= 'z') {
+ char_currentID[i] = 'a';
+ }
+ else {
+ char_currentID[i] = (char)(c+1);
+ carry = false;
+ }
+ ++i;
+ }
+ currentID = String.valueOf(char_currentID);
+ if (carry) {
+ currentID += 'a';
+ }
+ return currentID;
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/base/IDGeneratorTest.java b/test/com/isode/stroke/base/IDGeneratorTest.java
new file mode 100644
index 0000000..34d8cc3
--- /dev/null
+++ b/test/com/isode/stroke/base/IDGeneratorTest.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.base;
+
+import static org.junit.Assert.assertTrue;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import com.isode.stroke.base.IDGenerator;
+import java.util.*;
+
+public class IDGeneratorTest {
+
+ private Set<String> generatedIDs_ = new HashSet<String>();
+
+ /**
+ * Constructor
+ */
+ public IDGeneratorTest() {
+
+ }
+
+ /**
+ * Clears the set generatedIDs_.
+ */
+ void setUp() {
+ generatedIDs_.clear();
+ }
+
+ /**
+ * Tests randomly generated UUID functionality
+ */
+ @Test
+ public void testGenerate() {
+ IDGenerator testling = new IDGenerator();
+ for(int i = 0; i < 26*4; ++i) {
+ String id = testling.generateID();
+ assertTrue(generatedIDs_.add(id));
+ }
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/base/SimpleIDGeneratorTest.java b/test/com/isode/stroke/base/SimpleIDGeneratorTest.java
new file mode 100644
index 0000000..d4c021d
--- /dev/null
+++ b/test/com/isode/stroke/base/SimpleIDGeneratorTest.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.base;
+
+import static org.junit.Assert.assertTrue;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import com.isode.stroke.base.SimpleIDGenerator;
+import java.util.*;
+
+public class SimpleIDGeneratorTest {
+
+ private Set<String> generatedIDs_ = new HashSet<String>();
+
+ /**
+ * Constructor
+ */
+ public SimpleIDGeneratorTest() {
+
+ }
+
+ /**
+ * Clears the set generatedIDs_.
+ */
+ void setUp() {
+ generatedIDs_.clear();
+ }
+
+ /**
+ * Tests randomly generated UUID functionality
+ */
+ @Test
+ public void testGenerate() {
+ SimpleIDGenerator testling = new SimpleIDGenerator();
+ for(int i = 0; i < 26*4; ++i) {
+ String id = testling.generateID();
+ assertTrue(generatedIDs_.add(id));
+ }
+ }
+} \ No newline at end of file