Skip to content

Commit 87bf42d

Browse files
authored
Merge pull request #29 from pettermahlen/fix-arrays
Use java.util.Arrays for array fields
2 parents 6ef2125 + 3679452 commit 87bf42d

7 files changed

Lines changed: 328 additions & 3 deletions

File tree

dataenum-processor/src/main/java/com/spotify/dataenum/processor/DataEnumProcessor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.spotify.dataenum.processor.generator.spec.SpecTypeFactory;
3131
import com.spotify.dataenum.processor.parser.ParserException;
3232
import com.spotify.dataenum.processor.parser.SpecParser;
33+
import com.squareup.javapoet.ArrayTypeName;
3334
import com.squareup.javapoet.JavaFile;
3435
import com.squareup.javapoet.TypeSpec;
3536
import java.io.IOException;
@@ -113,7 +114,9 @@ private static boolean needsCheckNotNull(Spec enumDef) {
113114
private static boolean needsNullSafeEquals(Spec enumDef) {
114115
for (Value value : enumDef.values()) {
115116
for (Parameter parameter : value.parameters()) {
116-
if (!parameter.type().isPrimitive() && parameter.canBeNull()) {
117+
if (!parameter.type().isPrimitive()
118+
&& !(parameter.type() instanceof ArrayTypeName)
119+
&& parameter.canBeNull()) {
117120
return true;
118121
}
119122
}

dataenum-processor/src/main/java/com/spotify/dataenum/processor/generator/value/ValueTypeFactory.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.spotify.dataenum.processor.parser.ParserException;
3030
import com.spotify.dataenum.processor.util.Iterables;
3131
import com.squareup.javapoet.AnnotationSpec;
32+
import com.squareup.javapoet.ArrayTypeName;
33+
import com.squareup.javapoet.ClassName;
3234
import com.squareup.javapoet.FieldSpec;
3335
import com.squareup.javapoet.MethodSpec;
3436
import com.squareup.javapoet.MethodSpec.Builder;
@@ -184,10 +186,13 @@ private static MethodSpec createEquals(OutputValue value) throws ParserException
184186
result.addCode("$[return ");
185187

186188
List<Parameter> usingReferenceEquality = new ArrayList<>();
189+
List<Parameter> usingArraysEquality = new ArrayList<>();
187190
List<Parameter> usingEquals = new ArrayList<>();
188191

189192
for (Parameter parameter : value.parameters()) {
190-
if (useReferenceEquality(parameter)) {
193+
if (isArrayType(parameter)) {
194+
usingArraysEquality.add(parameter);
195+
} else if (useReferenceEquality(parameter)) {
191196
usingReferenceEquality.add(parameter);
192197
} else {
193198
usingEquals.add(parameter);
@@ -206,6 +211,16 @@ private static MethodSpec createEquals(OutputValue value) throws ParserException
206211
result.addCode("o.$1L == $1L", fieldName);
207212
}
208213

214+
for (Parameter parameter : usingArraysEquality) {
215+
if (first) {
216+
first = false;
217+
} else {
218+
result.addCode("\n&& ");
219+
}
220+
221+
result.addCode("$1T.equals(o.$2L, $2L)", ClassName.get(Arrays.class), parameter.name());
222+
}
223+
209224
for (Parameter parameter : usingEquals) {
210225
if (first) {
211226
first = false;
@@ -271,6 +286,8 @@ private static MethodSpec createHashCode(OutputValue value) {
271286
if (parameter.type().isPrimitive()) {
272287
TypeName boxedType = parameter.type().box();
273288
result.addStatement("$T.valueOf(this.$L).hashCode()", boxedType, fieldName);
289+
} else if (isArrayType(parameter)) {
290+
result.addStatement("$T.hashCode(this.$L)", ClassName.get(Arrays.class), fieldName);
274291
} else {
275292
if (parameter.canBeNull()) {
276293
result.addStatement("(this.$1L != null ? this.$1L.hashCode() : 0)", fieldName);
@@ -282,6 +299,10 @@ private static MethodSpec createHashCode(OutputValue value) {
282299
return result.build();
283300
}
284301

302+
private static boolean isArrayType(Parameter parameter) {
303+
return parameter.type() instanceof ArrayTypeName;
304+
}
305+
285306
private static MethodSpec createToString(OutputValue value) {
286307
MethodSpec.Builder result =
287308
MethodSpec.methodBuilder("toString")
@@ -300,7 +321,15 @@ private static MethodSpec createToString(OutputValue value) {
300321
for (Parameter parameter : value.parameters()) {
301322
String fieldName = parameter.name();
302323

303-
String valueFormat = parameter.redacted() ? "\"***\"" : "this.$1L";
324+
final String valueFormat;
325+
326+
if (parameter.redacted()) {
327+
valueFormat = "\"***\"";
328+
} else if (isArrayType(parameter)) {
329+
valueFormat = "Arrays.toString(this.$1L)";
330+
} else {
331+
valueFormat = "this.$1L";
332+
}
304333

305334
if (first) {
306335
first = false;

dataenum-processor/src/test/java/com/spotify/dataenum/processor/IntegrationTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ public void enumAsInner() throws Exception {
143143
assertThatEnumGeneratedMatchingFile("EnumAsInner");
144144
}
145145

146+
@Test
147+
public void arrayFields() throws Exception {
148+
assertThatEnumGeneratedMatchingFile("ArrayFields");
149+
}
150+
151+
@Test
152+
public void redactedStrings() throws Exception {
153+
assertThatEnumGeneratedMatchingFile("Redacted");
154+
}
155+
146156
@Test
147157
public void referenceOtherDataenum() throws Exception {
148158
Compilation compilation =
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* -\-\-
3+
* DataEnum
4+
* --
5+
* Copyright (c) 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
import static com.spotify.dataenum.DataenumUtils.checkNotNull;
22+
23+
import com.spotify.dataenum.function.Consumer;
24+
import com.spotify.dataenum.function.Function;
25+
import java.lang.Object;
26+
import java.lang.Override;
27+
import java.lang.String;
28+
import java.lang.StringBuilder;
29+
import java.util.Arrays;
30+
import javax.annotation.Generated;
31+
import javax.annotation.Nonnull;
32+
import javax.annotation.Nullable;
33+
34+
@Generated("com.spotify.dataenum.processor.DataEnumProcessor")
35+
public abstract class ArrayFields {
36+
ArrayFields() {
37+
}
38+
39+
public static ArrayFields value(@Nullable byte[] builder, @Nonnull char[] result, @Nonnull Object[] param3) {
40+
return new Value(builder, result, param3);
41+
}
42+
43+
public final boolean isValue() {
44+
return (this instanceof Value);
45+
}
46+
47+
public final Value asValue() {
48+
return (Value) this;
49+
}
50+
51+
public abstract void match(@Nonnull Consumer<Value> value);
52+
53+
public abstract <R_> R_ map(@Nonnull Function<Value, R_> value);
54+
55+
public static final class Value extends ArrayFields {
56+
private final byte[] builder;
57+
58+
private final char[] result;
59+
60+
private final Object[] param3;
61+
62+
Value(byte[] builder, char[] result, Object[] param3) {
63+
this.builder = builder;
64+
this.result = checkNotNull(result);
65+
this.param3 = checkNotNull(param3);
66+
}
67+
68+
@Nullable
69+
public final byte[] builder() {
70+
return builder;
71+
}
72+
73+
@Nonnull
74+
public final char[] result() {
75+
return result;
76+
}
77+
78+
@Nonnull
79+
public final Object[] param3() {
80+
return param3;
81+
}
82+
83+
@Override
84+
public boolean equals(Object other) {
85+
if (other == this) return true;
86+
if (!(other instanceof Value)) return false;
87+
Value o = (Value) other;
88+
return Arrays.equals(o.builder, builder)
89+
&& Arrays.equals(o.result, result)
90+
&& Arrays.equals(o.param3, param3);
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
int result = 0;
96+
result = result * 31 + Arrays.hashCode(this.builder);
97+
result = result * 31 + Arrays.hashCode(this.result);
98+
return result * 31 + Arrays.hashCode(this.param3);
99+
}
100+
101+
@Override
102+
public String toString() {
103+
StringBuilder builder = new StringBuilder();
104+
builder.append("Value{builder=").append(Arrays.toString(this.builder));
105+
builder.append(", result=").append(Arrays.toString(this.result));
106+
builder.append(", param3=").append(Arrays.toString(this.param3));
107+
return builder.append('}').toString();
108+
}
109+
110+
@Override
111+
public final void match(@Nonnull Consumer<Value> value) {
112+
value.accept(this);
113+
}
114+
115+
@Override
116+
public final <R_> R_ map(@Nonnull Function<Value, R_> value) {
117+
return value.apply(this);
118+
}
119+
}
120+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* -\-\-
3+
* DataEnum
4+
* --
5+
* Copyright (c) 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
import com.spotify.dataenum.DataEnum;
21+
import com.spotify.dataenum.dataenum_case;
22+
import javax.annotation.Nullable;
23+
24+
@DataEnum
25+
interface ArrayFields_dataenum {
26+
dataenum_case Value(@Nullable byte[] builder,
27+
char[] result,
28+
Object[] param3);
29+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* -\-\-
3+
* DataEnum
4+
* --
5+
* Copyright (c) 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
import static com.spotify.dataenum.DataenumUtils.checkNotNull;
21+
22+
import com.spotify.dataenum.function.Consumer;
23+
import com.spotify.dataenum.function.Function;
24+
import java.lang.Boolean;
25+
import java.lang.Object;
26+
import java.lang.Override;
27+
import java.lang.String;
28+
import java.lang.StringBuilder;
29+
import java.util.Arrays;
30+
import javax.annotation.Generated;
31+
import javax.annotation.Nonnull;
32+
33+
@Generated("com.spotify.dataenum.processor.DataEnumProcessor")
34+
public abstract class Redacted {
35+
Redacted() {
36+
}
37+
38+
public static Redacted value(@Nonnull int[] param1, boolean param2) {
39+
return new Value(param1, param2);
40+
}
41+
42+
public final boolean isValue() {
43+
return (this instanceof Value);
44+
}
45+
46+
public final Value asValue() {
47+
return (Value) this;
48+
}
49+
50+
public abstract void match(@Nonnull Consumer<Value> value);
51+
52+
public abstract <R_> R_ map(@Nonnull Function<Value, R_> value);
53+
54+
public static final class Value extends Redacted {
55+
private final int[] param1;
56+
57+
private final boolean param2;
58+
59+
Value(int[] param1, boolean param2) {
60+
this.param1 = checkNotNull(param1);
61+
this.param2 = param2;
62+
}
63+
64+
@Nonnull
65+
public final int[] param1() {
66+
return param1;
67+
}
68+
69+
public final boolean param2() {
70+
return param2;
71+
}
72+
73+
@Override
74+
public boolean equals(Object other) {
75+
if (other == this) return true;
76+
if (!(other instanceof Value)) return false;
77+
Value o = (Value) other;
78+
return o.param2 == param2
79+
&& Arrays.equals(o.param1, param1);
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
int result = 0;
85+
result = result * 31 + Arrays.hashCode(this.param1);
86+
return result * 31 + Boolean.valueOf(this.param2).hashCode();
87+
}
88+
89+
@Override
90+
public String toString() {
91+
StringBuilder builder = new StringBuilder();
92+
builder.append("Value{param1=").append("***");
93+
builder.append(", param2=").append("***");
94+
return builder.append('}').toString();
95+
}
96+
97+
@Override
98+
public final void match(@Nonnull Consumer<Value> value) {
99+
value.accept(this);
100+
}
101+
102+
@Override
103+
public final <R_> R_ map(@Nonnull Function<Value, R_> value) {
104+
return value.apply(this);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)