forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeUtil.java
More file actions
99 lines (90 loc) · 2.91 KB
/
Copy pathTypeUtil.java
File metadata and controls
99 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package graphql.schema.idl;
import graphql.language.Type;
import graphql.language.TypeName;
import graphql.language.ListType;
import graphql.language.NonNullType;
/**
* This class consists of {@code static} utility methods for operating on {@link graphql.language.Type}.
*/
public class TypeUtil {
/**
* This will return the type in graphql SDL format, eg [typeName!]!
*
* @param type the type in play
* @return the type in graphql SDL format, eg [typeName!]!
*/
public static String simplePrint(Type type) {
if (isNonNull(type)) {
return simplePrint(unwrapOne(type)) + "!";
} else if (isList(type)) {
return "[" + simplePrint(unwrapOne(type)) + "]";
}
return ((TypeName) type).getName();
}
/**
* Unwraps all layers of the type or just returns the type again if it's not a wrapped type
*
* @param type the type to be unwrapped
*
* @return the unwrapped type or the same type again if it's not wrapped
*/
public static TypeName unwrapAll(Type type) {
if (isList(type)) {
return unwrapAll(((ListType) type).getType());
} else if (type instanceof NonNullType) {
return unwrapAll(((NonNullType) type).getType());
}
return (TypeName) type;
}
/**
* Unwraps one layer of the type or just returns the type again if it's not a wrapped type
*
* @param type the type to be unwrapped
*
* @return the unwrapped type or the same type again if it's not wrapped
*/
public static Type unwrapOne(Type type) {
if (isNonNull(type)) {
return ((NonNullType) type).getType();
} else if (isList(type)) {
return ((ListType) type).getType();
}
return type;
}
/**
* Returns {@code true} if the provided type is a non null type,
* otherwise returns {@code false}.
*
* @param type the type to check
*
* @return {@code true} if the provided type is a non null type
* otherwise {@code false}
*/
public static boolean isNonNull(Type type) {
return type instanceof NonNullType;
}
/**
* Returns {@code true} if the provided type is a list type,
* otherwise returns {@code false}.
*
* @param type the type to check
*
* @return {@code true} if the provided type is a list typ,
* otherwise {@code false}
*/
public static boolean isList(Type type) {
return type instanceof ListType;
}
/**
* Returns {@code true} if the given type is a non null or list type,
* that is a wrapped type, otherwise returns {@code false}.
*
* @param type the type to check
*
* @return {@code true} if the given type is a non null or list type,
* otherwise {@code false}
*/
public static boolean isWrapped(Type type) {
return isList(type) || isNonNull(type);
}
}