Skip to content

Commit

Permalink
Code sanitation
Browse files Browse the repository at this point in the history
Signed-off-by: Philipp Michalski <Philipp.Michalski@bosch-si.com>
  • Loading branch information
Philipp Michalski authored and Philipp Michalski committed Feb 21, 2018
1 parent 5f60ded commit c2877b7
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ public class InternalMessage {

private enum Type {
TEXT,
BYTES
BYTES,
UNKNOWN
}

private final Map<String, String> headers;
private final Type type;

@Nullable
private final String textPayload;
@Nullable
private final ByteBuffer bytePayload;
private final Type type;

private InternalMessage(final Builder builder) {
this.headers = Collections.unmodifiableMap(new LinkedHashMap<>(builder.headers));
this.type = builder.type;
this.textPayload = builder.textPayload;
this.bytePayload = builder.bytePayload;
this.type = builder.type;

}

public Map<String, String> getHeaders() {
Expand All @@ -51,6 +56,11 @@ public Optional<String> findHeader(final String key) {
return Optional.ofNullable(headers.get(key)).filter(s -> !s.isEmpty());
}

public Optional<String> findHeaderIgnoreCase(final String key) {
return headers.entrySet().stream().filter(e -> key.equalsIgnoreCase(e.getKey())).findFirst()
.map(Map.Entry::getValue);
}

public boolean isTextMessage() {
return Type.TEXT.equals(type);
}
Expand Down Expand Up @@ -80,38 +90,43 @@ public boolean equals(final Object o) {

@Override
public int hashCode() {

return Objects.hash(headers, textPayload, bytePayload, type);
}



public static class Builder {

private Map<String, String> headers;
private final Map<String, String> headers;
private Type type = Type.UNKNOWN;
@Nullable
private String textPayload;
@Nullable
private ByteBuffer bytePayload;
private Type type;

public Builder(final Map<String, String> headers) {
this.headers = headers;
}

public Builder withText(@Nullable final String text) {
this.textPayload = text;
this.type = Type.TEXT;
this.textPayload = text;
this.bytePayload = null;
return this;
}

public Builder withBytes(@Nullable final byte[] bytes) {
withBytes(ByteBuffer.wrap(bytes));
if (Objects.isNull(bytes)) {
withBytes((ByteBuffer) null);
} else {
withBytes(ByteBuffer.wrap(bytes));
}
return this;
}

public Builder withBytes(@Nullable final ByteBuffer bytes) {
this.bytePayload = bytes;
this.type = Type.BYTES;
this.bytePayload = bytes;
this.textPayload = null;
return this;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/**
* TODO doc
*/
@Deprecated
final class ImmutablePayloadMapperMessage implements PayloadMapperMessage {

@Nullable private final String contentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/**
* TODO doc
*/
@Deprecated
final class ImmutablePayloadMapperOptions implements PayloadMapperOptions {

private final Map<String, String> optionsMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
*/
package org.eclipse.ditto.services.amqpbridge.mapping.mapper;

import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotEmpty;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.eclipse.ditto.model.amqpbridge.InternalMessage;
import org.eclipse.ditto.protocoladapter.Adaptable;
Expand All @@ -40,7 +39,7 @@ public abstract class MessageMapper extends Converter<InternalMessage, Adaptable
* The message content type expected by this mapper.
* Not final as it might be set via dynamic configuration.
*/
@SuppressWarnings({"CanBeFinal", "NullableProblems"})
@Nullable
private String contentType;

/**
Expand All @@ -49,6 +48,7 @@ public abstract class MessageMapper extends Converter<InternalMessage, Adaptable
*/
private boolean isContentTypeRequired;

@Nullable
public final String getContentType() {
return contentType;
}
Expand All @@ -59,8 +59,7 @@ public final String getContentType() {
* @throws IllegalArgumentException if contentType is null or empty
*/
@SuppressWarnings("WeakerAccess")
protected void setContentType(@Nonnull final String contentType) {
checkNotEmpty(contentType, "contentType");
protected void setContentType(final String contentType) {
this.contentType = contentType;
}

Expand All @@ -86,7 +85,6 @@ protected final void setContentTypeRequired(final boolean contentTypeRequired) {
@SuppressWarnings("WeakerAccess")
protected void requireMatchingContentType(final InternalMessage internalMessage) {
if (isContentTypeRequired()) {
//noinspection ConstantConditions (will be set in subclasses)
if (Objects.isNull(contentType) || contentType.isEmpty()) {
throw new IllegalArgumentException(String.format("A matching content type is required, but none configured. Set a content type with the following key in configuration: %s",
CONTENT_TYPE_KEY));
Expand All @@ -111,10 +109,7 @@ protected void requireMatchingContentType(final InternalMessage internalMessage)
*/
public static Optional<String> findContentType(final InternalMessage internalMessage) {
checkNotNull(internalMessage);
return internalMessage.getHeaders().entrySet().stream()
.filter(e -> CONTENT_TYPE_KEY.equalsIgnoreCase(e.getKey()))
.findFirst()
.map(Map.Entry::getValue);
return internalMessage.findHeaderIgnoreCase(CONTENT_TYPE_KEY);
}

/**
Expand All @@ -136,7 +131,7 @@ public static Optional<String> findContentType(final Adaptable adaptable) {
* @param configuration the configuration
* @throws IllegalArgumentException if the configuration is invalid.
*/
public final void configure(@Nonnull final MessageMapperConfiguration configuration){
public final void configure(final MessageMapperConfiguration configuration){
checkNotNull(configuration);
doConfigure(configuration);

Expand All @@ -149,11 +144,11 @@ public final void configure(@Nonnull final MessageMapperConfiguration configurat
setContentType(contentTypeValue);
}

protected abstract void doConfigure(@Nonnull final MessageMapperConfiguration configuration);
protected abstract void doConfigure(final MessageMapperConfiguration configuration);

protected abstract Adaptable doForwardMap(final InternalMessage internalMessage);
protected abstract Adaptable doForwardMap(@Nullable final InternalMessage internalMessage);

protected abstract InternalMessage doBackwardMap(final Adaptable adaptable);
protected abstract InternalMessage doBackwardMap(@Nullable final Adaptable adaptable);

@Override
protected final Adaptable doForward(final InternalMessage internalMessage) {
Expand All @@ -166,9 +161,8 @@ protected final InternalMessage doBackward(final Adaptable adaptable) {
return doBackwardMap(adaptable);
}

@SuppressWarnings("NullableProblems")
@Override
public boolean equals(final Object o) {
public boolean equals(@Nullable final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.eclipse.ditto.model.amqpbridge.MappingContext;
Expand All @@ -39,7 +38,7 @@
/**
* Encapsulates responsibility for instantiating {@link MessageMapper} objects.
*
* As the message mapper instantiation is usually triggered by an actor, there are only limitited possiblilities of
* As the message mapper instantiation is usually triggered by an actor, there are only limited possibilities of
* logging fine grained errors and at the same time keep all responsibility for mapper instantiation behavior away
* from the actor.
* Due to this, the factory can be instantiated with a reference to the actors log adapter and will log problems to
Expand Down Expand Up @@ -167,7 +166,7 @@ Optional<MessageMapper> findClassAndCreateInstance(final MappingContext mappingC

/**
* Instantiates message mappers from a list of contexts. If there is no mapper for a context or the instantiation
* process fails, then the context will be ignored and all exceptions are catched and logged! Compare list
* process fails, then the context will be ignored and all exceptions are suppressed and logged! Compare list
* lengths to ensure everything worked.
*
* @param contexts the contexts
Expand Down Expand Up @@ -205,9 +204,7 @@ public MessageMapperRegistry loadRegistry(final MessageMapper defaultMapper,
// --

@Nullable
private MessageMapper configureInstance(@Nonnull final MessageMapper mapper,
@Nonnull final MessageMapperConfiguration options)
{
private MessageMapper configureInstance(final MessageMapper mapper, final MessageMapperConfiguration options) {
try {
mapper.configure(options);
return mapper;
Expand Down Expand Up @@ -255,16 +252,13 @@ private static boolean isFactoryMethod(final Method m) {
return m.getReturnType().equals(MessageMapper.class) && m.getParameterTypes().length == 0;
}

@SuppressWarnings("ConstantConditions")
private void tryLogWarning(String template, Object... args) {
//noinspection ConstantConditions
if (Objects.isNull(logDebug)) return;
logDebug.accept(MessageFormat.format(template, args));
if (Objects.isNull(logWarning)) return;
logWarning.accept(MessageFormat.format(template, args));
}

private void tryLogDebug(String template, Object... args) {
//noinspection ConstantConditions
if (Objects.isNull(logWarning)) return;
logWarning.accept(MessageFormat.format(template, args));
if (Objects.isNull(logDebug)) return;
logDebug.accept(MessageFormat.format(template, args));
}
}
Loading

0 comments on commit c2877b7

Please sign in to comment.