Skip to content

Commit 836da6c

Browse files
committed
Add base class for operation options, javadoc and tests
1 parent 7857986 commit 836da6c

File tree

4 files changed

+259
-53
lines changed

4 files changed

+259
-53
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsub;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
23+
import java.io.Serializable;
24+
import java.util.Objects;
25+
26+
/**
27+
* Base class for Pub/Sub operation options.
28+
*/
29+
abstract class Option implements Serializable {
30+
31+
private static final long serialVersionUID = 4956295408130172192L;
32+
33+
private final OptionType optionType;
34+
private final Object value;
35+
36+
interface OptionType {
37+
38+
String name();
39+
}
40+
41+
Option(OptionType optionType, Object value) {
42+
this.optionType = checkNotNull(optionType);
43+
this.value = value;
44+
}
45+
46+
@SuppressWarnings("unchecked")
47+
<T extends OptionType> T optionType() {
48+
return (T) optionType;
49+
}
50+
51+
Object value() {
52+
return value;
53+
}
54+
55+
@Override
56+
public boolean equals(Object obj) {
57+
if (!(obj instanceof Option)) {
58+
return false;
59+
}
60+
Option other = (Option) obj;
61+
return Objects.equals(optionType, other.optionType)
62+
&& Objects.equals(value, other.value);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hash(optionType, value);
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return MoreObjects.toStringHelper(this)
73+
.add("name", optionType.name())
74+
.add("value", value)
75+
.toString();
76+
}
77+
}

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/PubSub.java

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import com.google.cloud.Page;
2121
import com.google.cloud.Service;
2222

23-
import java.io.Serializable;
2423
import java.util.Iterator;
2524
import java.util.List;
25+
import java.util.Map;
2626
import java.util.concurrent.Future;
2727
import java.util.concurrent.TimeUnit;
2828

