if (originalSone.isLocal()) {
soneBuilder = soneBuilder.local();
}
- Sone sone = soneBuilder.build();
SimpleXML soneXml;
try {
soneXml = SimpleXML.fromDocument(document);
} catch (NullPointerException npe1) {
/* for some reason, invalid XML can cause NPEs. */
- logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", sone), npe1);
+ logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone), npe1);
return null;
}
String soneTime = soneXml.getValue("time", null);
if (soneTime == null) {
/* TODO - mark Sone as bad. */
- logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
+ logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", originalSone));
return null;
}
try {
- sone.setTime(Long.parseLong(soneTime));
+ soneBuilder.lastUpdated(Long.parseLong(soneTime));
} catch (NumberFormatException nfe1) {
/* TODO - mark Sone as bad. */
- logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
+ logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", originalSone, soneTime));
return null;
}
+ Sone sone = soneBuilder.build();
SimpleXML clientXml = soneXml.getNode("client");
if (clientXml != null) {
protected Identity identity;
protected boolean local;
+ protected long lastUpdated;
@Override
public SoneBuilder from(Identity identity) {
return this;
}
+ @Override
+ public SoneBuilder lastUpdated(long lastUpdated) {
+ this.lastUpdated = lastUpdated;
+ return this;
+ }
+
protected void validate() throws IllegalStateException {
checkState(identity != null, "identity must not be null");
checkState(!local || (identity instanceof OwnIdentity),
"can not create local Sone from remote identity");
+ checkState(lastUpdated > 0, "last update time must be set");
}
}
private volatile long latestEdition;
/** The time of the last inserted update. */
- private volatile long time;
+ private final long time;
/** The status of this Sone. */
private volatile SoneStatus status = SoneStatus.unknown;
* @param local
* {@code true} if the Sone is a local Sone, {@code false} otherwise
*/
- public SoneImpl(Database database, Identity identity, boolean local) {
+ public SoneImpl(Database database, Identity identity, boolean local, long time) {
this.database = database;
this.id = identity.getId();
this.identity = identity;
this.local = local;
+ this.time = time;
}
//
* @return This Sone (for method chaining)
*/
public Sone setTime(long time) {
- this.time = time;
return this;
}
SoneBuilder from(Identity identity);
SoneBuilder local();
+ SoneBuilder lastUpdated(long lastUpdated);
+
Sone build() throws IllegalStateException;
}
@Override
public Sone build() throws IllegalStateException {
validate();
- return new SoneImpl(database, identity, local);
+ return new SoneImpl(database, identity, local, lastUpdated);
}
}
@Test
public void localSoneIsValidated() {
Identity ownIdentity = mock(OwnIdentity.class);
- soneBuilder.local().from(ownIdentity).build();
+ soneBuilder.local().from(ownIdentity).lastUpdated(1).build();
}
@Test(expected = IllegalStateException.class)
public void localSoneIsNotValidatedIfIdentityIsNotAnOwnIdentity() {
Identity identity = mock(Identity.class);
- soneBuilder.local().from(identity).build();
+ soneBuilder.local().from(identity).lastUpdated(1).build();
}
@Test(expected = IllegalStateException.class)
public void localSoneIsNotValidatedIfIdentityIsNull() {
- soneBuilder.local().build();
+ soneBuilder.local().lastUpdated(1).build();
}
@Test
public void remoteSoneIsValidated() {
Identity identity = mock(Identity.class);
- soneBuilder.from(identity).build();
+ soneBuilder.from(identity).lastUpdated(1).build();
}
@Test(expected = IllegalStateException.class)
public void remoteSoneIsNotValidatedIfIdentityIsNull() {
- soneBuilder.build();
+ soneBuilder.lastUpdated(1).build();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void localSoneIsNotValidatedWithoutUpdateTime() {
+ Identity identity = mock(OwnIdentity.class);
+ soneBuilder.from(identity).local().build();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void remoteSoneIsNotValidatedWithoutUpdateTime() {
+ Identity identity = mock(Identity.class);
+ soneBuilder.from(identity).build();
}
}