-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuilderProvider.java
264 lines (251 loc) · 11.3 KB
/
BuilderProvider.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.cardinal.voca4j.impl;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cardinal.voca4j.api.Builder.State;
import com.cardinal.voca4j.api.BuilderFactory;
import com.cardinal.voca4j.api.StateControlledBuilder;
/**
* A utility class that provides {@link StateControlledBuilder} instances while
* providing efficient memory management.
*
* @author Cardinal System <br>
*
* <hr>
* Complete list of available builer types: <br>
*
* <br>
*
* <ul>
* <li>{@linkplain com.cardinal.voca4j.api.activityentry.get.ActivityEntriesGet.ActivityEntriesGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.get.AlbumCommentsGet.AlbumCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.get.AlbumGet.AlbumGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.get.AlbumReviewsGet.AlbumReviewsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.get.AlbumTrackFieldsGet.AlbumTrackFieldsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.get.AlbumTracksGet.AlbumTracksGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.get.AlbumUserCollectionsGet.AlbumUserCollectionsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.query.AlbumNamesQuery.AlbumNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.query.AlbumQuery.AlbumQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.query.NewAlbumsQuery.NewAlbumsQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.album.query.TopAlbumsQuery.TopAlbumsQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.artist.get.ArtistCommentsGet.ArtistCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.artist.get.ArtistGet.ArtistGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.artist.query.ArtistNamesQuery.ArtistNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.artist.query.ArtistQuery.ArtistQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.comment.get.EntryCommentsGet.EntryCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.discussion.get.DiscussionFoldersGet.DiscussionFoldersGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.discussion.get.DiscussionFolderTopicsGet.DiscussionFolderTopicsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.discussion.get.DiscussionTopicGet.DiscussionTopicGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.discussion.get.DiscussionTopicsGet.DiscussionTopicsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.LyricsGet.LyricsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongByPVGet.SongByPVGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongCommentsGet.SongCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongDerivativesGet.SongDerivativesGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongGet.SongGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongRatingsGet.SongRatingsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongRelatedGet.SongRelatedGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongsHightlightedGet.SongsHightlightedGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.get.SongTopRatedGet.SongTopRatedGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.query.SongNamesQuery.SongNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.song.query.SongQuery.SongQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.entry.query.EntryNamesQuery.EntryNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.entry.query.EntryQuery.EntryQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.entrytypes.get.EntryTypeTagGet.EntryTypeTagGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.pv.get.PVsForSongsGet.PVsForSongsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseevent.get.ReleaseEventAlbumsGet.ReleaseEventAlbumsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseevent.get.ReleaseEventGet.ReleaseEventGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseevent.get.ReleaseEventSongsGet.ReleaseEventSongsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseevent.query.ReleaseEventNamesQuery.ReleaseEventNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseevent.query.ReleaseEventQuery.ReleaseEventQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseeventseries.get.ReleaseEventSeriesGet.ReleaseEventSeriesGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.releaseeventseries.query.ReleaseEventSeriesQuery.ReleaseEventSeriesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.songlist.get.SongListCommentsGet.SongListCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.songlist.get.SongListSongsGet.SongListSongsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.songlist.query.SongListFeaturedNamesQuery.SongListFeaturedNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.songlist.query.SongListFeaturedQuery.SongListFeaturedQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.get.TagByNameGet.TagByNameGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.get.TagGet.TagGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.get.TagChildrenGet.TagChildrenGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.get.TagCommentsGet.TagCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.get.TagCategoryNamesGet.TagCategoryNamesGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.get.TopTagsGet.TopTagsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.query.TagsQuery.TagsQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.tag.query.TagNamesQuery.TagNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserAlbumsGet.UserAlbumsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserEventsGet.UserEventsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserFollowedArtistsGet.UserFollowedArtistsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserGet.UserGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserProfileCommentsGet.UserProfileCommentsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserRatedSongsGet.UserRatedSongsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserSongListsGet.UserSongListsGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserSongRatingGet.UserSongRatingGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserFollowedArtistGet.UserFollowedArtistGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.get.UserAlbumCollectionStatusesGet.UserAlbumCollectionStatusesGetBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.query.UserQuery.UserQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.user.query.UserNamesQuery.UserNamesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.venue.query.VenuesQuery.VenuesQueryBuilder}</li>
*
* <li>{@linkplain com.cardinal.voca4j.api.resource.get.ResourcesGet.ResourcesGetBuilder}</li>
* </ul>
*/
public class BuilderProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(BuilderProvider.class);
private Map<Class<?>, List<StateControlledBuilder<?>>> builderMapping = new HashMap<Class<?>, List<StateControlledBuilder<?>>>();
private BuilderFactory factory;
public BuilderProvider() {
this.factory = new BuilderFactory();
}
public BuilderFactory getFactory() {
return factory;
}
/**
* Finds a previously used builder, resets and recycles it for reuse, then
* returns it. If there are no available builders, this will create a new one
* and map it for later reuse.
*
* @param <T> builder type.
* @param builderType builder class.
* @return builder.
*/
@SuppressWarnings("unchecked")
public <T extends StateControlledBuilder<?>> T getBuilder(Class<T> builderType) {
LOGGER.info("Fetching builder of type: " + ((Type) builderType).getTypeName());
boolean append = false;
if (builderMapping.containsKey(builderType)) {
List<StateControlledBuilder<?>> builders = builderMapping.get(builderType);
StateControlledBuilder<?> builder = builders.get(0);
int i = 1;
while (!builder.getState().equals(State.BUILT)) {
if (i == builders.size()) {
builder = null;
append = true;
break;
}
builder = builders.get(i);
LOGGER.info("Checking builder state: " + builder.toString() + " | " + builder.getState().name());
i++;
}
if (builder != null) {
LOGGER.info("Found available builder: " + builder.toString());
return (T) builder.resetRecycle();
}
}
LOGGER.info("No builders available.");
T builder = factory.create(builderType);
if (append)
builderMapping.get(builderType).add(builder);
else {
List<StateControlledBuilder<?>> list = new ArrayList<StateControlledBuilder<?>>();
list.add(builder);
builderMapping.put(builderType, list);
}
return builder;
}
/**
* Finds a previously used builder, resets and recycles it for reuse, then
* returns it. If there are no available builders, this will create a new one.
* This method does NOT allow the builder to be reused later.
*
* @param <T> builder type.
* @param builderType builder class.
* @return builder.
*/
@SuppressWarnings("unchecked")
public <T extends StateControlledBuilder<?>> T getPrivateBuilder(Class<T> builderType) {
LOGGER.info("Fetching private builder of type: " + ((Type) builderType).getTypeName());
if (builderMapping.containsKey(builderType)) {
List<StateControlledBuilder<?>> builders = builderMapping.get(builderType);
StateControlledBuilder<?> builder = builders.get(0);
int i = 1;
while (!builder.getState().equals(State.BUILT)) {
if (i == builders.size()) {
builder = null;
break;
}
builder = builders.get(i);
LOGGER.info("Checking builder state: " + builder.toString() + " | " + builder.getState().name());
i++;
}
if (builder != null) {
LOGGER.info("Found available builder: " + builder.toString());
builders.remove(builder);
if (builders.isEmpty())
builderMapping.remove(builderType);
return (T) builder.resetRecycle();
}
}
LOGGER.info("No builders available");
T builder = factory.create(builderType);
return builder;
}
}