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
;
14 * The Subscription class represents information about a registered
17 * @see ProspectiveSearchService#subscribe(String, String, long, String, Map)
20 public final class Subscription
{
23 * The state of the subscription in the backend system.
27 * Subscription is active.
32 * Successfully registered but not yet active.
37 * Inactive due to an error. (See the error value for explanation.)
42 * Mapping from internal to wrapper states.
44 static State
lookupState(final SubscriptionRecord
.State internalState
) {
45 switch (internalState
) {
47 return Subscription
.State
.OK
;
49 return Subscription
.State
.PENDING
;
51 return Subscription
.State
.ERROR
;
56 private final String id
;
57 private final String query
;
58 private final long expirationTimeSec
;
60 private String errorMsg
;
63 public Subscription(String id
, String query
, long expirationTimeSec
) {
64 this(id
, query
, expirationTimeSec
, State
.OK
, "");
68 public Subscription(String id
,
70 long expirationTimeSec
,
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());
88 * @return the id supplied during subscription
90 public String
getId() {
95 * @return the query supplied during subscription
97 public String
getQuery() {
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() {
112 /** @return the error message if the current state is ERROR */
113 public String
getErrorMessage() {
118 public boolean equals(Object other
) {
119 if (!(other
instanceof Subscription
)) {
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
);
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".
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),
147 expirationTimeSec
== 0 ?
"NEVER" : new Date(expirationTimeSec
* 1000L),