Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDI-650 Introduce asynchronous event notification options (#337)
- Loading branch information
Showing
5 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
api/src/main/java/javax/enterprise/event/ImmutableNotificationOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2016, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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 javax.enterprise.event; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* The immutable implementation of {@link NotificationOptions}. | ||
* | ||
* @author Martin Kouba | ||
* | ||
*/ | ||
class ImmutableNotificationOptions implements NotificationOptions { | ||
|
||
private final Executor executor; | ||
|
||
private final Map<String, Object> options; | ||
|
||
private ImmutableNotificationOptions(Executor executor, Map<String, Object> options) { | ||
this.executor = executor; | ||
this.options = new HashMap<>(options); | ||
} | ||
|
||
@Override | ||
public Executor getExecutor() { | ||
return executor; | ||
} | ||
|
||
@Override | ||
public Object get(String name) { | ||
return options.get(name); | ||
} | ||
|
||
static class Builder implements javax.enterprise.event.NotificationOptions.Builder { | ||
|
||
private Executor executor; | ||
|
||
private Map<String, Object> options; | ||
|
||
Builder() { | ||
this.options = new HashMap<>(); | ||
} | ||
|
||
public Builder setExecutor(Executor executor) { | ||
this.executor = executor; | ||
return this; | ||
} | ||
|
||
public Builder set(String name, Object value) { | ||
options.put(name, value); | ||
return this; | ||
} | ||
|
||
public ImmutableNotificationOptions build() { | ||
return new ImmutableNotificationOptions(executor, options); | ||
} | ||
|
||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
api/src/main/java/javax/enterprise/event/NotificationOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2016, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* 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 javax.enterprise.event; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* Notification options are used to configure observer notification. | ||
* | ||
* @author Martin Kouba | ||
* @see Event#fireAsync(Object, NotificationOptions) | ||
* @since 2.0 | ||
*/ | ||
public interface NotificationOptions { | ||
|
||
/** | ||
* | ||
* @return the executor used to execute an asynchronous event | ||
*/ | ||
Executor getExecutor(); | ||
|
||
/** | ||
* | ||
* @param optionName | ||
* @return the value of an option or <code>null</code> if no option for the given name exists | ||
*/ | ||
Object get(String optionName); | ||
|
||
/** | ||
* | ||
* @param executor | ||
* @return an immutable holder of an executor | ||
*/ | ||
static NotificationOptions ofExecutor(Executor executor) { | ||
return builder().setExecutor(executor).build(); | ||
} | ||
|
||
/** | ||
* | ||
* @param optionName | ||
* @param optionValue | ||
* @return an immutable holder of a single option | ||
*/ | ||
static NotificationOptions of(String optionName, Object optionValue) { | ||
return builder().set(optionName, optionValue).build(); | ||
} | ||
|
||
/** | ||
* | ||
* @return the options builder | ||
*/ | ||
static Builder builder() { | ||
return new ImmutableNotificationOptions.Builder(); | ||
} | ||
|
||
/** | ||
* Notification options builder. | ||
* | ||
* @author Martin Kouba | ||
* @since 2.0 | ||
*/ | ||
interface Builder { | ||
|
||
Builder setExecutor(Executor executor); | ||
|
||
Builder set(String optionName, Object optionValue); | ||
|
||
NotificationOptions build(); | ||
|
||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
api/src/test/java/org/jboss/cdi/api/test/event/NotificationOptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.jboss.cdi.api.test.event; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.Assert.assertNull; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.Executor; | ||
|
||
import javax.enterprise.event.NotificationOptions; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* | ||
* @author Martin Kouba | ||
*/ | ||
public class NotificationOptionsTest { | ||
|
||
@Test | ||
public void testBuilder() { | ||
Duration timeout = Duration.ofDays(1); | ||
NotificationOptions options = NotificationOptions.of("timeout", timeout); | ||
assertEquals(timeout, options.get("timeout")); | ||
assertNull(options.getExecutor()); | ||
assertNull(options.get("alpha")); | ||
options = NotificationOptions.builder().set("foo", "bar").setExecutor(new Executor() { | ||
@Override | ||
public void execute(Runnable command) { | ||
} | ||
}).build(); | ||
assertEquals("bar", options.get("foo")); | ||
assertNotNull(options.getExecutor()); | ||
options = NotificationOptions.ofExecutor(new Executor() { | ||
@Override | ||
public void execute(Runnable command) { | ||
} | ||
}); | ||
Executor executor = options.getExecutor(); | ||
assertNotNull(executor); | ||
assertNull(options.get("timeout")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters