<commit_before>/*
 * Copyright 2017 The Apache Software Foundation.
 *
 * Licensed 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.
 */

package org.apache.river.resource;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 *
 * @author peter
 */
public class OSGiServiceIterator implements BundleActivator {
    
    private static OSGiServiceIterator osi;
    
    static <S> Iterator<S> providers(Class<S> service){
	return new ServiceIterator(service, osi.bundleContext);
    }
    
    private BundleContext bundleContext;
    
    public OSGiServiceIterator(){}

    public void start(BundleContext bc) throws Exception {
	if (osi != null) throw new IllegalArgumentException("start may only be called once");
	Service.setOsgi();
	bundleContext = bc;
	osi = this;
    }

    public void stop(BundleContext bc) throws Exception {
	bundleContext = null;
	osi = null;
    }
    
    private static class ServiceIterator<S> implements Iterator<S> {
	private final S[] instances;
	private int index;
	
	ServiceIterator(Class<S> service, BundleContext bc){
	    ServiceTracker st = new ServiceTracker(bc, service.getCanonicalName(), null);
	    Object[] services = st.getServiceReferences();
	    List<S> matches = new ArrayList<S>(services.length);
	    for(int i=0, l=services.length; i<l; i++){
		if (service.isInstance(services[i])) matches.add((S) services[i]);
	    }
	    instances = (S[]) matches.toArray();
	    index = 0;
	}

	public boolean hasNext() {
	    return index < instances.length;
	}

	public S next() {
	    return instances[index++];
	}
	
    }
}<commit_msg>Module :: JGDMS Jini Platform - FindBugs static analysis - bugs #27<commit_after>/*
 * Copyright 2017 The Apache Software Foundation.
 *
 * Licensed 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.
 */

package org.apache.river.resource;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 *
 * @author peter
 */
public class OSGiServiceIterator implements BundleActivator {
    
    private static OSGiServiceIterator osi;
    
    static <S> Iterator<S> providers(Class<S> service){
	return new ServiceIterator(service, osi.bundleContext);
    }
    
    private BundleContext bundleContext;
    
    public OSGiServiceIterator(){}

    public void start(BundleContext bc) throws Exception {
	if (osi != null) throw new IllegalArgumentException("start may only be called once");
	Service.setOsgi();
	bundleContext = bc;
	osi = this;
    }

    public void stop(BundleContext bc) throws Exception {
	bundleContext = null;
	osi = null;
    }
    
    private static class ServiceIterator<S> implements Iterator<S> {
	private final S[] instances;
	private int index;
	
	ServiceIterator(Class<S> service, BundleContext bc){
	    ServiceTracker st = new ServiceTracker(bc, service.getCanonicalName(), null);
	    Object[] services = st.getServiceReferences();
	    List<S> matches = new ArrayList<S>(services.length);
	    for(int i=0, l=services.length; i<l; i++){
		if (service.isInstance(services[i])) matches.add((S) services[i]);
	    }
	    instances = (S[]) matches.toArray();
	    index = 0;
	}

	public boolean hasNext() {
	    return index < instances.length;
	}

	public S next() {
	    if (!hasNext()) throw new NoSuchElementException("End reached");
	    return instances[index++];
	}
	
    }
}
{'fraction_non_alphanumeric': 0.10217719336063807, 'fraction_numerical': 0.004742401379607674, 'mean_word_length': 3.8484848484848486, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698666', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1286, 'n_words': 438}