// @TODO: Add Javadoc comments
-// @TODO: When auto expanding, we'll need to set a maximum length to which the
-// generated IDs can grow, likely as an additional parameter to be
+// @TODO: When auto expanding, we'll need to allow setting of a maximum length to
+// which the generated IDs can grow, likely as an additional parameter to be
// passed to a constructor, with a default value hard-coded in the class.
+// If that length is exceeded, nextID() should throw an IllegalStateException.
// @TODO: Consider handling escaped characters or sequences which represent Unicode
// code points, both in the start and end characters of the sequence, and in the initial value.
//
// See the TODOs at the top of this class for additional
// functionality that needs to be implemented.
- public synchronized String nextID() {
+ public synchronized String nextID() throws IllegalStateException {
// Get next values for each character, from right to left
// (least significant to most significant).
return sb.toString();
}
- public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+ public synchronized boolean isValidID(String value) {
- if ( value == null || value == "") {
- throw new IllegalArgumentException("ID to validate must not be null or empty");
+ if (value == null || value.equals("")) {
+ return false;
}
Pattern pattern = Pattern.compile(getRegex());
public String getCurrentID();
// Sets the current value of an ID.
- public void setCurrentID(String value);
+ public void setCurrentID(String value) throws IllegalArgumentException;
// Resets an ID to its initial value.
public void resetID();
// Returns the next ID in the sequence, and sets
// the current value to that ID.
- public String nextID();
+ public String nextID() throws IllegalStateException;
// Validates an ID against a pattern of generated IDs.
public boolean isValidID(String value);
}
// Sets the current value of the ID associated with this IDPart.
- public synchronized void setCurrentID(String value) {
+ public synchronized void setCurrentID(String value) throws IllegalArgumentException {
generator.setCurrentID(value);
}
// Constructor.
public NumericIDGenerator(String initialValue, String maxLength)
throws IllegalArgumentException {
+
+ if (initialValue == null || initialValue.equals("")) {
+ throw new IllegalArgumentException("Initial ID value must not be null or empty");
+ }
try {
long l = Long.parseLong(initialValue.trim());
throw new IllegalArgumentException("Initial ID value must be parseable as a number");
}
+ if (maxLength == null || maxLength.equals("")) {
+ throw new IllegalArgumentException("Initial ID value must not be null or empty");
+ }
+
try {
this.maxLength = Integer.parseInt(maxLength);
} catch (NumberFormatException e) {
// @TODO Much of this code is copied from the main constructor,
// and may be ripe for refactoring.
+
+ if (value == null || value.equals("")) {
+ throw new IllegalArgumentException("ID value must not be null or empty");
+ }
+
try {
long l = Long.parseLong(value.trim());
if ( l < 0 ) {
- throw new IllegalArgumentException("Initial ID value should be zero (0) or greater");
+ throw new IllegalArgumentException("ID value should be zero (0) or greater");
}
this.currentValue = l;
this.initialValue = l;
public StringIDGenerator(String initialValue) throws IllegalArgumentException {
- if ( initialValue == null || initialValue == "") {
+ if (initialValue == null || initialValue.equals("")) {
throw new IllegalArgumentException("Initial ID value must not be null or empty");
}
}
public synchronized void setCurrentID(String value) throws IllegalArgumentException {
- if ( initialValue == null || initialValue == "") {
+ if (value == null || value.equals("")) {
throw new IllegalArgumentException("ID value must not be null or empty");
}
this.currentValue = value;
return this.currentValue;
}
- public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+ public synchronized boolean isValidID(String value) {
- if ( value == null || value == "") {
- throw new IllegalArgumentException("ID to validate must not be null or empty");
+ if (value == null || value.equals("")) {
+ return false;
}
Pattern pattern = Pattern.compile(getRegex());
public YearIDGenerator(String initialValue) throws IllegalArgumentException {
- if ( initialValue == null || initialValue == "") {
+ if (initialValue == null || initialValue.equals("")) {
throw new IllegalArgumentException("Initial ID value must not be null or empty");
}
// @TODO This code is copied from the main constructor,
// and thus there may be an opportunity for refactoring.
- if ( value == null || value == "") {
+ if (value == null || value.equals("")) {
throw new IllegalArgumentException("ID value must not be null or empty");
}
public synchronized boolean isValidID(String value) throws IllegalArgumentException {
- if ( value == null || value == "") {
- throw new IllegalArgumentException("ID to validate must not be null or empty");
+ if (value == null || value.equals("")) {
+ return false;
}
Pattern pattern = Pattern.compile(getRegex());
}
- public void testresetID() {
+ public void testResetID() {
part = new NumericIDPart("25");
assertEquals("26", part.nextID());
}
+ public void testNegativeInitialValue() {
+
+ try {
+ part = new NumericIDPart("-1");
+ fail("Should have thrown IllegalArgumentException here");
+ } catch (IllegalArgumentException expected) {
+ // This Exception should be thrown, and thus the test should pass.
+ }
+ }
+
+
public void testIsValidID() {
part = new NumericIDPart("1");
part = new NumericIDPart("1", "3");
assertFalse(part.isValidID("not a parseable long"));
+
+ part = new NumericIDPart("1", "3");
+ assertFalse(part.isValidID(null));
+
+ part = new NumericIDPart("1", "3");
+ assertFalse(part.isValidID(""));
}
part = new StringIDPart("T");
assertFalse(part.isValidID("TE"));
-
- }
- public void testNullValidationValue() {
-
- try {
- part = new StringIDPart("-");
- assertFalse(part.isValidID(null));
- fail("Should have thrown IllegalArgumentException here");
- } catch (IllegalArgumentException expected) {
- // This Exception should be thrown, and thus the test should pass.
- }
+ part = new StringIDPart("-");
+ assertFalse(part.isValidID(null));
+ part = new StringIDPart("-");
+ assertFalse(part.isValidID(""));
+
}
+
// @TODO: Add more tests of boundary conditions, exceptions ...
}
assertEquals(year, part.getCurrentID());
}
-
-/*
+ public void testSetCurrentID() {
+
+ part = new YearIDPart("1999");
+ part.setCurrentID("1999");
+ assertEquals("1999", part.getCurrentID());
+ part.setCurrentID("2000");
+ assertEquals("2000", part.getCurrentID());
+
+ }
+
+ public void testSetCurrentIDNullOrEmpty() {
+
+ part = new YearIDPart();
+
+ try {
+ part.setCurrentID(null);
+ fail("Should have thrown IllegalArgumentException here");
+ } catch (IllegalArgumentException expected) {
+ // This Exception should be thrown, and thus the test should pass.
+ }
+
+ try {
+ part.setCurrentID("");
+ fail("Should have thrown IllegalArgumentException here");
+ } catch (IllegalArgumentException expected) {
+ // This Exception should be thrown, and thus the test should pass.
+ }
+
+ }
+
public void testNextID() {
- part = new YearIDPart("XYZ");
- assertEquals("XYZ", part.nextID());
+
+ part = new YearIDPart("1999");
+ assertEquals("1999", part.nextID());
+
}
public void testresetID() {
- part = new YearIDPart(".");
- assertEquals(".", part.nextID());
+ part = new YearIDPart("1999");
+ assertEquals("1999", part.nextID());
part.resetID();
- assertEquals(".", part.nextID());
+ assertEquals("1999", part.getCurrentID());
}
public void testInitialID() {
- part = new YearIDPart("-");
- assertEquals("-", part.getInitialID());
+
+ part = new YearIDPart("1999");
+ assertEquals("1999", part.getInitialID());
+
}
-*/
public void testNullInitialValue() {
part = new YearIDPart();
assertFalse(part.isValidID("non-numeric value"));
-
+
+ part = new YearIDPart();
+ assertFalse(part.isValidID(null));
+
+ part = new YearIDPart();
+ assertFalse(part.isValidID(""));
+
}
- public void testNullValidationValue() {
-
- try {
- part = new YearIDPart();
- assertFalse(part.isValidID(null));
- fail("Should have thrown IllegalArgumentException here");
- } catch (IllegalArgumentException expected) {
- // This Exception should be thrown, and thus the test should pass.
- }
-
- }
// @TODO: Add more tests of boundary conditions, exceptions ...
}