1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / datastore / TransactionOptions.java
blob94bd0a902a690005b55fbce544a935c2ce406323
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 package com.google.appengine.api.datastore;
4 import java.util.ArrayList;
5 import java.util.List;
7 /**
8 * Describes options for transactions, passed at transaction creation time.
10 * <p>{@code XG} is a boolean that enables or disables the use of cross-group
11 * transactions.
13 * <p>Notes on usage:<br>
14 * The recommended way to instantiate a {@code TransactionsOptions} object is to
15 * statically import {@link Builder}.* and invoke a static
16 * creation method followed by an instance mutator (if needed):
18 * <pre>
19 * import static com.google.appengine.api.datastore.TransactionOptions.Builder.*;
21 * ...
23 * datastoreService.beginTransaction(withXG(true));
24 * </pre>
27 public final class TransactionOptions {
29 private boolean xg = false;
31 private TransactionOptions() {
34 TransactionOptions(TransactionOptions original) {
35 this.xg = original.xg;
38 /**
39 * Enable or disable the use of cross-group transactions.
41 * @param enable true to cross-group transactions, false to
42 * restrict transactions to a single entity group.
43 * @return {@code this} (for chaining)
45 public TransactionOptions setXG(boolean enable) {
46 this.xg = enable;
47 return this;
50 /**
51 * Return the cross-group transaction setting to default (disabled).
53 public TransactionOptions clearXG() {
54 this.xg = false;
55 return this;
58 /**
59 * @return {@code true} if cross-group transactions are allowed,
60 * {@code false} if they are not allowed.
62 public boolean isXG() {
63 return xg;
66 /**
67 * @deprecated
68 * @see #setXG
70 @Deprecated public TransactionOptions multipleEntityGroups(boolean enable) {
71 return setXG(enable);
74 /**
75 * @deprecated
76 * @see #clearXG
78 @Deprecated public TransactionOptions clearMultipleEntityGroups() {
79 return clearXG();
82 /**
83 * @deprecated
84 * @see #isXG
86 @Deprecated public Boolean allowsMultipleEntityGroups() {
87 return isXG();
90 @Override
91 public int hashCode() {
92 int result = 0;
94 result = result * 31 + (xg ? 1 : 0);
96 return result;
99 @Override
100 public boolean equals(Object obj) {
101 if (obj == null) {
102 return false;
105 if (obj.getClass() != this.getClass()) {
106 return false;
109 TransactionOptions that = (TransactionOptions) obj;
111 return xg == that.xg;
114 @Override
115 public String toString() {
116 List<String> result = new ArrayList<String>();
118 result.add("XG=" + xg);
120 return "TransactionOptions" + result;
124 * Contains static creation methods for {@link TransactionOptions}.
126 public static final class Builder {
128 * Create a {@link TransactionOptions} that enables or disables the use of
129 * cross-group transactions. Shorthand for
130 * <code>TransactionOptions.withDefaults().setXG(...);</code>
132 * @param enable true to allow cross-group transactions, false to
133 * restrict transactions to a single entity group.
134 * @return {@code this} (for chaining)
136 public static TransactionOptions withXG(boolean enable) {
137 return withDefaults().setXG(enable);
141 * @deprecated
142 * @see #withXG
144 @Deprecated public static TransactionOptions allowMultipleEntityGroups(boolean enable) {
145 return withDefaults().setXG(enable);
149 * Helper method for creating a {@link TransactionOptions} instance with
150 * default values. The defaults is false (disabled) for XG.
152 public static TransactionOptions withDefaults() {
153 return new TransactionOptions();
156 private Builder() {}