Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds nullability checks and annotations to remaining services #15842

Merged
merged 1 commit into from Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,7 @@

package com.hazelcast.client;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.UUID;

Expand All @@ -35,6 +36,7 @@ public interface ClientService {
*
* @return all connected clients to this member
*/
@Nonnull
Collection<Client> getConnectedClients();

/**
Expand All @@ -46,7 +48,7 @@ public interface ClientService {
* @return registration ID which can be used to remove the listener using the {@link #removeClientListener(UUID)} method
* @throws java.lang.NullPointerException if clientListener is {@code null}
*/
UUID addClientListener(ClientListener clientListener);
@Nonnull UUID addClientListener(@Nonnull ClientListener clientListener);

/**
* Removes a ClientListener.
Expand All @@ -57,5 +59,5 @@ public interface ClientService {
* @return {@code true} if registration is removed, {@code false} otherwise
* @throws java.lang.NullPointerException if registration ID is {@code null}
*/
boolean removeClientListener(UUID registrationId);
boolean removeClientListener(@Nonnull UUID registrationId);
}
Expand Up @@ -34,6 +34,10 @@
import com.hazelcast.cp.lock.FencedLock;
import com.hazelcast.cp.session.CPSessionManagementService;

import javax.annotation.Nonnull;

import static com.hazelcast.internal.util.Preconditions.checkNotNull;

/**
* Client-side impl of the CP subsystem to create CP data structure proxies
*/
Expand All @@ -49,28 +53,38 @@ public void init(ClientContext context) {
proxyFactory.init(context);
}

