Skip to content

Commit

Permalink
Replace unnecessary usage of AtomicReference with Optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
pferraro committed Oct 31, 2016
1 parent fcbf040 commit 992f47b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
Expand Up @@ -23,10 +23,10 @@
package org.wildfly.clustering.server.singleton;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.jboss.as.clustering.msc.ServiceContainerHelper;
Expand Down Expand Up @@ -213,9 +213,9 @@ public T getValue() {
}

@Override
public AtomicReference<T> getValueRef() {
public Optional<T> getLocalValue() {
try {
return this.primary.get() ? new AtomicReference<>(this.primaryController.getValue()) : null;
return this.primary.get() ? Optional.ofNullable(this.primaryController.getValue()) : null;
} catch (IllegalStateException e) {
// This might happen if primary service has not yet started, or if node is no longer the primary node
return null;
Expand Down
Expand Up @@ -25,8 +25,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import org.jboss.msc.service.Service;
Expand Down Expand Up @@ -67,15 +67,15 @@ public T getValue() {
if (!this.started) {
throw ClusteringServerLogger.ROOT_LOGGER.notStarted(this.serviceName.getCanonicalName());
}
Map<Node, CommandResponse<AtomicReference<T>>> responses = this.dispatcher.getValue().executeOnCluster(new SingletonValueCommand<T>());
Map<Node, CommandResponse<Optional<T>>> responses = this.dispatcher.getValue().executeOnCluster(new SingletonValueCommand<T>());
// Prune non-primary (i.e. null) results
result = responses.values().stream().map(response -> {
try {
return response.get();
} catch (ExecutionException e) {
throw new IllegalArgumentException(e);
}
}).filter(ref -> ref != null).map(ref -> ref.get()).collect(Collectors.toList());
}).filter(value -> value != null).map(value -> value.orElse(null)).collect(Collectors.toList());
// We expect only 1 result
if (result.size() > 1) {
// This would mean there are multiple primary nodes!
Expand Down
Expand Up @@ -21,13 +21,13 @@
*/
package org.wildfly.clustering.server.singleton;

import java.util.concurrent.atomic.AtomicReference;
import java.util.Optional;

public interface SingletonContext<T> {

void start();

void stop();

AtomicReference<T> getValueRef();
Optional<T> getLocalValue();
}
Expand Up @@ -21,13 +21,13 @@
*/
package org.wildfly.clustering.server.singleton;

import java.util.concurrent.atomic.AtomicReference;
import java.util.Optional;

public class SingletonValueCommand<T> implements SingletonCommand<AtomicReference<T>, T> {
public class SingletonValueCommand<T> implements SingletonCommand<Optional<T>, T> {
private static final long serialVersionUID = -2849349352107418635L;

@Override
public AtomicReference<T> execute(SingletonContext<T> context) {
return context.getValueRef();
public Optional<T> execute(SingletonContext<T> context) {
return context.getLocalValue();
}
}

0 comments on commit 992f47b

Please sign in to comment.