summaryrefslogtreecommitdiffstats
path: root/awt/javax/imageio/spi/ServiceRegistry.java
blob: 79b02a36644dd5803bcb2fa21acfb696e16a4b2e (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Rustem V. Rafikov
 * @version $Revision: 1.3 $
 */

package javax.imageio.spi;

import java.util.*;
import java.util.Map.Entry;

/**
 * The ServiceRegistry class provides ability to register, deregister, look up
 * and obtain service provider instances (SPIs). A service means a set of
 * interfaces and classes, and a service provider is an implementation of a
 * service. Service providers can be associated with one or more categories.
 * Each category is defined by a class or interface. Only a single instance of a
 * each class is allowed to be registered as a category.
 * 
 * @since Android 1.0
 */
public class ServiceRegistry {

    /**
     * The categories.
     */
    CategoriesMap categories = new CategoriesMap(this);

    /**
     * Instantiates a new ServiceRegistry with the specified categories.
     * 
     * @param categoriesIterator
     *            an Iterator of Class objects for defining of categories.
     */
    public ServiceRegistry(Iterator<Class<?>> categoriesIterator) {
        if (null == categoriesIterator) {
            throw new IllegalArgumentException("categories iterator should not be NULL");
        }
        while (categoriesIterator.hasNext()) {
            Class<?> c = categoriesIterator.next();
            categories.addCategory(c);
        }
    }

    /**
     * Looks up and instantiates the available providers of this service using
     * the specified class loader.
     * 
     * @param providerClass
     *            the Class object of the provider to be looked up.
     * @param loader
     *            the class loader to be used.
     * @return the iterator of providers objects for this service.
     */
    public static <T> Iterator<T> lookupProviders(Class<T> providerClass, ClassLoader loader) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Looks up and instantiates the available providers of this service using
     * the context class loader.
     * 
     * @param providerClass
     *            the Class object of the provider to be looked up.
     * @return the iterator of providers objects for this service.
     */
    public static <T> Iterator<T> lookupProviders(Class<T> providerClass) {
        return lookupProviders(providerClass, Thread.currentThread().getContextClassLoader());
    }

    /**
     * Registers the specified service provider object in the specified
     * categories.
     * 
     * @param provider
     *            the specified provider to be registered.
     * @param category
     *            the category.
     * @return true, if no provider of the same class is registered in this
     *         category, false otherwise.
     */
    public <T> boolean registerServiceProvider(T provider, Class<T> category) {
        return categories.addProvider(provider, category);
    }

    /**
     * Registers a list of service providers.
     * 
     * @param providers
     *            the list of service providers.
     */
    public void registerServiceProviders(Iterator<?> providers) {
        for (Iterator<?> iterator = providers; iterator.hasNext();) {
            categories.addProvider(iterator.next(), null);
        }
    }

    /**
     * Registers the specified service provider object in all categories.
     * 
     * @param provider
     *            the service provider.
     */
    public void registerServiceProvider(Object provider) {
        categories.addProvider(provider, null);
    }

    /**
     * Deregisters the specifies service provider from the specified category.
     * 
     * @param provider
     *            the service provider to be deregistered.
     * @param category
     *            the specified category.
     * @return true, if the provider was already registered in the specified
     *         category, false otherwise.
     */
    public <T> boolean deregisterServiceProvider(T provider, Class<T> category) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Deregisters the specified service provider from all categories.
     * 
     * @param provider
     *            the specified service provider.
     */
    public void deregisterServiceProvider(Object provider) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Gets an Iterator of registered service providers in the specified
     * category which satisfy the specified Filter. The useOrdering parameter
     * indicates whether the iterator will return all of the server provider
     * objects in a set order.
     * 
     * @param category
     *            the specified category.
     * @param filter
     *            the specified filter.
     * @param useOrdering
     *            the flag indicating that providers are ordered in the returned
     *            Iterator.
     * @return the iterator of registered service providers.
     */
    @SuppressWarnings("unchecked")
    public <T> Iterator<T> getServiceProviders(Class<T> category, Filter filter, boolean useOrdering) {
        return new FilteredIterator<T>(filter, (Iterator<T>)categories.getProviders(category,
                useOrdering));
    }

    /**
     * Gets an Iterator of all registered service providers in the specified
     * category. The useOrdering parameter indicates whether the iterator will
     * return all of the server provider objects in a set order.
     * 
     * @param category
     *            the specified category.
     * @param useOrdering
     *            the flag indicating that providers are ordered in the returned
     *            Iterator.
     * @return the Iterator of service providers.
     */
    @SuppressWarnings("unchecked")
    public <T> Iterator<T> getServiceProviders(Class<T> category, boolean useOrdering) {
        return (Iterator<T>)categories.getProviders(category, useOrdering);
    }

    /**
     * Gets the registered service provider object that has the specified class
     * type.
     * 
     * @param providerClass
     *            the specified provider class.
     * @return the service provider object.
     */
    public <T> T getServiceProviderByClass(Class<T> providerClass) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Sets an ordering between two service provider objects within the
     * specified category.
     * 
     * @param category
     *            the specified category.
     * @param firstProvider
     *            the first provider.
     * @param secondProvider
     *            the second provider.
     * @return true, if a previously unset order was set.
     */
    public <T> boolean setOrdering(Class<T> category, T firstProvider, T secondProvider) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Unsets an ordering between two service provider objects within the
     * specified category.
     * 
     * @param category
     *            the specified category.
     * @param firstProvider
     *            the first provider.
     * @param secondProvider
     *            the second provider.
     * @return true, if a previously unset order was removed.
     */
    public <T> boolean unsetOrdering(Class<T> category, T firstProvider, T secondProvider) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Deregisters all providers from the specified category.
     * 
     * @param category
     *            the specified category.
     */
    public void deregisterAll(Class<?> category) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Deregister all providers from all categories.
     */
    public void deregisterAll() {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Finalizes this object.
     * 
     * @throws Throwable
     *             if an error occurs during finalization.
     */
    @Override
    public void finalize() throws Throwable {
        // TODO uncomment when deregisterAll is implemented
        // deregisterAll();
    }

    /**
     * Checks whether the specified provider has been already registered.
     * 
     * @param provider
     *            the provider to be checked.
     * @return true, if the specified provider has been already registered,
     *         false otherwise.
     */
    public boolean contains(Object provider) {
        throw new UnsupportedOperationException("Not supported yet");
    }

    /**
     * Gets an iterator of Class objects representing the current categories.
     * 
     * @return the Iterator of Class objects.
     */
    public Iterator<Class<?>> getCategories() {
        return categories.list();
    }

    /**
     * The ServiceRegistry.Filter interface is used by
     * ServiceRegistry.getServiceProviders to filter providers according to the
     * specified criterion.
     * 
     * @since Android 1.0
     */
    public static interface Filter {

        /**
         * Returns true if the specified provider satisfies the criterion of
         * this Filter.
         * 
         * @param provider
         *            the provider.
         * @return true, if the specified provider satisfies the criterion of
         *         this Filter, false otherwise.
         */
        boolean filter(Object provider);
    }

    /**
     * The Class CategoriesMap.
     */
    private static class CategoriesMap {

        /**
         * The categories.
         */
        Map<Class<?>, ProvidersMap> categories = new HashMap<Class<?>, ProvidersMap>();

        /**
         * The registry.
         */
        ServiceRegistry registry;

        /**
         * Instantiates a new categories map.
         * 
         * @param registry
         *            the registry.
         */
        public CategoriesMap(ServiceRegistry registry) {
            this.registry = registry;
        }

        // -- TODO: useOrdering
        /**
         * Gets the providers.
         * 
         * @param category
         *            the category.
         * @param useOrdering
         *            the use ordering.
         * @return the providers.
         */
        Iterator<?> getProviders(Class<?> category, boolean useOrdering) {
            ProvidersMap providers = categories.get(category);
            if (null == providers) {
                throw new IllegalArgumentException("Unknown category: " + category);
            }
            return providers.getProviders(useOrdering);
        }

        /**
         * List.
         * 
         * @return the iterator< class<?>>.
         */
        Iterator<Class<?>> list() {
            return categories.keySet().iterator();
        }

        /**
         * Adds the category.
         * 
         * @param category
         *            the category.
         */
        void addCategory(Class<?> category) {
            categories.put(category, new ProvidersMap());
        }

        /**
         * Adds a provider to the category. If <code>category</code> is
         * <code>null</code> then the provider will be added to all categories
         * which the provider is assignable from.
         * 
         * @param provider
         *            provider to add.
         * @param category
         *            category to add provider to.
         * @return true, if there were such provider in some category.
         */
        boolean addProvider(Object provider, Class<?> category) {
            if (provider == null) {
                throw new IllegalArgumentException("provider should be != NULL");
            }

            boolean rt;
            if (category == null) {
                rt = findAndAdd(provider);
            } else {
                rt = addToNamed(provider, category);
            }

            if (provider instanceof RegisterableService) {
                ((RegisterableService)provider).onRegistration(registry, category);
            }

            return rt;
        }

        /**
         * Adds the to named.
         * 
         * @param provider
         *            the provider.
         * @param category
         *            the category.
         * @return true, if successful.
         */
        private boolean addToNamed(Object provider, Class<?> category) {
            Object obj = categories.get(category);

            if (null == obj) {
                throw new IllegalArgumentException("Unknown category: " + category);
            }

            return ((ProvidersMap)obj).addProvider(provider);
        }

        /**
         * Find and add.
         * 
         * @param provider
         *            the provider.
         * @return true, if successful.
         */
        private boolean findAndAdd(Object provider) {
            boolean rt = false;
            for (Entry<Class<?>, ProvidersMap> e : categories.entrySet()) {
                if (e.getKey().isAssignableFrom(provider.getClass())) {
                    rt |= e.getValue().addProvider(provider);
                }
            }
            return rt;
        }
    }

    /**
     * The Class ProvidersMap.
     */
    private static class ProvidersMap {
        // -- TODO: providers ordering support

        /**
         * The providers.
         */
        Map<Class<?>, Object> providers = new HashMap<Class<?>, Object>();

        /**
         * Adds the provider.
         * 
         * @param provider
         *            the provider.
         * @return true, if successful.
         */
        boolean addProvider(Object provider) {
            return providers.put(provider.getClass(), provider) != null;
        }

        /**
         * Gets the provider classes.
         * 
         * @return the provider classes.
         */
        Iterator<Class<?>> getProviderClasses() {
            return providers.keySet().iterator();
        }

        // -- TODO ordering
        /**
         * Gets the providers.
         * 
         * @param userOrdering
         *            the user ordering.
         * @return the providers.
         */
        Iterator<?> getProviders(boolean userOrdering) {
            return providers.values().iterator();
        }
    }

    /**
     * The Class FilteredIterator.
     */
    private static class FilteredIterator<E> implements Iterator<E> {

        /**
         * The filter.
         */
        private Filter filter;

        /**
         * The backend.
         */
        private Iterator<E> backend;

        /**
         * The next obj.
         */
        private E nextObj;

        /**
         * Instantiates a new filtered iterator.
         * 
         * @param filter
         *            the filter.
         * @param backend
         *            the backend.
         */
        public FilteredIterator(Filter filter, Iterator<E> backend) {
            this.filter = filter;
            this.backend = backend;
            findNext();
        }

        /**
         * Next.
         * 
         * @return the e.
         */
        public E next() {
            if (nextObj == null) {
                throw new NoSuchElementException();
            }
            E tmp = nextObj;
            findNext();
            return tmp;
        }

        /**
         * Checks for next.
         * 
         * @return true, if successful.
         */
        public boolean hasNext() {
            return nextObj != null;
        }

        /**
         * Removes the.
         */
        public void remove() {
            throw new UnsupportedOperationException();
        }

        /**
         * Sets nextObj to a next provider matching the criterion given by the
         * filter.
         */
        private void findNext() {
            nextObj = null;
            while (backend.hasNext()) {
                E o = backend.next();
                if (filter.filter(o)) {
                    nextObj = o;
                    return;
                }
            }
        }
    }
}