Skip to content

Commit e4ce96d

Browse files
committed
Merge pull request #147 from GoogleCloudPlatform/cleanup-tasklist
Cleanup error handling/docs in TaskList
2 parents c4439f6 + 41dbfd9 commit e4ce96d

File tree

1 file changed

+15
-9
lines changed
  • datastore/src/main/java/com/google/datastore/snippets

1 file changed

+15
-9
lines changed

datastore/src/main/java/com/google/datastore/snippets/TaskList.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.datastore.snippets;
1818

1919
import com.google.gcloud.datastore.Datastore;
20-
import com.google.gcloud.datastore.DatastoreException;
2120
import com.google.gcloud.datastore.DatastoreOptions;
2221
import com.google.gcloud.datastore.DateTime;
2322
import com.google.gcloud.datastore.Entity;
@@ -51,7 +50,8 @@ public class TaskList {
5150
* Adds a task entity to the Datastore.
5251
*
5352
* @param description The task description
54-
* @return The {@link Key} of the entity.
53+
* @return The {@link Key} of the entity
54+
* @throws DatastoreException if the ID allocation or put fails
5555
*/
5656
Key addTask(String description) {
5757
Key key = datastore.allocateId(keyFactory.newKey());
@@ -70,14 +70,18 @@ Key addTask(String description) {
7070
* Marks a task entity as done.
7171
*
7272
* @param id The ID of the task entity as given by {@link Key#id()}
73-
* @throws DatastoreException if the task does not exist
73+
* @return true if the task was found, false if not
74+
* @throws DatastoreException if the transaction fails
7475
*/
75-
void markDone(long id) {
76+
boolean markDone(long id) {
7677
Transaction transaction = datastore.newTransaction();
7778
try {
7879
Entity task = transaction.get(keyFactory.newKey(id));
79-
transaction.put(Entity.builder(task).set("done", true).build());
80+
if (task != null) {
81+
transaction.put(Entity.builder(task).set("done", true).build());
82+
}
8083
transaction.commit();
84+
return task != null;
8185
} finally {
8286
if (transaction.active()) {
8387
transaction.rollback();
@@ -89,6 +93,8 @@ void markDone(long id) {
8993
// [START retrieve_entities]
9094
/**
9195
* Returns a list of all task entities in ascending order of creation time.
96+
*
97+
* @throws DatastoreException if the query fails
9298
*/
9399
Iterator<Entity> listTasks() {
94100
Query<Entity> query =
@@ -102,6 +108,7 @@ Iterator<Entity> listTasks() {
102108
* Deletes a task entity.
103109
*
104110
* @param id The ID of the task entity as given by {@link Key#id()}
111+
* @throws DatastoreException if the delete fails
105112
*/
106113
void deleteTask(long id) {
107114
datastore.delete(keyFactory.newKey(id));
@@ -158,10 +165,9 @@ void handleCommandLine(String commandLine) {
158165
case "done":
159166
assertArgsLength(args, 2);
160167
long id = Long.parseLong(args[1]);
161-
try {
162-
markDone(id);
168+
if (markDone(id)) {
163169
System.out.println("task marked done");
164-
} catch (DatastoreException e) {
170+
} else {
165171
System.out.printf("did not find a Task entity with ID %d%n", id);
166172
}
167173
break;
@@ -178,7 +184,7 @@ void handleCommandLine(String commandLine) {
178184
case "delete":
179185
assertArgsLength(args, 2);
180186
deleteTask(Long.parseLong(args[1]));
181-
System.out.println("task deleted");
187+
System.out.println("task deleted (if it existed)");
182188
break;
183189
default:
184190
throw new IllegalArgumentException("unrecognized command: " + command);

0 commit comments

Comments
 (0)