Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / prospectivesearch / Subscription.java
blobdea9e1ded5c4e62cc802aaafc1cf03662c2b0a18
1 // Copyright 2011 Google Inc. All rights reserved.
3 package com.google.appengine.api.prospectivesearch;
5 import static com.google.common.base.Preconditions.checkArgument;
6 import static com.google.common.base.Preconditions.checkNotNull;
8 import com.google.appengine.api.prospectivesearch.ProspectiveSearchPb.SubscriptionRecord;
9 import com.google.common.annotations.VisibleForTesting;
11 import java.util.Date;
13 /**
14 * The Subscription class represents information about a registered
15 * subscription.
17 * @see ProspectiveSearchService#subscribe(String, String, long, String, Map)
20 public final class Subscription {
22 /**
23 * The state of the subscription in the backend system.
25 public enum State {
26 /**
27 * Subscription is active.
29 OK,
31 /**
32 * Successfully registered but not yet active.
34 PENDING,
36 /**
37 * Inactive due to an error. (See the error value for explanation.)
39 ERROR;
41 /**
42 * Mapping from internal to wrapper states.
44 static State lookupState(final SubscriptionRecord.State internalState) {
45 switch (internalState) {
46 case OK:
47 return Subscription.State.OK;
48 case PENDING:
49 return Subscription.State.PENDING;
50 default:
51 return Subscription.State.ERROR;
56 private final String id;
57 private final String query;
58 private final long expirationTimeSec;
59 private State state;
60 private String errorMsg;
62 @VisibleForTesting
63 public Subscription(String id, String query, long expirationTimeSec) {
64 this(id, query, expirationTimeSec, State.OK, "");
67 @VisibleForTesting
68 public Subscription(String id,
69 String query,
70 long expirationTimeSec,
71 State state,
72 String errorMsg) {
73 checkArgument(expirationTimeSec >= 0,
74 "Lease duration must be non-negative: %s", expirationTimeSec);
75 this.id = checkNotNull(id);
76 this.query = checkNotNull(query);
77 this.expirationTimeSec = expirationTimeSec;
78 this.state = checkNotNull(state);
79 this.errorMsg = checkNotNull(errorMsg);
82 Subscription(SubscriptionRecord sr) {
83 this(sr.getId(), sr.getVanillaQuery(), (long) sr.getExpirationTimeSec(),
84 State.lookupState(sr.getStateEnum()), sr.getErrorMessage());
87 /**
88 * @return the id supplied during subscription
90 public String getId() {
91 return id;
94 /**
95 * @return the query supplied during subscription
97 public String getQuery() {
98 return query;
101 /** @return the expiration time of this subscription, in seconds
102 * since January 1, 1970 UTC */
103 public long getExpirationTime() {
104 return expirationTimeSec;
107 /** @return the current state of the subscription */
108 public State getState() {
109 return state;
112 /** @return the error message if the current state is ERROR */
113 public String getErrorMessage() {
114 return errorMsg;
117 @Override
118 public boolean equals(Object other) {
119 if (!(other instanceof Subscription)) {
120 return false;
122 Subscription o = (Subscription) other;
123 return id.equals(o.id) &&
124 query.equals(o.query) &&
125 expirationTimeSec == o.expirationTimeSec &&
126 state.equals(o.state);
129 @Override
130 public int hashCode() {
131 return id.hashCode();
135 * @return a string of the format: %s@%d{id=%s, query=%s,
136 * expires=%s, state=%s}, where the first two replacements are the
137 * class name and the system identity hashcode, and the date
138 * attribute may be "NEVER".
140 @Override
141 public String toString() {
142 return String.format("%s@%d{id=%s, query=%s, expires=%s, state=%s}",
143 this.getClass().getName(),
144 System.identityHashCode(this),
146 query,
147 expirationTimeSec == 0 ? "NEVER" : new Date(expirationTimeSec * 1000L),
148 state);