Skip to content

Commit 4d2a473

Browse files
committed
Added quality of life for TrackRecommendationGet field setting
1 parent 12fbd4d commit 4d2a473

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/main/java/com/spotify/requests/tracks/TrackRecommendationGet.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.spotify.requests.util.Market;
77

88
import java.lang.reflect.Field;
9+
import java.util.Arrays;
10+
import java.util.List;
911

1012

1113
/**
@@ -119,6 +121,52 @@ public TrackRecommendationGet(String[] seedArtists, String[] seedGenres, String[
119121
}
120122

121123

124+
/**
125+
* @param maxs maps array to field order in class
126+
* max_acousticness, max_danceability, max_energy, max_instrumentalness, max_liveness, max_loudness, max_speechiness, max_tempo, max_valence, max_duration_ms, max_key, max_mode, max_popularity, max_time_signature
127+
* 0 , 1 , 2 , 3, ...
128+
*/
129+
public void setMaximums(double[] maxs) {
130+
if (maxs.length != 14)
131+
throw new IndexOutOfBoundsException(String.format("Failed to set maximums should be size 14 found size %s.", maxs.length));
132+
133+
this.setGenericFields(maxs, "max_");
134+
}
135+
136+
public void setMinimums(double[] mins) {
137+
if (mins.length != 14)
138+
throw new IndexOutOfBoundsException(String.format("Failed to set minimums should be size 14 found size %s.", mins.length));
139+
140+
this.setGenericFields(mins, "min_");
141+
}
142+
143+
public void setTargets(double[] targets) {
144+
if (targets.length != 14)
145+
throw new IndexOutOfBoundsException(String.format("Failed to set targets should be size 14 found size %s", targets.length));
146+
147+
this.setGenericFields(targets, "target_");
148+
}
149+
150+
private void setGenericFields(double[] gen, String s) {
151+
List<Field> filteredFields = Arrays.stream(this.getClass().getDeclaredFields())
152+
.filter(field -> field.getName().startsWith(s)).toList();
153+
try {
154+
for (int i = 0; i < filteredFields.size(); i++) {
155+
Field f = filteredFields.get(i);
156+
f.setAccessible(true);
157+
if (f.getType().equals(int.class))
158+
f.set(this, (int) gen[i]);
159+
else
160+
f.set(this, gen[i]);
161+
162+
f.setAccessible(false);
163+
}
164+
} catch (IllegalAccessException e) {
165+
throw new RuntimeException(e);
166+
}
167+
}
168+
169+
122170
public void setLimit(int limit) {
123171
this.limit = limit;
124172
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.spotify.requests.tracks;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.lang.reflect.Field;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
class TrackRecommendationGetTest {
10+
11+
12+
@Test
13+
public void setMaximums() throws NoSuchFieldException, IllegalAccessException {
14+
15+
String[] arr = new String[6];
16+
TrackRecommendationGet trg = new TrackRecommendationGet(arr, arr, arr);
17+
18+
double[] values = new double[]{10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d, 10d};
19+
trg.setMaximums(values);
20+
21+
Field timeSig = trg.getClass().getDeclaredField("max_time_signature");
22+
timeSig.setAccessible(true);
23+
Object timeSigO = timeSig.get(trg);
24+
assertEquals(10, (int) timeSigO);
25+
26+
}
27+
28+
29+
@Test
30+
public void setMinimums() throws NoSuchFieldException, IllegalAccessException {
31+
32+
String[] arr = new String[6];
33+
TrackRecommendationGet trg = new TrackRecommendationGet(arr, arr, arr);
34+
35+
double[] values = new double[]{-10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d, -10d};
36+
trg.setMaximums(values);
37+
38+
Field timeSig = trg.getClass().getDeclaredField("min_time_signature");
39+
timeSig.setAccessible(true);
40+
Object timeSigO = timeSig.get(trg);
41+
assertEquals(-10, (int) timeSigO);
42+
43+
}
44+
45+
@Test
46+
public void setTargets() throws NoSuchFieldException, IllegalAccessException {
47+
48+
String[] arr = new String[6];
49+
TrackRecommendationGet trg = new TrackRecommendationGet(arr, arr, arr);
50+
51+
double[] values = new double[]{5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d};
52+
trg.setTargets(values);
53+
54+
Field timeSig = trg.getClass().getDeclaredField("target_time_signature");
55+
timeSig.setAccessible(true);
56+
Object timeSigO = timeSig.get(trg);
57+
assertEquals(5, (int) timeSigO);
58+
59+
}
60+
61+
62+
}

0 commit comments

Comments
 (0)