<section name="GIT access">
<p>Anonymous git access is at
<code>https://wamblee.org/git/public/utils</code></p>
- <p>More user-friendly access through a browser is at <a
+ <p>Browse the repository at <a
href="https://wamblee.org/gitweb/utils">https://wamblee.org/gitweb/utils</a>
</p>
</section>
*/
public class Id<T> implements Comparable<Id<T>> {
- private long id;
+ private String id;
/**
* Constructs the id.
*
* @param aId
* Integer id.
+ * @throws NullPointerException
+ * in case the id is null.
*/
- public Id(long aId) {
+ public Id(String aId) {
+ if (aId == null) {
+ throw new NullPointerException("id is null");
+ }
id = aId;
}
/**
* @return The underlying id.
*/
- public long getId() {
+ public String getId() {
return id;
}
@Override
public int hashCode() {
- return ((Long) id).hashCode();
+ return id.hashCode();
}
@Override
if (!(aObj instanceof Id)) {
return false;
}
- return id == ((Id<T>) aObj).id;
+ return id.equals(((Id<T>) aObj).id);
}
@Override
public String toString() {
- return id + "";
+ return id;
}
@Override
- public int compareTo(org.wamblee.general.Id<T> aId) {
- return ((Long) id).compareTo((Long) aId.getId());
+ public int compareTo(Id<T> aId) {
+ return id.compareTo(aId.getId());
}
}
@Test
public void testGetSet() {
- Id<IdTest> id = new Id<IdTest>(100L);
- assertEquals(100L, id.getId());
+ Id<IdTest> id = new Id<IdTest>("hello");
+ assertEquals("hello", id.getId());
}
@Test
public void testEqualsHashCodeCompare() {
- Id<IdTest> id1 = new Id<IdTest>(100L);
- Id<IdTest> id2 = new Id<IdTest>(200L);
- Id<IdTest> id3 = new Id<IdTest>(100L);
+ Id<IdTest> id1 = new Id<IdTest>("a");
+ Id<IdTest> id2 = new Id<IdTest>("b");
+ Id<IdTest> id3 = new Id<IdTest>("a");
assertEquals(id1, id3);
assertFalse(id1.equals(id2));
assertFalse(id1.equals(null));
- assertFalse(id1.equals("hello"));
+ assertFalse(id1.equals("a"));
assertEquals(id1.hashCode(), id3.hashCode());
assertTrue(id1.compareTo(id2) < 0);
assertTrue(id2.compareTo(id1) > 0);
assertEquals(0, id1.compareTo(id3));
}
+
+ @Test(expected = NullPointerException.class)
+ public void testNullNotAccepted() {
+ Id<IdTest> id = new Id<IdTest>(null);
+ }
}