return sb.toString();
}
+ public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+ // Currently stubbed-out
+ return true;
+ }
+
}
public AlphabeticIDPart(String startVal, String endVal, String baseVal) {
super(new AlphabeticIDGenerator(startVal, endVal, baseVal));
};
-
+
}
public String getCurrentID();
public String getNextID();
+
+ public boolean isValidID(String value);
+
+ // public String getRegex();
}
}
// Returns the next value of this ID.
- public synchronized String getNextID() {
- try {
- return generator.getNextID();
- } catch (IllegalStateException e) {
- throw e;
- }
+ public synchronized String getNextID() throws IllegalStateException {
+ return generator.getNextID();
}
- // public boolean validate() {};
+ // Validates an instance of the ID, to
+ // identify if it matches this IDPart's pattern.
+ //
+ // Some IDParts may offer generic validation,
+ // while others may offer per-instance valiadation
+ // based on the values, series, etc. that are
+ // stored within those parts.
+ public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+ return generator.isValidID(value);
+ }
}
try {
long l = Long.parseLong(initialValue.trim());
if ( l < 0 ) {
- throw new IllegalArgumentException("Initial value should be zero (0) or greater");
+ throw new IllegalArgumentException("Initial ID value should be zero (0) or greater");
}
this.currentValue = l;
this.initialValue = l;
} catch (NullPointerException e) {
- throw new IllegalArgumentException("Initial value should not be null");
+ throw new IllegalArgumentException("Initial ID value should not be null");
} catch (NumberFormatException e) {
- throw new IllegalArgumentException("Initial value must be parseable as a number");
+ throw new IllegalArgumentException("Initial ID value must be parseable as a number");
}
}
this.currentValue++;
return Long.toString(this.currentValue);
}
+
+ public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+ // Currently stubbed-out
+ return true;
+ }
}
package org.collectionspace.services.id;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
public class StringIDGenerator implements IDGenerator {
private String initialValue = null;
public StringIDGenerator(String initialValue) throws IllegalArgumentException {
if ( initialValue == null || initialValue == "") {
- throw new IllegalArgumentException("Initial value must not be null or empty");
+ throw new IllegalArgumentException("Initial ID value must not be null or empty");
}
this.initialValue = initialValue;
public synchronized String getNextID() {
return this.currentValue;
}
+
+ public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+
+ if ( value == null || value == "") {
+ throw new IllegalArgumentException("ID to validate must not be null or empty");
+ }
+
+ Pattern pattern = Pattern.compile(getRegex());
+ Matcher matcher = pattern.matcher(value);
+ if (matcher.matches()) {
+ return true;
+ } else {
+ return false;
+ }
+
+ }
+
+ public synchronized String getRegex() {
+ String regex = "(" + this.initialValue + ")";
+ return regex;
+ }
}
import java.util.Calendar;
import java.util.GregorianCalendar;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
public class YearIDGenerator implements IDGenerator {
private String initialValue = null;
public YearIDGenerator(String initialValue) throws IllegalArgumentException {
if ( initialValue == null || initialValue == "") {
- throw new IllegalArgumentException("Initial value must not be null or empty");
+ throw new IllegalArgumentException("Initial ID value must not be null or empty");
}
// @TODO: Add regex-based validation here, by calling a
int y = cal.get(Calendar.YEAR);
return Integer.toString(y);
}
+
+ public synchronized boolean isValidID(String value) throws IllegalArgumentException {
+
+ if ( value == null || value == "") {
+ throw new IllegalArgumentException("ID to validate must not be null or empty");
+ }
+
+ Pattern pattern = Pattern.compile(getRegex());
+ Matcher matcher = pattern.matcher(value);
+ if (matcher.matches()) {
+ return true;
+ } else {
+ return false;
+ }
+
+ }
+
+ public synchronized String getRegex() {
+ // NOTE: Currently hard-coded to accept only a range of
+ // four-digit Gregorian Calendar year dates.
+ String regex = "(\\d\\d\\d\\d)";
+ return regex;
+ }
}
}
}
+
+ public void testIsValidID() {
+
+ part = new StringIDPart("-");
+ assertTrue(part.isValidID("-"));
+
+ part = new StringIDPart("-");
+ assertFalse(part.isValidID("--"));
+
+ // Test chars with special meaning in regexes.
+ part = new StringIDPart(".");
+ assertTrue(part.isValidID("."));
+
+ part = new StringIDPart("TE");
+ assertTrue(part.isValidID("TE"));
+
+ part = new StringIDPart("TE");
+ assertFalse(part.isValidID("T"));
+
+ 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.
+ }
+
+ }
// @TODO: Add more tests of boundary conditions, exceptions ...
}
}
}
+
+ public void testIsValidID() {
+
+ part = new YearIDPart();
+ assertTrue(part.isValidID("2009"));
+
+ part = new YearIDPart();
+ assertFalse(part.isValidID("839"));
+
+ part = new YearIDPart();
+ assertFalse(part.isValidID("10100"));
+
+ part = new YearIDPart();
+ assertFalse(part.isValidID("non-numeric value"));
+
+ }
+
+ 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 ...
}