Skip to content

Commit

Permalink
[GR-30030] Fixed review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzezula committed Mar 22, 2021
1 parent 57524b1 commit 921393b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,18 @@ private static ParameterizedTypeDescriptorImpl asParameterizedTypeImpl(TypeDescr
* <li>If this and {@code toRemove} types are parameterized types (array, iterable, iterator,
* hash) of the same kind the type parameter types are subtracted applying the same rules.</li>
* </ul>
* <p>
* Examples:
* <ul>
* <li>NUMBER.subtract(NUMBER) -> no type</li>
* <li>NUMBER.subtract(STRING) -> NUMBER</li>
* <li>UNION[NUMBER|STRING|OBJECT].subtract(NUMBER) -> UNION[STRING|OBJECT]</li>
* <li>UNION[NUMBER|STRING|OBJECT].subtract(UNION[NUMBER|STRING]) -> OBJECT</li>
* <li>INTERSECTION[NUMBER&STRING&OBJECT].subtract(NUMBER) -> INTERSECTION[STRING&OBJECT]</li>
* <li>INTERSECTION[NUMBER&STRING&OBJECT].subtract(INTERSECTION[NUMBER&STRING]) -> OBJECT</li>
* <li>ARRAY[UNION[NUMBER|STRING]].subtract(ARRAY[NUMBER]) -> ARRAY[STRING]</li>
* <li>ARRAY[INTERSECTION[NUMBER|STRING]].subtract(ARRAY[NUMBER]) -> ARRAY[STRING]</li>
* </ul>
*
* @param toRemove the type to remove.
* @since 20.2
Expand Down Expand Up @@ -1520,7 +1532,15 @@ TypeDescriptorImpl create(Set<TypeDescriptorImpl> subTypes) {
case 1:
return subTypes.iterator().next();
default:
return new IntersectionImpl(subTypes);
Set<TypeDescriptorImpl> useSubTypes = new HashSet<>();
for (TypeDescriptorImpl subType : subTypes) {
if (subType.getClass() == IntersectionImpl.class) {
useSubTypes.addAll(((IntersectionImpl) subType).types);
} else {
useSubTypes.add(subType);
}
}
return new IntersectionImpl(useSubTypes);
}
}

Expand Down Expand Up @@ -1613,6 +1633,14 @@ TypeDescriptorImpl create(Set<TypeDescriptorImpl> subTypes) {
case 1:
return subTypes.iterator().next();
default:
Set<TypeDescriptorImpl> useSubTypes = new HashSet<>();
for (TypeDescriptorImpl subType : subTypes) {
if (subType.getClass() == UnionImpl.class) {
useSubTypes.addAll(((UnionImpl) subType).types);
} else {
useSubTypes.add(subType);
}
}
return new UnionImpl(subTypes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ public void testSubtract() {
TypeDescriptor expected = predefinedAndNoType.subtract(TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.EXECUTABLE, TypeDescriptor.INSTANTIABLE));
Assert.assertEquals(expected, TypeDescriptor.ANY.subtract(TypeDescriptor.NUMBER));
Assert.assertEquals(noType, TypeDescriptor.ANY.subtract(predefinedAndNoType));

// Examples
TypeDescriptor unionNumOrStrOrObj = TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.STRING, TypeDescriptor.OBJECT);
Assert.assertEquals(TypeDescriptor.union(TypeDescriptor.STRING, TypeDescriptor.OBJECT), unionNumOrStrOrObj.subtract(TypeDescriptor.NUMBER));
Assert.assertEquals(TypeDescriptor.OBJECT, unionNumOrStrOrObj.subtract(TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.STRING)));
TypeDescriptor intersectionNumOrStrOrObj = TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING, TypeDescriptor.OBJECT);
Assert.assertEquals(TypeDescriptor.intersection(TypeDescriptor.STRING, TypeDescriptor.OBJECT), intersectionNumOrStrOrObj.subtract(TypeDescriptor.NUMBER));
Assert.assertEquals(TypeDescriptor.OBJECT, intersectionNumOrStrOrObj.subtract(TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING)));
Assert.assertEquals(TypeDescriptor.array(TypeDescriptor.STRING),
TypeDescriptor.array(TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.STRING)).subtract(TypeDescriptor.array(TypeDescriptor.NUMBER)));
Assert.assertEquals(TypeDescriptor.array(TypeDescriptor.STRING),
TypeDescriptor.array(TypeDescriptor.intersection(TypeDescriptor.NUMBER, TypeDescriptor.STRING)).subtract(TypeDescriptor.array(TypeDescriptor.NUMBER)));
}

@Test
Expand Down

0 comments on commit 921393b

Please sign in to comment.