18
18
import com .google .common .collect .ImmutableList ;
19
19
import com .google .common .collect .Lists ;
20
20
import lombok .Getter ;
21
+ import lombok .NonNull ;
21
22
23
+ import javax .annotation .Nonnull ;
22
24
import javax .annotation .Nullable ;
23
25
import java .util .Collections ;
24
26
import java .util .List ;
25
27
26
28
/**
27
29
* Row type.
30
+ *
31
+ * @author tgianos
28
32
* @author zhenl
33
+ * @since 1.0.0
29
34
*/
30
35
public class RowType extends AbstractType implements ParametricType {
31
- /** default type. */
32
- public static final RowType ROW = new RowType (Collections .<Type >emptyList (), Collections .<String >emptyList ());
36
+ /**
37
+ * default type.
38
+ */
39
+ static final RowType ROW = new RowType (Collections .<RowField >emptyList ());
33
40
34
41
@ Getter
35
42
private final List <RowField > fields ;
43
+
36
44
/**
37
45
* Constructor.
38
- * @param fieldTypes fieldTypes
39
- * @param fieldNames fieldNames
46
+ *
47
+ * @param fields The fields of this row
40
48
*/
41
- public RowType (final List <Type > fieldTypes , final List <String > fieldNames ) {
42
- super (new TypeSignature (
49
+ public RowType (@ Nonnull @ NonNull final List <RowField > fields ) {
50
+ super (
51
+ new TypeSignature (
43
52
TypeEnum .ROW ,
44
- Lists .transform (fieldTypes , new Function <Type , TypeSignature >() {
45
- public TypeSignature apply (@ Nullable final Type input ) {
46
- return input == null ? null : input .getTypeSignature ();
53
+ Lists .transform (
54
+ Lists .transform (
55
+ fields ,
56
+ new Function <RowField , Type >() {
57
+ public Type apply (@ Nullable final RowField input ) {
58
+ return input == null ? null : input .getType ();
59
+ }
60
+ }
61
+ ),
62
+ new Function <Type , TypeSignature >() {
63
+ public TypeSignature apply (@ Nullable final Type input ) {
64
+ return input == null ? null : input .getTypeSignature ();
65
+ }
66
+ }),
67
+ !fields .isEmpty () && fields .get (0 ).getName () != null
68
+ ? Lists .transform (fields , new Function <RowField , Object >() {
69
+ public Object apply (@ Nullable final RowField input ) {
70
+ return input == null ? null : input .getName ();
71
+ }
47
72
}
48
- }),
49
- fieldNames == null ? Lists . newArrayList () : Lists .< Object > newArrayList ( fieldNames )
73
+ )
74
+ : null
50
75
)
51
76
);
52
77
78
+ this .fields = ImmutableList .copyOf (fields );
79
+ }
80
+
81
+ /**
82
+ * Create a new Row Type.
83
+ *
84
+ * @param types The types to create can not be empty
85
+ * @param names The literals to use. Can be null but if not must be the same length as types.
86
+ * @return a new RowType
87
+ */
88
+ public static RowType createRowType (
89
+ @ Nonnull @ NonNull final List <Type > types ,
90
+ @ Nullable final List <String > names
91
+ ) {
92
+ Preconditions .checkArgument (!types .isEmpty (), "types is empty" );
93
+
53
94
final ImmutableList .Builder <RowField > builder = ImmutableList .builder ();
54
- for (int i = 0 ; i < fieldTypes .size (); i ++) {
55
- builder .add (new RowField (fieldTypes .get (i ), fieldNames .get (i )));
95
+ if (names == null ) {
96
+ for (final Type type : types ) {
97
+ builder .add (new RowField (type , null ));
98
+ }
99
+ } else {
100
+ Preconditions .checkArgument (
101
+ types .size () == names .size (),
102
+ "types and names must be matched in size"
103
+ );
104
+ for (int i = 0 ; i < types .size (); i ++) {
105
+ builder .add (
106
+ new RowField (types .get (i ), names .get (i ))
107
+ );
108
+ }
56
109
}
57
- fields = builder .build ();
110
+ return new RowType ( builder .build () );
58
111
}
59
112
60
113
@ Override
@@ -63,47 +116,45 @@ public TypeEnum getBaseType() {
63
116
}
64
117
65
118
@ Override
66
- public RowType createType (final List <Type > types , final List <Object > literals ) {
67
- Preconditions .checkArgument (!types .isEmpty (), "types is empty" );
68
-
69
- if (literals .isEmpty ()) {
70
- return new RowType (types , Lists .<String >newArrayList ());
119
+ public RowType createType (@ Nonnull @ NonNull final List <Type > types , @ Nullable final List <Object > literals ) {
120
+ if (literals != null ) {
121
+ final ImmutableList .Builder <String > builder = ImmutableList .builder ();
122
+ for (final Object literal : literals ) {
123
+ builder .add (TypeUtils .checkType (literal , String .class , "literal" ));
124
+ }
125
+ return RowType .createRowType (types , builder .build ());
126
+ } else {
127
+ return RowType .createRowType (types , null );
71
128
}
129
+ }
72
130
73
- Preconditions . checkArgument ( types . size () == literals . size (), "types and literals must be matched in size" );
74
-
75
- final ImmutableList .Builder <String > builder = ImmutableList .builder ();
76
- for (Object literal : literals ) {
77
- builder .add (TypeUtils . checkType ( literal , String . class , "literal" ));
131
+ @ Override
132
+ public List < Type > getParameters () {
133
+ final ImmutableList .Builder <Type > result = ImmutableList .builder ();
134
+ for (final RowField field : this . fields ) {
135
+ result .add (field . getType ( ));
78
136
}
79
- return new RowType ( types , builder .build () );
137
+ return result .build ();
80
138
}
81
139
82
140
/**
83
141
* Row field.
84
142
*/
85
143
public static class RowField {
86
- @ Getter private final Type type ;
87
- @ Getter private final String name ;
88
-
89
- /** constructor.
144
+ @ Getter
145
+ private final Type type ;
146
+ @ Getter
147
+ private final String name ;
148
+
149
+ /**
150
+ * constructor.
151
+ *
90
152
* @param type type
91
153
* @param name name
92
154
*/
93
- public RowField (final Type type , final String name ) {
94
- this .type = Preconditions .checkNotNull (type , "type is null" );
95
- this .name = Preconditions .checkNotNull (name , "name is null" );
96
- }
97
-
98
- }
99
-
100
- @ Override
101
- public List <Type > getParameters () {
102
- final ImmutableList .Builder <Type > result = ImmutableList .builder ();
103
- for (RowField field : fields ) {
104
- result .add (field .getType ());
155
+ public RowField (@ Nonnull @ NonNull final Type type , @ Nullable final String name ) {
156
+ this .type = type ;
157
+ this .name = name ;
105
158
}
106
- return result .build ();
107
159
}
108
-
109
160
}
0 commit comments