Skip to content

Commit b0e520b

Browse files
committed
Removed more .streams with our filterAndMap code
1 parent d9ef509 commit b0e520b

File tree

8 files changed

+82
-58
lines changed

8 files changed

+82
-58
lines changed

src/main/java/graphql/collect/ImmutableKit.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,27 @@ public static <T> ImmutableSet<T> addToSet(Collection<? extends T> existing, T n
185185
return newSet.build();
186186
}
187187

188-
}
188+
189+
/**
190+
* Filters a variable args array to a list
191+
*
192+
* @param filter the predicate the filter with
193+
* @param args the variable args
194+
* @param <T> fot two
195+
*
196+
* @return a filtered list
197+
*/
198+
@SafeVarargs
199+
public static <T> List<T> filterVarArgs(Predicate<? super T> filter, T... args) {
200+
if (args.length == 0) {
201+
return ImmutableList.of();
202+
}
203+
ImmutableList.Builder<T> builder = ImmutableList.builderWithExpectedSize(args.length);
204+
for (T arg : args) {
205+
if (filter.test(arg)) {
206+
builder.add(arg);
207+
}
208+
}
209+
return builder.build();
210+
}
211+
}

src/main/java/graphql/introspection/Introspection.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import graphql.GraphQLContext;
88
import graphql.Internal;
99
import graphql.PublicApi;
10+
import graphql.collect.ImmutableKit;
1011
import graphql.execution.ExecutionContext;
1112
import graphql.execution.MergedField;
1213
import graphql.execution.MergedSelectionSet;
@@ -48,7 +49,6 @@
4849
import java.util.Set;
4950
import java.util.concurrent.atomic.AtomicBoolean;
5051
import java.util.function.Function;
51-
import java.util.stream.Collectors;
5252