@Nonnull
@Override
public IAtomicLong getAtomicLong(String name) {
public IAtomicLong getAtomicLong(@Nonnull String name) {
checkNotNull(name, "Retrieving an atomic long instance with a null name is not allowed!");
return proxyFactory.createProxy(AtomicLongService.SERVICE_NAME, name);
}

@Nonnull
@Override
public <E> IAtomicReference<E> getAtomicReference(String name) {
public <E> IAtomicReference<E> getAtomicReference(@Nonnull String name) {
checkNotNull(name, "Retrieving an atomic reference instance with a null name is not allowed!");
return proxyFactory.createProxy(AtomicRefService.SERVICE_NAME, name);
}

@Nonnull
@Override
public ICountDownLatch getCountDownLatch(String name) {
public ICountDownLatch getCountDownLatch(@Nonnull String name) {
checkNotNull(name, "Retrieving a count down latch instance with a null name is not allowed!");
return proxyFactory.createProxy(CountDownLatchService.SERVICE_NAME, name);
}

@Nonnull
@Override
public FencedLock getLock(String name) {
public FencedLock getLock(@Nonnull String name) {
checkNotNull(name, "Retrieving an fenced lock instance with a null name is not allowed!");
return proxyFactory.createProxy(LockService.SERVICE_NAME, name);
}

@Nonnull
@Override
public ISemaphore getSemaphore(String name) {
public ISemaphore getSemaphore(@Nonnull String name) {
checkNotNull(name, "Retrieving a semaphore instance with a null name is not allowed!");
return proxyFactory.createProxy(SemaphoreService.SERVICE_NAME, name);
}

Expand Down
Expand Up @@ -38,6 +38,7 @@
import com.hazelcast.cp.internal.datastructures.semaphore.SemaphoreService;
import com.hazelcast.cp.lock.FencedLock;

import javax.annotation.Nonnull;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand All @@ -62,7 +63,8 @@ public void init(ClientContext context) {
this.context = context;
}

public <T extends DistributedObject> T createProxy(String serviceName, String proxyName) {
public @Nonnull
<T extends DistributedObject> T createProxy(String serviceName, String proxyName) {
proxyName = withoutDefaultGroupName(proxyName);
String objectName = getObjectNameForProxy(proxyName);

Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.hazelcast.spi.partition.IPartitionService;
import com.hazelcast.transaction.TransactionManagerService;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
Expand All @@ -55,6 +56,7 @@ public interface ClientEngine extends Consumer<ClientMessage> {
*/
boolean bind(ClientEndpoint endpoint);

@Nonnull
Collection<Client> getClients();

int getClientEndpointCount();
Expand Down
Expand Up @@ -64,6 +64,7 @@
import com.hazelcast.spi.properties.GroupProperty;
import com.hazelcast.transaction.TransactionManagerService;

import javax.annotation.Nonnull;
import javax.security.auth.login.LoginException;
import java.net.InetSocketAddress;
import java.util.Collection;
Expand Down Expand Up @@ -373,6 +374,7 @@ public void memberRemoved(MembershipServiceEvent event) {
public void memberAttributeChanged(MemberAttributeServiceEvent event) {
}

@Nonnull
@Override
public Collection<Client> getClients() {
Collection<ClientEndpoint> endpoints = endpointManager.getEndpoints();
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.hazelcast.spi.impl.eventservice.EventService;
import com.hazelcast.spi.impl.NodeEngine;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.UUID;

Expand All @@ -42,13 +43,15 @@ public ClientServiceProxy(Node node) {
this.nodeEngine = node.nodeEngine;
}

@Nonnull
@Override
public Collection<Client> getConnectedClients() {
return clientEngine.getClients();
}

@Nonnull
@Override
public UUID addClientListener(ClientListener clientListener) {
public UUID addClientListener(@Nonnull ClientListener clientListener) {
checkNotNull(clientListener, "clientListener should not be null");

EventService eventService = nodeEngine.getEventService();
Expand All @@ -58,7 +61,7 @@ public UUID addClientListener(ClientListener clientListener) {
}

@Override
public boolean removeClientListener(UUID registrationId) {
public boolean removeClientListener(@Nonnull UUID registrationId) {
checkNotNull(registrationId, "registrationId should not be null");

EventService eventService = nodeEngine.getEventService();
Expand Down
Expand Up @@ -30,6 +30,7 @@
import com.hazelcast.spi.partition.IPartitionService;
import com.hazelcast.transaction.TransactionManagerService;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
Expand All @@ -45,6 +46,7 @@ public boolean bind(ClientEndpoint endpoint) {
return true;
}

@Nonnull
@Override
public Collection<Client> getClients() {
return emptyList();
Expand Down
Expand Up @@ -27,24 +27,19 @@
import com.hazelcast.logging.LoggingService;
import com.hazelcast.internal.util.ConstructorFunction;

import javax.annotation.Nonnull;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;

import static com.hazelcast.internal.util.ConcurrencyUtil.getOrPutIfAbsent;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;

public class ClientLoggingService implements LoggingService {

private final ConcurrentMap<String, ILogger> mapLoggers = new ConcurrentHashMap<String, ILogger>(100);
private final ConcurrentMap<String, ILogger> mapLoggers = new ConcurrentHashMap<>(100);

private final ConstructorFunction<String, ILogger> loggerConstructor
= new ConstructorFunction<String, ILogger>() {

@Override
public ILogger createNew(String key) {
return new DefaultLogger(key);
}
};
private final ConstructorFunction<String, ILogger> loggerConstructor = DefaultLogger::new;

private final LoggerFactory loggerFactory;
private final BuildInfo buildInfo;
Expand All @@ -66,20 +61,24 @@ public void updateClusterName(String clusterName) {
}

@Override
public void addLogListener(Level level, LogListener logListener) {
public void addLogListener(@Nonnull Level level, @Nonnull LogListener logListener) {
throw new UnsupportedOperationException();
}

@Override
public void removeLogListener(LogListener logListener) {
public void removeLogListener(@Nonnull LogListener logListener) {
throw new UnsupportedOperationException();
}

public ILogger getLogger(String name) {
@Nonnull
public ILogger getLogger(@Nonnull String name) {
checkNotNull(name, "name must not be null");
return getOrPutIfAbsent(mapLoggers, name, loggerConstructor);
}

public ILogger getLogger(Class clazz) {
@Nonnull
public ILogger getLogger(@Nonnull Class clazz) {
checkNotNull(clazz, "class must not be null");
return getOrPutIfAbsent(mapLoggers, clazz.getName(), loggerConstructor);
}

Expand Down
Expand Up @@ -523,6 +523,7 @@ public <K, V> ReplicatedMap<K, V> getReplicatedMap(@Nonnull String name) {
return getDistributedObject(ReplicatedMapService.SERVICE_NAME, name);
}

@Nonnull
@Override
public <E> ITopic<E> getReliableTopic(@Nonnull String name) {
checkNotNull(name, "Retrieving a topic instance with a null name is not allowed!");
Expand Down Expand Up @@ -593,12 +594,14 @@ public ClientTransactionManagerService getTransactionManager() {
return transactionManager;
}

@Nonnull
@Override
public FlakeIdGenerator getFlakeIdGenerator(@Nonnull String name) {
checkNotNull(name, "Retrieving a Flake ID-generator instance with a null name is not allowed!");
return getDistributedObject(FlakeIdGeneratorService.SERVICE_NAME, name);
}

@Nonnull
@Override
public CardinalityEstimator getCardinalityEstimator(@Nonnull String name) {
checkNotNull(name, "Retrieving a cardinality estimator instance with a null name is not allowed!");
Expand All @@ -612,6 +615,7 @@ public PNCounter getPNCounter(@Nonnull String name) {
return getDistributedObject(PNCounterService.SERVICE_NAME, name);
}

@Nonnull
@Override
public IScheduledExecutorService getScheduledExecutorService(@Nonnull String name) {
checkNotNull(name, "Retrieving a scheduled executor instance with a null name is not allowed!");
Expand Down
Expand Up @@ -188,11 +188,13 @@ public TransactionContext newTransactionContext(@Nonnull TransactionOptions opti
return getClient().newTransactionContext(options);
}

@Nonnull
@Override
public FlakeIdGenerator getFlakeIdGenerator(@Nonnull String name) {
return getClient().getFlakeIdGenerator(name);
}

@Nonnull
@Override
public CardinalityEstimator getCardinalityEstimator(@Nonnull String name) {
return getClient().getCardinalityEstimator(name);
Expand Down
Expand Up @@ -28,6 +28,7 @@
import com.hazelcast.internal.util.UuidUtil;
import com.hazelcast.internal.util.executor.PoolExecutorThreadFactory;

import javax.annotation.Nonnull;
import java.util.EventListener;
import java.util.List;
import java.util.UUID;
Expand All @@ -42,6 +43,7 @@
import static com.hazelcast.core.LifecycleEvent.LifecycleState.SHUTTING_DOWN;
import static com.hazelcast.core.LifecycleEvent.LifecycleState.STARTED;
import static com.hazelcast.core.LifecycleEvent.LifecycleState.STARTING;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;
import static com.hazelcast.internal.util.StringUtil.isNullOrEmpty;

/**
Expand Down Expand Up @@ -89,15 +91,18 @@ private ILogger getLogger() {
return client.getLoggingService().getLogger(LifecycleService.class);
}

@Nonnull
@Override
public UUID addLifecycleListener(LifecycleListener lifecycleListener) {
public UUID addLifecycleListener(@Nonnull LifecycleListener lifecycleListener) {
checkNotNull(lifecycleListener, "lifecycleListener must not be null");
final UUID id = UuidUtil.newUnsecureUUID();
lifecycleListeners.put(id, lifecycleListener);
return id;
}

@Override
public boolean removeLifecycleListener(UUID registrationId) {
public boolean removeLifecycleListener(@Nonnull UUID registrationId) {
checkNotNull(registrationId, "registrationId must not be null");
return lifecycleListeners.remove(registrationId) != null;
}

Expand Down
Expand Up @@ -31,11 +31,14 @@
import com.hazelcast.partition.PartitionLostListener;
import com.hazelcast.partition.PartitionService;

import javax.annotation.Nonnull;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.hazelcast.internal.util.Preconditions.checkNotNull;

/**
* @author mdogan 5/16/13
*/
Expand All @@ -61,7 +64,8 @@ public Set<Partition> getPartitions() {
}

@Override
public Partition getPartition(Object key) {
public Partition getPartition(@Nonnull Object key) {
checkNotNull(key, "key cannot be null");
final int partitionId = partitionService.getPartitionId(key);
return partitionService.getPartition(partitionId);
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.hazelcast.transaction.impl.xa.SerializableXID;
import com.hazelcast.transaction.impl.xa.XAResourceImpl;

import javax.annotation.Nonnull;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
Expand All @@ -50,9 +51,8 @@ public class XAResourceProxy extends ClientProxy implements HazelcastXAResource

private static final int DEFAULT_TIMEOUT_SECONDS = (int) MILLISECONDS.toSeconds(TransactionOptions.DEFAULT_TIMEOUT_MILLIS);

private final ConcurrentMap<Long, TransactionContext> threadContextMap = new ConcurrentHashMap<Long, TransactionContext>();
private final ConcurrentMap<Xid, List<TransactionContext>> xidContextMap
= new ConcurrentHashMap<Xid, List<TransactionContext>>();
private final ConcurrentMap<Long, TransactionContext> threadContextMap = new ConcurrentHashMap<>();
private final ConcurrentMap<Xid, List<TransactionContext>> xidContextMap = new ConcurrentHashMap<>();
private final AtomicInteger timeoutInSeconds = new AtomicInteger(DEFAULT_TIMEOUT_SECONDS);

public XAResourceProxy(String serviceName, String objectName, ClientContext context) {
Expand All @@ -65,7 +65,7 @@ public void start(Xid xid, int flags) throws XAException {
TransactionContext threadContext = threadContextMap.get(currentThreadId());
switch (flags) {
case TMNOFLAGS:
List<TransactionContext> contexts = new CopyOnWriteArrayList<TransactionContext>();
List<TransactionContext> contexts = new CopyOnWriteArrayList<>();
List<TransactionContext> currentContexts = xidContextMap.putIfAbsent(xid, contexts);
if (currentContexts != null) {
throw new XAException("There is already TransactionContexts for the given xid: " + xid);
Expand Down Expand Up @@ -215,10 +215,15 @@ public boolean setTransactionTimeout(int seconds) throws XAException {
return true;
}

@Nonnull
@Override
public TransactionContext getTransactionContext() {
long threadId = Thread.currentThread().getId();
return threadContextMap.get(threadId);
TransactionContext transactionContext = threadContextMap.get(threadId);
if (transactionContext == null) {
throw new IllegalStateException("No TransactionContext associated with current thread: " + threadId);
}
return transactionContext;
}

private XATransactionProxy getTransaction(TransactionContext context) {
Expand Down