001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.registry;
016    
017    import com.liferay.portal.kernel.util.Accessor;
018    import com.liferay.portal.kernel.util.Function;
019    import com.liferay.registry.ServiceReference;
020    import com.liferay.registry.collections.ServiceMapper;
021    import com.liferay.registry.collections.ServiceReferenceMapper;
022    
023    /**
024     * @author Carlos Sierra Andr??s
025     */
026    public class ServiceReferenceMapperFactory {
027    
028            public static <K, S> ServiceReferenceMapper<K, S> create(
029                    final Accessor<S, K> accessor) {
030    
031                    return new ServiceReferenceMapper<K, S>() {
032    
033                            @Override
034                            public void map(
035                                    ServiceReference<S> serviceReference, Emitter<K> emitter) {
036    
037                                    com.liferay.registry.collections.ServiceReferenceMapperFactory.
038                                            create(
039                                                    new ServiceMapper<K, S>() {
040    
041                                                            @Override
042                                                            public void map(S service, Emitter<K> emitter) {
043                                                                    emitter.emit(accessor.get(service));
044                                                            }
045    
046                                                    });
047                            }
048    
049                    };
050            }
051    
052            public static <K, S> ServiceReferenceMapper<K, S> create(
053                    final Function<S, K> function) {
054    
055                    return new ServiceReferenceMapper<K, S>() {
056    
057                            @Override
058                            public void map(
059                                    ServiceReference<S> serviceReference, Emitter<K> emitter) {
060    
061                                    com.liferay.registry.collections.ServiceReferenceMapperFactory.
062                                            create(
063                                                    new ServiceMapper<K, S>() {
064    
065                                                            @Override
066                                                            public void map(S service, Emitter<K> emitter) {
067                                                                    emitter.emit(function.apply(service));
068                                                            }
069    
070                                                    });
071                            }
072    
073                    };
074            }
075    
076    }