5353
import static graphql.Assert.assertTrue;
5454
import static graphql.Scalars.GraphQLBoolean;
@@ -356,9 +356,8 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
356356
Object type = environment.getSource();
357357
GraphQLFieldDefinition fieldDef = (GraphQLFieldDefinition) type;
358358
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
359-
return fieldDef.getArguments().stream()
360-
.filter(arg -> includeDeprecated || !arg.isDeprecated())
361-
.collect(Collectors.toList());
359+
return ImmutableKit.filter(fieldDef.getArguments(),
360+
arg -> includeDeprecated || !arg.isDeprecated());
362361
};
363362
register(__Field, "name", nameDataFetcher);
364363
register(__Field, "description", descriptionDataFetcher);
@@ -406,9 +405,8 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
406405
if (includeDeprecated) {
407406
return fieldDefinitions;
408407
}
409-
return fieldDefinitions.stream()
410-
.filter(field -> !field.isDeprecated())
411-
.collect(Collectors.toList());
408+
return ImmutableKit.filter(fieldDefinitions,
409+
field -> !field.isDeprecated());
412410
}
413411
return null;
414412
};
@@ -444,9 +442,8 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
444442
if (includeDeprecated) {
445443
return values;
446444
}
447-
return values.stream()
448-
.filter(enumValue -> !enumValue.isDeprecated())
449-
.collect(Collectors.toList());
445+
return ImmutableKit.filter(values,
446+
enumValue -> !enumValue.isDeprecated());
450447
}
451448
return null;
452449
};
@@ -463,9 +460,8 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
463460
if (includeDeprecated) {
464461
return inputFields;
465462
}
466-
return inputFields
467-
.stream().filter(inputField -> !inputField.isDeprecated())
468-
.collect(Collectors.toList());
463+
return ImmutableKit.filter(inputFields,
464+
inputField -> !inputField.isDeprecated());
469465
}
470466
return null;
471467
};
@@ -650,9 +646,8 @@ public enum DirectiveLocation {
650646
IntrospectionDataFetcher<?> argsDataFetcher = environment -> {
651647
GraphQLDirective directive = environment.getSource();
652648
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
653-
return directive.getArguments().stream()
654-
.filter(arg -> includeDeprecated || !arg.isDeprecated())
655-
.collect(Collectors.toList());
649+
return ImmutableKit.filter(directive.getArguments(),
650+
arg -> includeDeprecated || !arg.isDeprecated());
656651
};
657652
register(__Directive, "name", nameDataFetcher);
658653
register(__Directive, "description", descriptionDataFetcher);

src/main/java/graphql/introspection/IntrospectionQueryBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.google.common.collect.ImmutableList;
44
import graphql.PublicApi;
5+
import graphql.collect.ImmutableKit;
56
import graphql.language.AstPrinter;
67
import graphql.language.BooleanValue;
78
import graphql.language.Document;
89
import graphql.language.OperationDefinition;
910
import graphql.language.SelectionSet;
1011

11-
import java.util.Arrays;
1212
import java.util.List;
1313
import java.util.Objects;
1414

@@ -20,7 +20,6 @@
2020
import static graphql.language.OperationDefinition.newOperationDefinition;
2121
import static graphql.language.SelectionSet.newSelectionSet;
2222
import static graphql.language.TypeName.newTypeName;
23-
import static java.util.stream.Collectors.toList;
2423

2524
/**
2625
* {@link IntrospectionQueryBuilder} allows you to build introspection queries controlled
@@ -152,6 +151,7 @@ public Options isOneOf(boolean flag) {
152151
this.inputValueDeprecation,
153152
this.typeRefFragmentDepth);
154153
}
154+
155155
/**
156156
* This will allow you to include the `isRepeatable` field for directives in the introspection query.
157157
*
@@ -223,7 +223,7 @@ public Options typeRefFragmentDepth(int typeRefFragmentDepth) {
223223

224224
@SafeVarargs
225225
private static <T> List<T> filter(T... args) {
226-
return Arrays.stream(args).filter(Objects::nonNull).collect(toList());
226+
return ImmutableKit.filterVarArgs(Objects::nonNull, args);
227227
}
228228

229229
/**

src/main/java/graphql/introspection/IntrospectionWithDirectivesSupport.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import graphql.DirectivesUtil;
55
import graphql.PublicApi;
66
import graphql.PublicSpi;
7+
import graphql.collect.ImmutableKit;
78
import graphql.execution.ValuesResolver;
89
import graphql.language.AstPrinter;
910
import graphql.language.Node;
@@ -41,7 +42,6 @@
4142
import static graphql.schema.GraphQLNonNull.nonNull;
4243
import static graphql.schema.GraphQLObjectType.newObject;
4344
import static graphql.util.TraversalControl.CONTINUE;
44-
import static java.util.stream.Collectors.toList;
4545

4646
/**
4747
* The graphql specification does not allow you to retrieve the directives and their argument values that
@@ -171,9 +171,9 @@ private GraphQLObjectType mkAppliedDirectiveType(String name, GraphQLType direct
171171
}
172172

173173
private GraphQLSchema addDirectiveDefinitionFilter(GraphQLSchema schema) {
174-
DataFetcher<?> df = env -> {
174+
DataFetcher<?> df = env -> {
175175
List<GraphQLDirective> definedDirectives = env.getGraphQLSchema().getDirectives();
176-
return filterDirectives(schema,true, null, definedDirectives);
176+
return filterDirectives(schema, true, null, definedDirectives);
177177
};
178178
GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry().transform(bld ->
179179
bld.dataFetcher(coordinates(__Schema, "directives"), df));
@@ -199,8 +199,8 @@ private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, G
199199
DataFetcher<?> argsDF = env -> {
200200
final GraphQLAppliedDirective directive = env.getSource();
201201
// we only show directive arguments that have values set on them
202-
return directive.getArguments().stream()
203-
.filter(arg -> arg.getArgumentValue().isSet());
202+
return ImmutableKit.filter(directive.getArguments(),
203+
arg -> arg.getArgumentValue().isSet());
204204
};
205205
DataFetcher<?> argValueDF = env -> {
206206
final GraphQLAppliedDirectiveArgument argument = env.getSource();
@@ -225,17 +225,19 @@ private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, G
225225
}
226226

227227
private List<GraphQLDirective> filterDirectives(GraphQLSchema schema, boolean isDefinedDirective, GraphQLDirectiveContainer container, List<GraphQLDirective> directives) {
228-
return directives.stream().filter(directive -> {
229-
DirectivePredicateEnvironment env = buildDirectivePredicateEnv(schema, isDefinedDirective, container, directive.getName());
230-
return directivePredicate.isDirectiveIncluded(env);
231-
}).collect(toList());
228+
return ImmutableKit.filter(directives,
229+
directive -> {
230+
DirectivePredicateEnvironment env = buildDirectivePredicateEnv(schema, isDefinedDirective, container, directive.getName());
231+
return directivePredicate.isDirectiveIncluded(env);
232+
});
232233
}
233234

234235
private List<GraphQLAppliedDirective> filterAppliedDirectives(GraphQLSchema schema, boolean isDefinedDirective, GraphQLDirectiveContainer container, List<GraphQLAppliedDirective> directives) {
235-
return directives.stream().filter(directive -> {
236-
DirectivePredicateEnvironment env = buildDirectivePredicateEnv(schema, isDefinedDirective, container, directive.getName());
237-
return directivePredicate.isDirectiveIncluded(env);
238-
}).collect(toList());
236+
return ImmutableKit.filter(directives,
237+
directive -> {
238+
DirectivePredicateEnvironment env = buildDirectivePredicateEnv(schema, isDefinedDirective, container, directive.getName());
239+
return directivePredicate.isDirectiveIncluded(env);
240+
});
239241
}
240242

241243
@NonNull

src/main/java/graphql/schema/idl/SchemaParseOrder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.schema.idl;
22

33
import com.google.common.collect.ImmutableMap;
4+
import graphql.collect.ImmutableKit;
45
import graphql.language.SDLDefinition;
56
import graphql.language.SDLNamedDefinition;
67
import graphql.language.SourceLocation;
@@ -21,7 +22,6 @@
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.StringJoiner;
24-
import java.util.stream.Collectors;
2525

2626
import static java.util.Optional.ofNullable;
2727

@@ -53,10 +53,9 @@ public Map<String, List<SDLDefinition<?>>> getInOrder() {
5353
public Map<String, List<SDLNamedDefinition<?>>> getInNameOrder() {
5454
Map<String, List<SDLNamedDefinition<?>>> named = new LinkedHashMap<>();
5555
definitionOrder.forEach((location, def) -> {
56-
List<SDLNamedDefinition<?>> namedDefs = def.stream()
57-
.filter(d -> d instanceof SDLNamedDefinition)
58-
.map(d -> (SDLNamedDefinition<?>) d)
59-
.collect(Collectors.toList());
56+
List<SDLNamedDefinition<?>> namedDefs = ImmutableKit.filterAndMap(def,
57+
d -> d instanceof SDLNamedDefinition,
58+
d -> (SDLNamedDefinition<?>) d);
6059
named.put(location, namedDefs);
6160
});
6261
return named;

src/main/java/graphql/schema/idl/SchemaTypeChecker.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,8 @@ private void checkForMissingTypes(List<GraphQLError> errors, TypeDefinitionRegis
130130
List<InputObjectTypeDefinition> inputTypes = filterTo(typesMap, InputObjectTypeDefinition.class);
131131
inputTypes.forEach(inputType -> {
132132
List<InputValueDefinition> inputValueDefinitions = inputType.getInputValueDefinitions();
133-
List<Type> inputValueTypes = inputValueDefinitions.stream()
134-
.map(InputValueDefinition::getType)
135-
.collect(toList());
136-
133+
List<Type> inputValueTypes = ImmutableKit.map(inputValueDefinitions, InputValueDefinition::getType);
137134
inputValueTypes.forEach(checkTypeExists("input value", typeRegistry, errors, inputType));
138-
139135
});
140136
}
141137

@@ -149,10 +145,7 @@ private void checkDirectiveDefinitions(TypeDefinitionRegistry typeRegistry, List
149145
checkNamedUniqueness(errors, arguments, InputValueDefinition::getName,
150146
(name, arg) -> new NonUniqueNameError(directiveDefinition, arg));
151147

152-
List<Type> inputValueTypes = arguments.stream()
153-
.map(InputValueDefinition::getType)
154-
.collect(toList());
155-
148+
List<Type> inputValueTypes = ImmutableKit.map(arguments, InputValueDefinition::getType);
156149
inputValueTypes.forEach(
157150
checkTypeExists(typeRegistry, errors, "directive definition", directiveDefinition, directiveDefinition.getName())
158151
);
@@ -316,7 +309,7 @@ private void checkTypeResolversArePresent(List<GraphQLError> errors, TypeDefinit
316309
}
317310

318311
private void checkFieldTypesPresent(TypeDefinitionRegistry typeRegistry, List<GraphQLError> errors, TypeDefinition typeDefinition, List<FieldDefinition> fields) {
319-
List<Type> fieldTypes = fields.stream().map(FieldDefinition::getType).collect(toList());
312+
List<Type> fieldTypes = ImmutableKit.map(fields, FieldDefinition::getType);
320313
fieldTypes.forEach(checkTypeExists("field", typeRegistry, errors, typeDefinition));
321314

322315
List<Type> fieldInputValues = fields.stream()
@@ -363,9 +356,7 @@ private Consumer<? super Type> checkInterfaceTypeExists(TypeDefinitionRegistry t
363356
}
364357

365358
private <T extends TypeDefinition> List<T> filterTo(Map<String, TypeDefinition> types, Class<? extends T> clazz) {
366-
return types.values().stream()
367-
.filter(t -> clazz.equals(t.getClass()))
368-
.map(clazz::cast)
369-
.collect(toList());
359+
return ImmutableKit.filterAndMap(types.values(), t -> clazz.equals(t.getClass()),
360+
clazz::cast);
370361
}
371362
}

src/main/java/graphql/schema/idl/SchemaTypeDirectivesChecker.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import graphql.GraphQLError;
44
import graphql.Internal;
5+
import graphql.collect.ImmutableKit;
56
import graphql.introspection.Introspection.DirectiveLocation;
67
import graphql.language.Argument;
78
import graphql.language.Directive;
@@ -34,7 +35,6 @@
3435
import java.util.List;
3536
import java.util.Map;
3637
import java.util.Optional;
37-
import java.util.stream.Collectors;
3838

3939
import static graphql.introspection.Introspection.DirectiveLocation.ARGUMENT_DEFINITION;
4040
import static graphql.introspection.Introspection.DirectiveLocation.ENUM;
@@ -149,11 +149,8 @@ private void checkDirectives(DirectiveLocation expectedLocation, List<GraphQLErr
149149
}
150150

151151
private boolean inRightLocation(DirectiveLocation expectedLocation, DirectiveDefinition directiveDefinition) {
152-
List<String> names = directiveDefinition.getDirectiveLocations()
153-
.stream().map(graphql.language.DirectiveLocation::getName)
154-
.map(String::toUpperCase)
155-
.collect(Collectors.toList());
156-
152+
List<String> names = ImmutableKit.map(directiveDefinition.getDirectiveLocations(),
153+
it -> it.getName().toUpperCase());
157154
return names.contains(expectedLocation.name().toUpperCase());
158155
}
159156

src/test/groovy/graphql/collect/ImmutableKitTest.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,21 @@ class ImmutableKitTest extends Specification {
7474
then:
7575
flatList == ["A", "B", "C", "D", "E",]
7676
}
77+
78+
def "can filter variable args"() {
79+
when:
80+
def list = ImmutableKit.filterVarArgs({ String s -> s.endsWith("x") }, "a", "b", "ax", "bx", "c")
81+
then:
82+
list == ["ax", "bx"]
83+
84+
when:
85+
list = ImmutableKit.filterVarArgs({ String s -> s.startsWith("Z") }, "a", "b", "ax", "bx", "c")
86+
then:
87+
list == []
88+
89+
when:
90+
list = ImmutableKit.filterVarArgs({ String s -> s.startsWith("x") })
91+
then:
92+
list == []
93+
}
7794
}

0 commit comments

Comments
 (0)