Skip to content

Commit eb0b39b

Browse files
committed
Change ParameterSpecification getSupportedTypes to return an EnumSet
closes #2
1 parent 464bdde commit eb0b39b

File tree

6 files changed

+56
-37
lines changed

6 files changed

+56
-37
lines changed

src/main/java/uk/ac/sussex/gdsc/analytics/parameters/Constants.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
package uk.ac.sussex.gdsc.analytics.parameters;
3131

32+
import java.util.EnumSet;
33+
3234
/**
3335
* Contains constants.
3436
*/
@@ -74,9 +76,9 @@ final class Constants {
7476
static final char[] EMPTY_CHARS = new char[0];
7577

7678
/**
77-
* A zero length hit type array.
79+
* A set of all the hit types.
7880
*/
79-
static final HitType[] EMPTY_HIT_TYPE = new HitType[0];
81+
static final EnumSet<HitType> ALL_OF_HIT_TYPE = EnumSet.allOf(HitType.class);
8082

8183
/**
8284
* No construction.

src/main/java/uk/ac/sussex/gdsc/analytics/parameters/CustomParameterSpecification.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package uk.ac.sussex.gdsc.analytics.parameters;
3131

32+
import java.util.EnumSet;
3233
import java.util.Objects;
3334

3435
/**
@@ -60,9 +61,9 @@ public class CustomParameterSpecification implements ParameterSpecification {
6061
/**
6162
* The supported hit types.
6263
*
63-
* <p>If null then all types are supported
64+
* <p>This is not null.
6465
*/
65-
private final HitType[] supportedHitTypes;
66+
private final EnumSet<HitType> supportedHitTypes;
6667

6768
/**
6869
* Instantiates a new custom parameter specification.
@@ -79,7 +80,12 @@ public CustomParameterSpecification(String formalName, String nameFormat, ValueT
7980
this.nameFormat = Objects.requireNonNull(nameFormat, "Name format");
8081
this.valueType = Objects.requireNonNull(valueType, "Value type");
8182
this.maxLength = maxLength;
82-
this.supportedHitTypes = supportedHitTypes.clone();
83+
if (supportedHitTypes.length == 0) {
84+
this.supportedHitTypes = Constants.ALL_OF_HIT_TYPE;
85+
} else {
86+
// Note: This adds the first type again from the array argument but the set is built correctly
87+
this.supportedHitTypes = EnumSet.of(supportedHitTypes[0], supportedHitTypes);
88+
}
8389
this.numberOfIndexes = ParameterUtils.countIndexes(nameFormat);
8490
}
8591

@@ -109,7 +115,7 @@ public int getMaxLength() {
109115
}
110116

111117
@Override
112-
public HitType[] getSupportedHitTypes() {
118+
public EnumSet<HitType> getSupportedHitTypes() {
113119
return supportedHitTypes.clone();
114120
}
115121
}

src/main/java/uk/ac/sussex/gdsc/analytics/parameters/ParameterSpecification.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
package uk.ac.sussex.gdsc.analytics.parameters;
3131

32+
import java.util.EnumSet;
33+
3234
/**
3335
* Defines a parameter specification for the Google Analytics Measurement Protocol.
3436
*
@@ -105,39 +107,35 @@ default int getMaxLength() {
105107
/**
106108
* Gets the supported hit types for the parameter.
107109
*
108-
* <p>If null (or empty) then all types are supported. Otherwise the supported types are returned.
110+
* <p>If the set is {@code null} then it can be assumed that all hit types are supported since
111+
* supporting no hit types is not valid.
109112
*
110-
* <p>The default is an empty array.
113+
* <p>For convenience the default is a set of all hit types.
111114
*
112115
* @return the supported hit types
113116
* @see #isSupported(HitType)
114117
*/
115-
default HitType[] getSupportedHitTypes() {
116-
return Constants.EMPTY_HIT_TYPE;
118+
default EnumSet<HitType> getSupportedHitTypes() {
119+
return Constants.ALL_OF_HIT_TYPE;
117120
}
118121

119122
/**
120123
* Checks the hit type is supported.
121124
*
122-
* <p>If the argument is {@code null} then this will return {@code false}. The exception to this
123-
* is if the {@code null} hit type is present in the array returned from
124-
* {@link #getSupportedHitTypes()}.
125+
* <p>If the argument is {@code null} then this will return {@code false}.
126+
*
127+
* <p>The default method checks the hit type is present in the set returned from
128+
* {@link #getSupportedHitTypes()}. If the set is {@code null} then it is assumed
129+
* that all hit types are supported since supporting no hit types is not valid.
125130
*
126131
* @param hitType the hit type
127132
* @return true, if is supported
128133
*/
129134
default boolean isSupported(HitType hitType) {
130-
final HitType[] supportedHitTypes = getSupportedHitTypes();
131-
if (supportedHitTypes == null || supportedHitTypes.length == 0) {
132-
// Don't support the null hit type!
133-
return hitType != null;
134-
}
135-
// This assumes that supported hit types will never contain a null
136-
for (final HitType supported : supportedHitTypes) {
137-
if (supported == hitType) {
138-
return true;
139-
}
135+
if (hitType == null) {
136+
return false;
140137
}
141-
return false;
138+
final EnumSet<HitType> supportedHitTypes = getSupportedHitTypes();
139+
return supportedHitTypes == null || supportedHitTypes.contains(hitType);
142140
}
143141
}

src/main/java/uk/ac/sussex/gdsc/analytics/parameters/ProtocolSpecification.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
package uk.ac.sussex.gdsc.analytics.parameters;
3131

32+
import java.util.EnumSet;
33+
3234
/**
3335
* Defines parameters for the Google Analytics Measurement Protocol.
3436
*
@@ -1993,7 +1995,7 @@ public enum ProtocolSpecification implements ParameterSpecification {
19931995
*
19941996
* <p>If null then all types are supported
19951997
*/
1996-
private final HitType[] supportedHitTypes;
1998+
private final EnumSet<HitType> supportedHitTypes;
19971999

19982000
/**
19992001
* Creates a new instance.
@@ -2010,8 +2012,12 @@ public enum ProtocolSpecification implements ParameterSpecification {
20102012
this.nameFormat = nameFormat;
20112013
this.valueType = valueType;
20122014
this.maxLength = maxLength;
2013-
this.supportedHitTypes =
2014-
supportedHitTypes.length == 0 ? Constants.EMPTY_HIT_TYPE : supportedHitTypes.clone();
2015+
if (supportedHitTypes.length == 0) {
2016+
this.supportedHitTypes = Constants.ALL_OF_HIT_TYPE;
2017+
} else {
2018+
// Note: This adds the first type again from the array argument but the set is built correctly
2019+
this.supportedHitTypes = EnumSet.of(supportedHitTypes[0], supportedHitTypes);
2020+
}
20152021
this.numberOfIndexes = ParameterUtils.countIndexes(nameFormat);
20162022
}
20172023

@@ -2042,8 +2048,13 @@ public int getMaxLength() {
20422048
return maxLength;
20432049
}
20442050

2051+
/**
2052+
* Gets the supported hit types for the parameter.
2053+
*
2054+
* <p>This method returns a copy of the set. The returned set is never {@code null}.
2055+
*/
20452056
@Override
2046-
public HitType[] getSupportedHitTypes() {
2057+
public EnumSet<HitType> getSupportedHitTypes() {
20472058
// This will not be null
20482059
return supportedHitTypes.clone();
20492060
}

src/test/java/uk/ac/sussex/gdsc/analytics/parameters/CustomParameterSpecificationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package uk.ac.sussex.gdsc.analytics.parameters;
3131

32+
import java.util.EnumSet;
3233
import org.apache.commons.rng.UniformRandomProvider;
3334
import org.apache.commons.rng.simple.RandomSource;
3435
import org.junit.jupiter.api.Assertions;
@@ -73,7 +74,7 @@ void testProperties() {
7374

7475
final CustomParameterSpecification spec = new CustomParameterSpecification("", "",
7576
ValueType.TEXT, 0, HitType.EVENT, HitType.EXCEPTION);
76-
Assertions.assertArrayEquals(new HitType[] {HitType.EVENT, HitType.EXCEPTION},
77+
Assertions.assertEquals(EnumSet.of(HitType.EVENT, HitType.EXCEPTION),
7778
spec.getSupportedHitTypes());
7879
}
7980
}

src/test/java/uk/ac/sussex/gdsc/analytics/parameters/ParameterSpecificationTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package uk.ac.sussex.gdsc.analytics.parameters;
3131

32+
import java.util.EnumSet;
3233
import org.junit.jupiter.api.Assertions;
3334
import org.junit.jupiter.api.Test;
3435

@@ -55,7 +56,7 @@ public ValueType getValueType() {
5556
};
5657
Assertions.assertEquals(0, spec.getMaxLength());
5758
Assertions.assertNotNull(spec.getSupportedHitTypes());
58-
Assertions.assertEquals(0, spec.getSupportedHitTypes().length);
59+
Assertions.assertEquals(EnumSet.allOf(HitType.class), spec.getSupportedHitTypes());
5960
Assertions.assertEquals(1, spec.getNumberOfIndexes());
6061
// Don't support null
6162
Assertions.assertEquals(false, spec.isSupported(null));
@@ -64,7 +65,7 @@ public ValueType getValueType() {
6465
Assertions.assertTrue(spec.isSupported(ht));
6566
}
6667

67-
final HitType[] supported = new HitType[] {HitType.EVENT};
68+
final EnumSet<HitType> supported = EnumSet.of(HitType.EVENT);
6869
spec = new ParameterSpecification() {
6970

7071
@Override
@@ -78,7 +79,7 @@ public CharSequence getNameFormat() {
7879
}
7980

8081
@Override
81-
public HitType[] getSupportedHitTypes() {
82+
public EnumSet<HitType> getSupportedHitTypes() {
8283
return supported;
8384
}
8485

@@ -88,14 +89,14 @@ public ValueType getValueType() {
8889
}
8990
};
9091
Assertions.assertEquals(0, spec.getMaxLength());
91-
Assertions.assertArrayEquals(supported, spec.getSupportedHitTypes());
92+
Assertions.assertEquals(supported, spec.getSupportedHitTypes());
9293
Assertions.assertEquals(2, spec.getNumberOfIndexes());
9394
Assertions.assertEquals(false, spec.isSupported(null));
9495
for (final HitType ht : HitType.values()) {
9596
Assertions.assertEquals(ht == HitType.EVENT, spec.isSupported(ht));
9697
}
9798

98-
final HitType[] supported2 = new HitType[] {HitType.EVENT, HitType.ITEM};
99+
final EnumSet<HitType> supported2 = EnumSet.of(HitType.EVENT, HitType.ITEM);
99100
spec = new ParameterSpecification() {
100101

101102
@Override
@@ -109,7 +110,7 @@ public CharSequence getNameFormat() {
109110
}
110111

111112
@Override
112-
public HitType[] getSupportedHitTypes() {
113+
public EnumSet<HitType> getSupportedHitTypes() {
113114
return supported2;
114115
}
115116

@@ -118,7 +119,7 @@ public ValueType getValueType() {
118119
return null;
119120
}
120121
};
121-
Assertions.assertArrayEquals(supported2, spec.getSupportedHitTypes());
122+
Assertions.assertEquals(supported2, spec.getSupportedHitTypes());
122123
Assertions.assertEquals(false, spec.isSupported(null));
123124
for (final HitType ht : HitType.values()) {
124125
Assertions.assertEquals(ht == HitType.EVENT || ht == HitType.ITEM, spec.isSupported(ht));
@@ -137,7 +138,7 @@ public CharSequence getNameFormat() {
137138
}
138139

139140
@Override
140-
public HitType[] getSupportedHitTypes() {
141+
public EnumSet<HitType> getSupportedHitTypes() {
141142
// Explicitly return null
142143
return null;
143144
}

0 commit comments

Comments
 (0)