validateDocument(replacementDocument);
}
final Instant now = Instant.now();
ArgumentChecker.isTrue(MasterUtils.checkUniqueVersionsFrom(replacementDocuments), "No two versioned documents may have the same \"version from\" instant");
final ConfigDocument storedDocument = _store.get(objectId.getObjectId());
if (storedDocument == null) {
throw new DataNotFoundException("Document not found: " + objectId.getObjectId());
}
if (replacementDocuments.isEmpty()) {
_store.remove(objectId.getObjectId());
_changeManager.entityChanged(ChangeType.REMOVED, objectId.getObjectId(), null, null, now);
return Collections.emptyList();
} else {
final Instant storedVersionFrom = storedDocument.getVersionFromInstant();
final Instant storedVersionTo = storedDocument.getVersionToInstant();
final List<ConfigDocument> orderedReplacementDocuments = MasterUtils.adjustVersionInstants(now, storedVersionFrom, storedVersionTo, replacementDocuments);
final ConfigDocument lastReplacementDocument = orderedReplacementDocuments.get(orderedReplacementDocuments.size() - 1);
if (_store.replace(objectId.getObjectId(), storedDocument, lastReplacementDocument) == false) {
throw new IllegalArgumentException("Concurrent modification");
}
return ImmutableList.of(lastReplacementDocument.getUniqueId());
}
}