Skip to content

Commit

Permalink
Merge pull request #40 from eupakhomov/0.5_use_optionals
Browse files Browse the repository at this point in the history
0.5 use optionals
  • Loading branch information
TVolden authored Apr 16, 2018
2 parents ae696b6 + 6f34c7f commit 14aa860
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 80 deletions.
34 changes: 22 additions & 12 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ of this software and associated documentation files (the "Software"), to deal
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -74,25 +75,33 @@ public void connect(String uri, ClientEvents events)

@Override
public void handleConfirmation(String uniqueId, Confirmation confirmation) {
CompletableFuture<Confirmation> promise = promiseRepository.getPromise(uniqueId);
if (promise != null) {
promise.complete(confirmation);
Optional<CompletableFuture<Confirmation>> promiseOptional = promiseRepository.getPromise(uniqueId);
if (promiseOptional.isPresent()) {
promiseOptional.get().complete(confirmation);
promiseRepository.removePromise(uniqueId);
} else {
logger.debug("Promise not found for confirmation {}", confirmation);
}
}

@Override
public Confirmation handleRequest(Request request) {
Feature feature = featureRepository.findFeature(request);
return feature.handleRequest(null, request);
public Confirmation handleRequest(Request request) throws UnsupportedFeatureException {
Optional<Feature> featureOptional = featureRepository.findFeature(request);
if(featureOptional.isPresent()) {
return featureOptional.get().handleRequest(null, request);
} else {
throw new UnsupportedFeatureException();
}
}

@Override
public void handleError(String uniqueId, String errorCode, String errorDescription, Object payload) {
CompletableFuture<Confirmation> promise = promiseRepository.getPromise(uniqueId);
if (promise != null) {
promise.completeExceptionally(new CallErrorException(errorCode, errorCode, payload));
Optional<CompletableFuture<Confirmation>> promiseOptional = promiseRepository.getPromise(uniqueId);
if (promiseOptional.isPresent()) {
promiseOptional.get().completeExceptionally(new CallErrorException(errorCode, errorCode, payload));
promiseRepository.removePromise(uniqueId);
} else {
logger.debug("Promise not found for error {}", errorDescription);
}
}

Expand Down Expand Up @@ -133,16 +142,17 @@ public void disconnect()
* @see CompletableFuture
*/
public CompletableFuture<Confirmation> send(Request request) throws UnsupportedFeatureException, OccurenceConstraintException {
Feature feature = featureRepository.findFeature(request);
if (feature == null)
Optional<Feature> featureOptional = featureRepository.findFeature(request);
if (!featureOptional.isPresent())
throw new UnsupportedFeatureException();

if (!request.validate())
throw new OccurenceConstraintException();

String id = session.storeRequest(request);
CompletableFuture<Confirmation> promise = promiseRepository.createPromise(id);
session.sendRequest(feature.getAction(), request, id);

session.sendRequest(featureOptional.get().getAction(), request, id);
return promise;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ of this software and associated documentation files (the "Software"), to deal

import java.util.ArrayList;
import java.util.Collections;
import java.util.Optional;

public class FeatureRepository implements IFeatureRepository {

Expand Down Expand Up @@ -62,19 +63,16 @@ public void addFeatureProfile(Profile profile) {
* Anything else will return null.
*
* @param needle Object supports {@link String}, {@link Request} or {@link Confirmation}
* @return Instance of the supported Feature
* @return Optional of instance of the supported Feature
*/
public Feature findFeature(Object needle) {
Feature output = null;

public Optional<Feature> findFeature(Object needle) {
for (Feature feature : featureList) {
if (featureContains(feature, needle)) {
output = feature;
break;
return Optional.of(feature);
}
}

return output;
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.feature.Feature;
import eu.chargetime.ocpp.feature.profile.Profile;

import java.util.Optional;

public interface IFeatureRepository {
void addFeatureProfile(Profile profile);

Feature findFeature(Object needle);
Optional<Feature> findFeature(Object needle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ of this software and associated documentation files (the "Software"), to deal

import eu.chargetime.ocpp.model.Confirmation;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public interface IPromiseRepository {
CompletableFuture<Confirmation> createPromise(String uniqueId);

CompletableFuture<Confirmation> getPromise(String uniqueId);
Optional<CompletableFuture<Confirmation>> getPromise(String uniqueId);

void removePromise(String uniqueId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;


import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/*
Expand Down Expand Up @@ -55,10 +57,10 @@ public CompletableFuture<Confirmation> createPromise(String uniqueId) {
* Get stored call back {@link CompletableFuture}.
*
* @param uniqueId identification for the {@link Request}
* @return call back {@link CompletableFuture}
* @return optional of call back {@link CompletableFuture}
*/
public CompletableFuture<Confirmation> getPromise(String uniqueId) {
return promises.get(uniqueId);
public Optional<CompletableFuture<Confirmation>> getPromise(String uniqueId) {
return Optional.ofNullable(promises.get(uniqueId));
}

/**
Expand Down
20 changes: 13 additions & 7 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

/*
Expand Down Expand Up @@ -56,27 +57,32 @@ public Queue () {
public String store(Request request) {
String ticket = UUID.randomUUID().toString();
requestQueue.put(ticket, request);

logger.debug("Queue size: {}", requestQueue.size());

return ticket;
}

/**
* Restore a {@link Request} using a unique identifier.
* The identifier can only be used once.
* If no Request was found, null is returned.
*
* FIXME: use optional instead
*
* @param ticket unique identifier returned when {@link Request} was initially stored.
* @return the stored {@link Request}
* @return the optional with stored {@link Request}
*/
public Request restoreRequest(String ticket) {
Request request = null;
public Optional<Request> restoreRequest(String ticket) {

try {
request = requestQueue.get(ticket);
Request request = requestQueue.get(ticket);
requestQueue.remove(ticket);

logger.debug("Queue size: {}", requestQueue.size());

return Optional.ofNullable(request);
} catch (Exception ex) {
logger.warn("restoreRequest({}) failed", ticket, ex);
}
return request;
return Optional.empty();
}
}
66 changes: 48 additions & 18 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/*
Expand Down Expand Up @@ -73,26 +74,51 @@ public void open(String hostname, int port, ServerEvents serverEvents) {
session.accept(new SessionEvents() {
@Override
public void handleConfirmation(String uniqueId, Confirmation confirmation) {
promiseRepository.getPromise(uniqueId).complete(confirmation);
promiseRepository.removePromise(uniqueId);

Optional<CompletableFuture<Confirmation>> promiseOptional = promiseRepository.getPromise(uniqueId);
if (promiseOptional.isPresent()) {
promiseOptional.get().complete(confirmation);
promiseRepository.removePromise(uniqueId);
} else {
logger.debug("Promise not found for confirmation {}", confirmation);
}
}

@Override
public Confirmation handleRequest(Request request) {
Feature feature = featureRepository.findFeature(request);
return feature.handleRequest(getSessionID(session), request);
public Confirmation handleRequest(Request request) throws UnsupportedFeatureException {
Optional<Feature> featureOptional = featureRepository.findFeature(request);
if(featureOptional.isPresent()) {
Optional<UUID> sessionIdOptional = getSessionID(session);
if(sessionIdOptional.isPresent()) {
return featureOptional.get().handleRequest(sessionIdOptional.get(), request);
} else {
throw new IllegalStateException("Active session not found");
}
} else {
throw new UnsupportedFeatureException();
}
}

@Override
public void handleError(String uniqueId, String errorCode, String errorDescription, Object payload) {
promiseRepository.getPromise(uniqueId).completeExceptionally(new CallErrorException(errorCode, errorCode, payload));
promiseRepository.removePromise(uniqueId);
Optional<CompletableFuture<Confirmation>> promiseOptional = promiseRepository.getPromise(uniqueId);
if(promiseOptional.isPresent()) {
promiseOptional.get().completeExceptionally(new CallErrorException(errorCode, errorCode, payload));
promiseRepository.removePromise(uniqueId);
} else {
logger.debug("Promise not found for error {}", errorDescription);
}
}

@Override
public void handleConnectionClosed() {
serverEvents.lostSession(getSessionID(session));
sessions.remove(session);
Optional<UUID> sessionIdOptional = getSessionID(session);
if(sessionIdOptional.isPresent()) {
serverEvents.lostSession(sessionIdOptional.get());
sessions.remove(sessionIdOptional.get());
} else {
logger.warn("Active session not found");
}
}

@Override
Expand All @@ -101,21 +127,21 @@ public void handleConnectionOpened() {
}
});
sessions.put(UUID.randomUUID(), session);
serverEvents.newSession(getSessionID(session), information);
serverEvents.newSession(getSessionID(session).get(), information);
});
}

private UUID getSessionID(ISession session) {
private Optional<UUID> getSessionID(ISession session) {

if (!sessions.containsValue(session))
return null;
return Optional.empty();

for (Map.Entry<UUID, ISession> entry : sessions.entrySet()) {
if (entry.getValue() == session)
return entry.getKey();
return Optional.of(entry.getKey());
}

return null;
return Optional.empty();
}

/**
Expand All @@ -135,17 +161,21 @@ public void close() {
* @throws OccurenceConstraintException Thrown if the request isn't valid.
*/
public CompletableFuture<Confirmation> send(UUID sessionIndex, Request request) throws UnsupportedFeatureException, OccurenceConstraintException {
Feature feature = featureRepository.findFeature(request);
if (feature == null)
Optional<Feature> featureOptional = featureRepository.findFeature(request);
if (!featureOptional.isPresent()) {
throw new UnsupportedFeatureException();
}

if (!request.validate())
if (!request.validate()) {
throw new OccurenceConstraintException();
}

ISession session = sessions.get(sessionIndex);


String id = session.storeRequest(request);
CompletableFuture<Confirmation> promise = promiseRepository.createPromise(id);
session.sendRequest(feature.getAction(), request, id);
session.sendRequest(featureOptional.get().getAction(), request, id);
return promise;
}

Expand Down
Loading

0 comments on commit 14aa860

Please sign in to comment.