1818import com .serjltt .moshi .adapters .FallbackEnum ;
1919import com .serjltt .moshi .adapters .FallbackOnNull ;
2020import com .squareup .moshi .Moshi ;
21- import com .xing .api .Resource .Factory ;
2221import com .xing .api .internal .Experimental ;
2322import com .xing .api .internal .json .BirthDateJsonAdapter ;
2423import com .xing .api .internal .json .ContactPathJsonAdapter ;
2827import com .xing .api .internal .json .SafeCalendarJsonAdapter ;
2928import com .xing .api .internal .json .SafeEnumJsonAdapter ;
3029import com .xing .api .internal .json .TimeZoneJsonAdapter ;
31- import com .xing .api .resources .BookmarksResource ;
32- import com .xing .api .resources .ContactsResource ;
33- import com .xing .api .resources .GroupsResource ;
34- import com .xing .api .resources .JobsResource ;
35- import com .xing .api .resources .MessagesResource ;
36- import com .xing .api .resources .MiscellaneousResource ;
37- import com .xing .api .resources .ProfileEditingResource ;
38- import com .xing .api .resources .ProfileVisitsResource ;
39- import com .xing .api .resources .RecommendationsResource ;
40- import com .xing .api .resources .UserProfilesResource ;
41-
42- import java .lang .reflect .Constructor ;
43- import java .lang .reflect .Modifier ;
44- import java .util .ArrayList ;
45- import java .util .LinkedHashMap ;
46- import java .util .LinkedHashSet ;
30+
4731import java .util .LinkedList ;
4832import java .util .List ;
49- import java .util .Map ;
50- import java .util .Set ;
5133import java .util .concurrent .Executor ;
5234
5335import okhttp3 .HttpUrl ;
5840import static com .xing .api .Utils .stateNull ;
5941
6042/**
61- * Main access point for the XING API. Creates and holds instances of {@linkplain Response resources} that provide access
62- * points and response/error handling for XING APIs.
63- * <p>
64- * Usage:
65- * <pre>{@code
66- * // Will instantiate ContactsResource.
67- * ContactsResource resource = xingApi.resource(ContactsResource.class);
68- * }</pre>
69- * <p>
70- * Two states of XingApi are supported:
43+ * Main access point for the XING API. Two states of XingApi are supported:
7144 * <dl>
7245 * <li>Logged in - Requires a user's access token and token secret (See {@linkplain XingApi.Builder}.)</li>
7346 * <li>Logged out - See {@linkplain Builder#loggedOut()}</li>
7649 * @since 2.0.0
7750 */
7851public final class XingApi {
79- /** A list of built in factories for resources shipped with the library. */
80- private static final List <Resource .Factory > BUILT_IN_FACTORIES = new ArrayList <>();
81-
82- static {
83- BUILT_IN_FACTORIES .add (BookmarksResource .FACTORY );
84- BUILT_IN_FACTORIES .add (ContactsResource .FACTORY );
85- BUILT_IN_FACTORIES .add (GroupsResource .FACTORY );
86- BUILT_IN_FACTORIES .add (JobsResource .FACTORY );
87- BUILT_IN_FACTORIES .add (MessagesResource .FACTORY );
88- BUILT_IN_FACTORIES .add (MiscellaneousResource .FACTORY );
89- BUILT_IN_FACTORIES .add (ProfileEditingResource .FACTORY );
90- BUILT_IN_FACTORIES .add (ProfileVisitsResource .FACTORY );
91- BUILT_IN_FACTORIES .add (RecommendationsResource .FACTORY );
92- BUILT_IN_FACTORIES .add (UserProfilesResource .FACTORY );
93- }
94-
95- @ SuppressWarnings ("CollectionWithoutInitialCapacity" )
96- private final Map <Class <? extends Resource >, Resource > resourcesCache = new LinkedHashMap <>();
9752 private final List <AuthErrorCallback > authErrorCallbacks = new LinkedList <>();
9853
99- private final Set <Resource .Factory > resourceFactories ;
10054 private final OkHttpClient client ;
10155 private final HttpUrl apiEndpoint ;
10256 private final Converter converter ;
10357 private final CallbackAdapter callbackAdapter ;
10458 private final Executor callbackExecutor ;
10559
10660 XingApi (OkHttpClient client , HttpUrl apiEndpoint , Converter converter , CallbackAdapter callbackAdapter ,
107- Executor callbackExecutor , List < Resource . Factory > resourceFactories ) {
61+ Executor callbackExecutor ) {
10862 this .client = client ;
10963 this .apiEndpoint = apiEndpoint ;
11064 this .converter = converter ;
11165 this .callbackAdapter = callbackAdapter ;
11266 this .callbackExecutor = callbackExecutor ;
113-
114- /* Initialise the factories and add custom ones. */
115- this .resourceFactories = new LinkedHashSet <>(resourceFactories );
116- this .resourceFactories .addAll (BUILT_IN_FACTORIES );
117- }
118-
119- /** Return a {@link Resource} instance specified by the provided class. */
120- @ SuppressWarnings ("unchecked" )
121- public <T extends Resource > T resource (Class <T > resource ) {
122- Resource res = resourcesCache .get (checkNotNull (resource , "resource == null" ));
123- if (res == null ) {
124- // First try to create the resource via a factory
125- for (Resource .Factory factory : resourceFactories ) {
126- res = factory .create (resource , this );
127-
128- if (res != null ) { // yay!
129- resourcesCache .put (resource , res );
130- return (T ) res ;
131- }
132- }
133-
134- // Fallback to the reflection path
135- checkResourceClassDeclaration (resource );
136- try {
137- Constructor <? extends Resource > constructor = resource .getDeclaredConstructor (XingApi .class );
138- constructor .setAccessible (true );
139- res = constructor .newInstance (this );
140- resourcesCache .put (resource , res );
141- } catch (Exception ex ) {
142- throw new IllegalArgumentException ("Resource class malformed." , ex );
143- }
144- }
145- return (T ) res ;
14667 }
14768
14869 /** Returns the api endpoint for <strong>this</strong> client instance. */
@@ -193,14 +114,6 @@ void notifyAuthError(Response<?, ResponseBody> rawResponse) {
193114 }
194115 }
195116
196- /** Throws an exception if class was declared non-static or non-final. */
197- private static void checkResourceClassDeclaration (Class <? extends Resource > resource ) {
198- int modifiers = resource .getModifiers ();
199- if (resource .isLocalClass () || (resource .isMemberClass () && !Modifier .isStatic (modifiers ))) {
200- throw new IllegalArgumentException ("Resource class must be declared static." );
201- }
202- }
203-
204117 /**
205118 * Build a new {@link XingApi}.
206119 * <p>
@@ -296,7 +209,6 @@ public static final class CustomStep extends BuildStep<CustomStep> {
296209 * @since 2.1.0
297210 */
298211 public static class BuildStep <T extends BuildStep > {
299- private final List <Factory > resourceFactory = new ArrayList <>();
300212 private OkHttpClient .Builder clientBuilder ;
301213 private Moshi .Builder moshiBuilder ;
302214 private Executor callbackExecutor ;
@@ -306,12 +218,6 @@ public static class BuildStep<T extends BuildStep> {
306218 apiEndpoint = HttpUrl .parse ("https://api.xing.com/" );
307219 }
308220
309- /** Add a resource factory for a specific {@linkplain Resource resource}. */
310- public final T addResourceFactory (Resource .Factory factory ) {
311- resourceFactory .add (checkNotNull (factory , "factory == null" ));
312- return self ();
313- }
314-
315221 /**
316222 * Change the api endpoint.
317223 * <p>
@@ -393,7 +299,7 @@ public final XingApi build() {
393299 CallbackAdapter adapter = Platform .get ().callbackAdapter (callbackExecutor );
394300 Converter converter = new Converter (moshiBuilder .build ());
395301
396- return new XingApi (clientBuilder ().build (), apiEndpoint , converter , adapter , callbackExecutor , resourceFactory );
302+ return new XingApi (clientBuilder ().build (), apiEndpoint , converter , adapter , callbackExecutor );
397303 }
398304 }
399305}
0 commit comments