Skip to content

Commit

Permalink
refactor for more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rogin committed Aug 8, 2023
1 parent 4752e4e commit 33977cb
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 54 deletions.
122 changes: 92 additions & 30 deletions server/src/com/mirth/connect/server/userutil/DestinationSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -59,10 +61,12 @@ public DestinationSet(ImmutableConnectorMessage connectorMessage) {
* from processing for this message.
*/
public boolean remove(Object metaDataIdOrConnectorName) {
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);
//return remove(Collections.singleton(metaDataIdOrConnectorName));

if (metaDataId != null) {
return metaDataIds.remove(metaDataId);
Optional<Integer> metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);

if (metaDataId.isPresent()) {
return metaDataIds.remove(metaDataId.get());
}

return false;
Expand All @@ -78,9 +82,32 @@ public boolean remove(Object metaDataIdOrConnectorName) {
* from processing for this message.
*/
public boolean remove(Collection<Object> metaDataIdOrConnectorNames) {
return metaDataIdOrConnectorNames.stream()
.map(m -> remove(m))
.anyMatch(b -> b == true);
// return metaDataIdOrConnectorNames.stream()
// .map(this::convertToMetaDataId)
// .filter(Optional::isPresent)
// .map(Optional::get)
// .map(metaDataIds::remove)
// .filter(Boolean::booleanValue)
// .count() > 0;

// boolean removed = false;
// for(Object item : metaDataIdOrConnectorNames) {
// Optional<Integer> m = convertToMetaDataId(item);
// if(m.isPresent() && metaDataIds.remove(m.get())) {
// removed = true;
// }
// }
// return removed;

boolean removed = false;

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

return removed;
}

/**
Expand All @@ -93,13 +120,9 @@ public boolean remove(Collection<Object> metaDataIdOrConnectorNames) {
* from processing for this message.
*/
public boolean removeAllExcept(Object metaDataIdOrConnectorName) {
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);
Optional<Integer> metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);

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

return false;
return metaDataId.isPresent() && metaDataIds.retainAll(Collections.singleton(metaDataId.get()));
}

/**
Expand All @@ -113,8 +136,9 @@ public boolean removeAllExcept(Object metaDataIdOrConnectorName) {
*/
public boolean removeAllExcept(Collection<Object> metaDataIdOrConnectorNames) {
Set<Integer> set = metaDataIdOrConnectorNames.stream()
.map(m -> convertToMetaDataId(m))
.filter(m -> m != null)
.map(this::convertToMetaDataId)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());

return metaDataIds.retainAll(set);
Expand All @@ -133,18 +157,20 @@ public boolean removeAll() {
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);
result = (Integer) Context.jsToJava(metaDataIdOrConnectorName, int.class);
} else {
return destinationIdMap.get(metaDataIdOrConnectorName.toString());
result = destinationIdMap.get(metaDataIdOrConnectorName.toString());
}
}

return null;
return Optional.ofNullable(result);
}

@Override
Expand All @@ -159,9 +185,7 @@ public boolean isEmpty() {

@Override
public boolean contains(Object metaDataIdOrConnectorName) {
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);

return metaDataId != null && metaDataIds.contains(metaDataId);
return containsAll(Collections.singleton(metaDataIdOrConnectorName));
}

@Override
Expand All @@ -181,22 +205,60 @@ public <T> T[] toArray(T[] a) {

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

@Override
public boolean containsAll(Collection<?> metaDataIdOrConnectorNames) {
return metaDataIdOrConnectorNames.stream()
.map(m -> convertToMetaDataId(m))
.filter(m -> m != null)
.allMatch(m -> metaDataIds.contains(m));
Set<Integer> set = metaDataIdOrConnectorNames.stream()
.map(this::convertToMetaDataId)
.filter(Optional::isPresent)
.map(Optional::get)
//can't do allMatch as a null input creates an empty stream, which returns true
//.allMatch(metaDataIds::contains);
.collect(Collectors.toSet());

return !set.isEmpty() && metaDataIds.containsAll(set);
}

@Override
public boolean addAll(Collection<? extends Integer> metaDataIdOrConnectorNames) {
return metaDataIdOrConnectorNames.stream()
.map(m -> add(m))
.anyMatch(b -> b == true);
// return metaDataIdOrConnectorNames.stream()
// .map(this::convertToMetaDataId)
// .filter(Optional::isPresent)
// .map(Optional::get)
// .map(this::add)
// .filter(Boolean::booleanValue)
// .count() > 0;

//TODO get back to this one
// List<Integer> list = metaDataIdOrConnectorNames.stream()
// .map(this::convertToMetaDataId)
// .filter(Optional::isPresent)
// .map(Optional::get)
// .collect(Collectors.toList());

// return !list.isEmpty() && metaDataIds.addAll(list);

boolean changed = false;

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

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

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

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

return changed;
}

@Override
Expand Down
Loading

0 comments on commit 33977cb

Please sign in to comment.