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

0.5 use optionals #40

Merged
merged 2 commits into from
Apr 16, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions ocpp-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@
</scm>

<dependencies>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -53,16 +67,6 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
39 changes: 25 additions & 14 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.feature.Feature;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
Expand All @@ -43,7 +45,7 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class Client
{
private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(Client.class);
private static final Logger logger = LoggerFactory.getLogger(Client.class);

private Session session;
private final IFeatureRepository featureRepository;
Expand Down Expand Up @@ -73,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 @@ -132,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
@@ -1,7 +1,8 @@
package eu.chargetime.ocpp;

import eu.chargetime.ocpp.model.*;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayDeque;

Expand Down Expand Up @@ -43,7 +44,7 @@ of this software and associated documentation files (the "Software"), to deal
* Must be overloaded to implement a specific format.
*/
public abstract class Communicator {
private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(Communicator.class);
private static final Logger logger = LoggerFactory.getLogger(Communicator.class);

private RetryRunner retryRunner;
protected Radio radio;
Expand Down
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
25 changes: 16 additions & 9 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Queue.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package eu.chargetime.ocpp;

import eu.chargetime.ocpp.model.Request;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/*
Expand Down Expand Up @@ -38,7 +40,7 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class Queue
{
private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(Queue.class);
private static final Logger logger = LoggerFactory.getLogger(Queue.class);

private HashMap<String, Request> requestQueue;

Expand All @@ -55,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();
}
}
Loading