Skip to content

Commit

Permalink
Add base class for operation options, javadoc and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed May 10, 2016
1 parent 9879a16 commit 57d613b
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.pubsub;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;

import java.io.Serializable;
import java.util.Objects;

/**
* Base class for Pub/Sub operation options.
*/
abstract class Option implements Serializable {

private static final long serialVersionUID = 4956295408130172192L;

private final OptionType optionType;
private final Object value;

interface OptionType {

String name();
}

Option(OptionType optionType, Object value) {
this.optionType = checkNotNull(optionType);
this.value = value;
}

@SuppressWarnings("unchecked")
<T extends OptionType> T optionType() {
return (T) optionType;
}

Object value() {
return value;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Option)) {
return false;
}
Option other = (Option) obj;
return Objects.equals(optionType, other.optionType)
&& Objects.equals(value, other.value);
}

@Override
public int hashCode() {
return Objects.hash(optionType, value);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", optionType.name())
.add("value", value)
.toString();
}
}
108 changes: 55 additions & 53 deletions gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/PubSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import com.google.cloud.Page;
import com.google.cloud.Service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

Expand All @@ -33,65 +33,67 @@
*/
public interface PubSub extends Service<PubSubOptions> {

final class ListOption implements Serializable {
/**
* Class for specifying options for listing topics and subscriptions.
*/
final class ListOption extends Option {

private static final long serialVersionUID = 6517442127283383124L;

private final Option option;
private final Object value;
enum OptionType implements Option.OptionType {
PAGE_SIZE, PAGE_TOKEN;

enum Option {
PAGE_SIZE, PAGE_TOKEN
}

private ListOption(Option option, Object value) {
this.option = option;
this.value = value;
}

Option option() {
return option;
@SuppressWarnings("unchecked")
<T> T get(Map<Option.OptionType, ?> options) {
return (T) options.get(this);
}
}

Object value() {
return value;
private ListOption(OptionType option, Object value) {
super(option, value);
}

/**
* Returns an option to specify the maximum number of resources returned per page.
*/
public static ListOption pageSize(int pageSize) {
return new ListOption(Option.PAGE_SIZE, pageSize);
return new ListOption(OptionType.PAGE_SIZE, pageSize);
}

/**
* Returns an option to specify the page token from which to start listing resources.
*/
public static ListOption pageToken(String pageToken) {
return new ListOption(Option.PAGE_TOKEN, pageToken);
return new ListOption(OptionType.PAGE_TOKEN, pageToken);
}
}

final class PullOption implements Serializable {
/**
* Class for specifying options for pulling messages.
*/
final class PullOption extends Option {

private static final long serialVersionUID = -5220474819637439937L;

private final Option option;
private final Object value;
enum OptionType implements Option.OptionType {
MAX_MESSAGES;

enum Option {
MAX_MESSAGES
}

private PullOption(Option option, Object value) {
this.option = option;
this.value = value;
}

Option option() {
return option;
@SuppressWarnings("unchecked")
<T> T get(Map<Option.OptionType, ?> options) {
return (T) options.get(this);
}
}

Object value() {
return value;
private PullOption(OptionType option, Object value) {
super(option, value);
}

/**
* Returns an option to specify the maximum number of messages that can be returned by the pull
* operation.
*/
public static PullOption maxMessages(int maxMessages) {
return new PullOption(Option.MAX_MESSAGES, maxMessages);
return new PullOption(OptionType.MAX_MESSAGES, maxMessages);
}
}

Expand All @@ -108,32 +110,32 @@ interface MessageProcessor {
*/
interface MessageConsumer extends AutoCloseable {

final class PullOption implements Serializable {
/**
* Class for specifying options to pull messages through a {@code MessageConsumer}.
*/
final class PullOption extends Option {

private static final long serialVersionUID = 4792164134340316582L;

private final Option option;
private final Object value;

enum Option {
MAX_CONCURRENT_CALLBACKS
}

private PullOption(Option option, Object value) {
this.option = option;
this.value = value;
}
enum OptionType implements Option.OptionType {
MAX_CONCURRENT_CALLBACKS;

Option option() {
return option;
@SuppressWarnings("unchecked")
<T> T get(Map<OptionType, ?> options) {
return (T) options.get(this);
}
}

Object value() {
return value;
private PullOption(OptionType option, Object value) {
super(option, value);
}

/**
* Returns an option to specify the maximum number of messages that can be executed
* concurrently at any time.
*/
public static PullOption maxConcurrentCallbacks(int maxConcurrency) {
return new PullOption(Option.MAX_CONCURRENT_CALLBACKS, maxConcurrency);
return new PullOption(OptionType.MAX_CONCURRENT_CALLBACKS, maxConcurrency);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.pubsub;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;

import com.google.cloud.pubsub.Option.OptionType;
import com.google.cloud.pubsub.PubSub.ListOption;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class OptionTest {

private static final OptionType OPTION_TYPE = ListOption.OptionType.PAGE_SIZE;
private static final OptionType ANOTHER_OPTION_TYPE = ListOption.OptionType.PAGE_TOKEN;
private static final String VALUE = "some value";
private static final String OTHER_VALUE = "another value";
private static final Option OPTION = new Option(OPTION_TYPE, VALUE) {};
private static final Option OPTION_EQUALS = new Option(OPTION_TYPE, VALUE) {};
private static final Option OPTION_NOT_EQUALS1 = new Option(ANOTHER_OPTION_TYPE, OTHER_VALUE) {};
private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_OPTION_TYPE, VALUE) {};

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testEquals() {
assertEquals(OPTION, OPTION_EQUALS);
assertNotEquals(OPTION, OPTION_NOT_EQUALS1);
assertNotEquals(OPTION, OPTION_NOT_EQUALS2);
}

@Test
public void testHashCode() {
assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode());
}

@Test
public void testConstructor() {
assertEquals(OPTION_TYPE, OPTION.optionType());
assertEquals(VALUE, OPTION.value());
Option option = new Option(OPTION_TYPE, null) {};
assertEquals(OPTION_TYPE, option.optionType());
assertNull(option.value());
thrown.expect(NullPointerException.class);
new Option(null, VALUE) {};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.pubsub;

import static org.junit.Assert.assertEquals;

import com.google.cloud.pubsub.PubSub.ListOption;
import com.google.cloud.pubsub.PubSub.MessageConsumer;
import com.google.cloud.pubsub.PubSub.PullOption;

import org.junit.Test;

public class PubSubTest {

private static final int PAGE_SIZE = 42;
private static final String PAGE_TOKEN = "page token";
private static final int MAX_MESSAGES = 42;
private static final int MAX_CONCURRENT_CALLBACKS = 42;

@Test
public void testListOption() {
// page token
ListOption listOption = ListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, listOption.value());
assertEquals(ListOption.OptionType.PAGE_TOKEN, listOption.optionType());
// page size
listOption = ListOption.pageSize(PAGE_SIZE);
assertEquals(PAGE_SIZE, listOption.value());
assertEquals(ListOption.OptionType.PAGE_SIZE, listOption.optionType());
}

@Test
public void testPullOptions() {
PullOption pullOption = PullOption.maxMessages(MAX_MESSAGES);
assertEquals(MAX_MESSAGES, pullOption.value());
assertEquals(PullOption.OptionType.MAX_MESSAGES, pullOption.optionType());
}

@Test
public void testMessageConsumerPullOptions() {
MessageConsumer.PullOption pullOption =
MessageConsumer.PullOption.maxConcurrentCallbacks(MAX_CONCURRENT_CALLBACKS);
assertEquals(MAX_CONCURRENT_CALLBACKS, pullOption.value());
assertEquals(MessageConsumer.PullOption.OptionType.MAX_CONCURRENT_CALLBACKS,
pullOption.optionType());
}
}

0 comments on commit 57d613b

Please sign in to comment.