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

#4414 implement Set #2

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
171 changes: 115 additions & 56 deletions server/src/com/mirth/connect/server/userutil/DestinationSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.mozilla.javascript.Context;

Expand All @@ -24,9 +27,9 @@
* Utility class used in the preprocessor or source filter/transformer to prevent the message from
* being sent to specific destinations.
*/
public class DestinationSet {
public class DestinationSet implements Set<Integer> {

private Map<String, Integer> destinationIdMap;
private Map<String, Integer> destinationIdMap = Collections.emptyMap();
private Set<Integer> metaDataIds;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also default to Collections.emptySet() ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as we use its add() which emptySet() doesn't implement


/**
Expand All @@ -43,6 +46,7 @@ public DestinationSet(ImmutableConnectorMessage connectorMessage) {
this.metaDataIds = (Set<Integer>) connectorMessage.getSourceMap().get(Constants.DESTINATION_SET_KEY);
}
} catch (Exception e) {
metaDataIds = new HashSet<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the catch block even necessary here?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the rare case of a ClassCastException when pulling out the SourceMap. Until they fix that, it's worth leaving.

}
}

Expand All @@ -56,15 +60,7 @@ public DestinationSet(ImmutableConnectorMessage connectorMessage) {
* from processing for this message.
*/
public boolean remove(Object metaDataIdOrConnectorName) {
if (metaDataIds != null) {
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);

if (metaDataId != null) {
return metaDataIds.remove(metaDataId);
}
}

return false;
return remove(Collections.singleton(metaDataIdOrConnectorName));
}

/**
Expand All @@ -77,15 +73,15 @@ public boolean remove(Object metaDataIdOrConnectorName) {
* from processing for this message.
*/
public boolean remove(Collection<Object> metaDataIdOrConnectorNames) {
boolean removed = false;

for (Object metaDataIdOrConnectorName : metaDataIdOrConnectorNames) {
if (remove(metaDataIdOrConnectorName)) {
removed = true;
}
}

return removed;
if(metaDataIdOrConnectorNames == null) { return false; }

return metaDataIdOrConnectorNames.stream()
.map(this::convertToMetaDataId)
.filter(Optional::isPresent)
.map(Optional::get)
.map(metaDataIds::remove)
.filter(Boolean::booleanValue)
.count() > 0;
}

/**
Expand All @@ -98,15 +94,7 @@ public boolean remove(Collection<Object> metaDataIdOrConnectorNames) {
* from processing for this message.
*/
public boolean removeAllExcept(Object metaDataIdOrConnectorName) {
if (metaDataIds != null) {
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);

if (metaDataId != null) {
return metaDataIds.retainAll(Collections.singleton(metaDataId));
}
}

return false;
return removeAllExcept(Collections.singleton(metaDataIdOrConnectorName));
}

/**
Expand All @@ -119,21 +107,15 @@ public boolean removeAllExcept(Object metaDataIdOrConnectorName) {
* from processing for this message.
*/
public boolean removeAllExcept(Collection<Object> metaDataIdOrConnectorNames) {
if (metaDataIds != null) {
Set<Integer> set = new HashSet<Integer>();

for (Object metaDataIdOrConnectorName : metaDataIdOrConnectorNames) {
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);

if (metaDataId != null) {
set.add(metaDataId);
}
}

return metaDataIds.retainAll(set);
}

return false;
if(metaDataIdOrConnectorNames == null) { return false; }

Set<Integer> set = metaDataIdOrConnectorNames.stream()
rogin marked this conversation as resolved.
Show resolved Hide resolved
.map(this::convertToMetaDataId)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());

return metaDataIds.retainAll(set);
}

/**
Expand All @@ -144,25 +126,102 @@ public boolean removeAllExcept(Collection<Object> metaDataIdOrConnectorNames) {
* from processing for this message.
*/
public boolean removeAll() {
if (metaDataIds != null && metaDataIds.size() > 0) {
metaDataIds.clear();
return true;
}

return false;
int origSize = size();
clear();
return origSize > 0;
}

private Integer convertToMetaDataId(Object metaDataIdOrConnectorName) {
private Optional<Integer> convertToMetaDataId(Object metaDataIdOrConnectorName) {
Integer result = null;

if (metaDataIdOrConnectorName != null) {
if (metaDataIdOrConnectorName instanceof Number) {
return ((Number) metaDataIdOrConnectorName).intValue();
result = Integer.valueOf(((Number) metaDataIdOrConnectorName).intValue());
} else if (metaDataIdOrConnectorName.getClass().getName().equals("org.mozilla.javascript.NativeNumber")) {
return (Integer) Context.jsToJava(metaDataIdOrConnectorName, int.class);
} else if (destinationIdMap != null) {
return destinationIdMap.get(metaDataIdOrConnectorName.toString());
result = (Integer) Context.jsToJava(metaDataIdOrConnectorName, int.class);
} else {
result = destinationIdMap.get(metaDataIdOrConnectorName.toString());
}
}

return Optional.ofNullable(result);
}

@Override
public int size() {
return metaDataIds.size();
}

@Override
public boolean isEmpty() {
return metaDataIds.isEmpty();
}

@Override
public boolean contains(Object metaDataIdOrConnectorName) {
Optional<Integer> m = convertToMetaDataId(metaDataIdOrConnectorName);

return m.isPresent() && metaDataIds.contains(m.get());
}

@Override
public Iterator<Integer> iterator() {
return Collections.unmodifiableSet(metaDataIds).iterator();
}

@Override
public Object[] toArray() {
return metaDataIds.toArray();
}

@Override
public <T> T[] toArray(T[] a) {
return metaDataIds.toArray(a);
}

@Override
public boolean add(Integer metaDataId) {
return metaDataId != null && metaDataIds.add(metaDataId);
}

@Override
public boolean containsAll(Collection<?> metaDataIdOrConnectorNames) {
if(metaDataIdOrConnectorNames == null) { return false; }

return metaDataIdOrConnectorNames.stream()
.map(this::contains)
.allMatch(Boolean::booleanValue);
}

@Override
public boolean addAll(Collection<? extends Integer> metaDataIdOrConnectorNames) {
boolean changed = false;

if(metaDataIdOrConnectorNames != null) {
for(Object item : metaDataIdOrConnectorNames) {
Optional<Integer> m = convertToMetaDataId(item);

if(m.isPresent() && metaDataIds.add(m.get())) {
changed = true;
}
}
}

return null;
return changed;
}

@Override
public boolean retainAll(Collection<?> metaDataIdOrConnectorNames) {
return removeAllExcept((Collection<Object>)metaDataIdOrConnectorNames);
}

@Override
public boolean removeAll(Collection<?> metaDataIdOrConnectorNames) {
return remove((Collection<Object>)metaDataIdOrConnectorNames);
}

@Override
public void clear() {
metaDataIds.clear();
}
}
Loading