@@ -33,65 +33,67 @@
3333
*/
3434
public interface PubSub extends Service<PubSubOptions> {
3535

36-
final class ListOption implements Serializable {
36+
/**
37+
* Class for specifying options for listing topics and subscriptions.
38+
*/
39+
final class ListOption extends Option {
3740

3841
private static final long serialVersionUID = 6517442127283383124L;
3942

40-
private final Option option;
41-
private final Object value;
43+
enum OptionType implements Option.OptionType {
44+
PAGE_SIZE, PAGE_TOKEN;
4245

43-
enum Option {
44-
PAGE_SIZE, PAGE_TOKEN
45-
}
46-
47-
private ListOption(Option option, Object value) {
48-
this.option = option;
49-
this.value = value;
50-
}
51-
52-
Option option() {
53-
return option;
46+
@SuppressWarnings("unchecked")
47+
<T> T get(Map<Option.OptionType, ?> options) {
48+
return (T) options.get(this);
49+
}
5450
}
5551

56-
Object value() {
57-
return value;
52+
private ListOption(OptionType option, Object value) {
53+
super(option, value);
5854
}
5955

56+
/**
57+
* Returns an option to specify the maximum number of resources returned per page.
58+
*/
6059
public static ListOption pageSize(int pageSize) {
61-
return new ListOption(Option.PAGE_SIZE, pageSize);
60+
return new ListOption(OptionType.PAGE_SIZE, pageSize);
6261
}
6362

63+
/**
64+
* Returns an option to specify the page token from which to start listing resources.
65+
*/
6466
public static ListOption pageToken(String pageToken) {
65-
return new ListOption(Option.PAGE_TOKEN, pageToken);
67+
return new ListOption(OptionType.PAGE_TOKEN, pageToken);
6668
}
6769
}
6870

69-
final class PullOption implements Serializable {
71+
/**
72+
* Class for specifying options for pulling messages.
73+
*/
74+
final class PullOption extends Option {
7075

7176
private static final long serialVersionUID = -5220474819637439937L;
7277

73-
private final Option option;
74-
private final Object value;
78+
enum OptionType implements Option.OptionType {
79+
MAX_MESSAGES;
7580

76-
enum Option {
77-
MAX_MESSAGES
78-
}
79-
80-
private PullOption(Option option, Object value) {
81-
this.option = option;
82-
this.value = value;
83-
}
84-
85-
Option option() {
86-
return option;
81+
@SuppressWarnings("unchecked")
82+
<T> T get(Map<Option.OptionType, ?> options) {
83+
return (T) options.get(this);
84+
}
8785
}
8886

89-
Object value() {
90-
return value;
87+
private PullOption(OptionType option, Object value) {
88+
super(option, value);
9189
}
9290

91+
/**
92+
* Returns an option to specify the maximum number of messages that can be returned by the pull
93+
* operation.
94+
*/
9395
public static PullOption maxMessages(int maxMessages) {
94-
return new PullOption(Option.MAX_MESSAGES, maxMessages);
96+
return new PullOption(OptionType.MAX_MESSAGES, maxMessages);
9597
}
9698
}
9799

@@ -108,32 +110,32 @@ interface MessageProcessor {
108110
*/
109111
interface MessageConsumer extends AutoCloseable {
110112

111-
final class PullOption implements Serializable {
113+
/**
114+
* Class for specifying options to pull messages through a {@code MessageConsumer}.
115+
*/
116+
final class PullOption extends Option {
112117

113118
private static final long serialVersionUID = 4792164134340316582L;
114119

115-
private final Option option;
116-
private final Object value;
117-
118-
enum Option {
119-
MAX_CONCURRENT_CALLBACKS
120-
}
121-
122-
private PullOption(Option option, Object value) {
123-
this.option = option;
124-
this.value = value;
125-
}
120+
enum OptionType implements Option.OptionType {
121+
MAX_CONCURRENT_CALLBACKS;
126122

127-
Option option() {
128-
return option;
123+
@SuppressWarnings("unchecked")
124+
<T> T get(Map<OptionType, ?> options) {
125+
return (T) options.get(this);
126+
}
129127
}
130128

131-
Object value() {
132-
return value;
129+
private PullOption(OptionType option, Object value) {
130+
super(option, value);
133131
}
134132

133+
/**
134+
* Returns an option to specify the maximum number of messages that can be executed
135+
* concurrently at any time.
136+
*/
135137
public static PullOption maxConcurrentCallbacks(int maxConcurrency) {
136-
return new PullOption(Option.MAX_CONCURRENT_CALLBACKS, maxConcurrency);
138+
return new PullOption(OptionType.MAX_CONCURRENT_CALLBACKS, maxConcurrency);
137139
}
138140
}
139141

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsub;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.Assert.assertNull;
22+
23+
import com.google.cloud.pubsub.Option.OptionType;
24+
import com.google.cloud.pubsub.PubSub.ListOption;
25+
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.rules.ExpectedException;
29+
30+
public class OptionTest {
31+
32+
private static final OptionType OPTION_TYPE = ListOption.OptionType.PAGE_SIZE;
33+
private static final OptionType ANOTHER_OPTION_TYPE = ListOption.OptionType.PAGE_TOKEN;
34+
private static final String VALUE = "some value";
35+
private static final String OTHER_VALUE = "another value";
36+
private static final Option OPTION = new Option(OPTION_TYPE, VALUE) {};
37+
private static final Option OPTION_EQUALS = new Option(OPTION_TYPE, VALUE) {};
38+
private static final Option OPTION_NOT_EQUALS1 = new Option(ANOTHER_OPTION_TYPE, OTHER_VALUE) {};
39+
private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_OPTION_TYPE, VALUE) {};
40+
41+
@Rule
42+
public ExpectedException thrown = ExpectedException.none();
43+
44+
@Test
45+
public void testEquals() {
46+
assertEquals(OPTION, OPTION_EQUALS);
47+
assertNotEquals(OPTION, OPTION_NOT_EQUALS1);
48+
assertNotEquals(OPTION, OPTION_NOT_EQUALS2);
49+
}
50+
51+
@Test
52+
public void testHashCode() {
53+
assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode());
54+
}
55+
56+
@Test
57+
public void testConstructor() {
58+
assertEquals(OPTION_TYPE, OPTION.optionType());
59+
assertEquals(VALUE, OPTION.value());
60+
Option option = new Option(OPTION_TYPE, null) {};
61+
assertEquals(OPTION_TYPE, option.optionType());
62+
assertNull(option.value());
63+
thrown.expect(NullPointerException.class);
64+
new Option(null, VALUE) {};
65+
}
66+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsub;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.cloud.pubsub.PubSub.ListOption;
22+
import com.google.cloud.pubsub.PubSub.MessageConsumer;
23+
import com.google.cloud.pubsub.PubSub.PullOption;
24+
25+
import org.junit.Test;
26+
27+
public class PubSubTest {
28+
29+
private static final int PAGE_SIZE = 42;
30+
private static final String PAGE_TOKEN = "page token";
31+
private static final int MAX_MESSAGES = 42;
32+
private static final int MAX_CONCURRENT_CALLBACKS = 42;
33+
34+
@Test
35+
public void testListOption() {
36+
// page token
37+
ListOption listOption = ListOption.pageToken(PAGE_TOKEN);
38+
assertEquals(PAGE_TOKEN, listOption.value());
39+
assertEquals(ListOption.OptionType.PAGE_TOKEN, listOption.optionType());
40+
// page size
41+
listOption = ListOption.pageSize(PAGE_SIZE);
42+
assertEquals(PAGE_SIZE, listOption.value());
43+
assertEquals(ListOption.OptionType.PAGE_SIZE, listOption.optionType());
44+
}
45+
46+
@Test
47+
public void testPullOptions() {
48+
PullOption pullOption = PullOption.maxMessages(MAX_MESSAGES);
49+
assertEquals(MAX_MESSAGES, pullOption.value());
50+
assertEquals(PullOption.OptionType.MAX_MESSAGES, pullOption.optionType());
51+
}
52+
53+
@Test
54+
public void testMessageConsumerPullOptions() {
55+
MessageConsumer.PullOption pullOption =
56+
MessageConsumer.PullOption.maxConcurrentCallbacks(MAX_CONCURRENT_CALLBACKS);
57+
assertEquals(MAX_CONCURRENT_CALLBACKS, pullOption.value());
58+
assertEquals(MessageConsumer.PullOption.OptionType.MAX_CONCURRENT_CALLBACKS,
59+
pullOption.optionType());
60+
}
61+
}

0 commit comments

Comments
 (0)