org.jvnet.hk2.internal.IterableProviderImpl Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.hk2.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.utilities.InjecteeImpl;
import org.glassfish.hk2.api.IterableProvider;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.Unqualified;
import org.glassfish.hk2.utilities.NamedImpl;
import org.glassfish.hk2.utilities.reflection.Pretty;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
/**
* @author jwells
*
* @param The type for this provider
*/
public class IterableProviderImpl implements IterableProvider {
private final ServiceLocatorImpl locator;
private final Type requiredType;
private final Set requiredQualifiers;
private final Unqualified unqualified;
private final Injectee originalInjectee;
private final boolean isIterable;
/* package */ IterableProviderImpl(
ServiceLocatorImpl locator,
Type requiredType,
Set requiredQualifiers,
Unqualified unqualified,
Injectee originalInjectee,
boolean isIterable) {
this.locator = locator;
this.requiredType = requiredType;
this.requiredQualifiers = Collections.unmodifiableSet(requiredQualifiers);
this.unqualified = unqualified;
this.originalInjectee = originalInjectee;
this.isIterable = isIterable;
}
private void justInTime() {
InjecteeImpl injectee = new InjecteeImpl(originalInjectee);
injectee.setRequiredType(requiredType);
injectee.setRequiredQualifiers(requiredQualifiers);
if (unqualified != null) {
injectee.setUnqualified(unqualified);
}
// This does nothing more than run the JIT resolvers
locator.getInjecteeDescriptor(injectee);
}
/* (non-Javadoc)
* @see javax.inject.Provider#get()
*/
@SuppressWarnings("unchecked")
@Override
public T get() {
justInTime();
// Must do this in this way to ensure that the generated item is properly associated with the root
return (T) locator.getUnqualifiedService(requiredType, unqualified,
isIterable, requiredQualifiers.toArray(new Annotation[requiredQualifiers.size()]));
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.IterableProvider#getHandle()
*/
@SuppressWarnings("unchecked")
@Override
public ServiceHandle getHandle() {
justInTime();
return (ServiceHandle) locator.getUnqualifiedServiceHandle(requiredType, unqualified,
isIterable, requiredQualifiers.toArray(new Annotation[requiredQualifiers.size()]));
}
/* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator iterator() {
justInTime();
List> handles;
handles = ReflectionHelper.>>cast(locator.getAllUnqualifiedServiceHandles(requiredType,
unqualified, isIterable, requiredQualifiers.toArray(new Annotation[requiredQualifiers.size()])));
return new MyIterator(handles);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.IterableProvider#getSize()
*/
@Override
public int getSize() {
justInTime();
return locator.getAllUnqualifiedServiceHandles(requiredType, unqualified, isIterable,
requiredQualifiers.toArray(new Annotation[requiredQualifiers.size()])).size();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.IterableProvider#named(java.lang.String)
*/
@Override
public IterableProvider named(String name) {
return qualifiedWith(new NamedImpl(name));
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.IterableProvider#ofType(java.lang.reflect.Type)
*/
@Override
public IterableProvider ofType(Type type) {
return new IterableProviderImpl(locator, type, requiredQualifiers, unqualified, originalInjectee, isIterable);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.IterableProvider#qualifiedWith(java.lang.annotation.Annotation[])
*/
@Override
public IterableProvider qualifiedWith(Annotation... qualifiers) {
HashSet moreAnnotations = new HashSet(requiredQualifiers);
for (Annotation qualifier : qualifiers) {
moreAnnotations.add(qualifier);
}
return new IterableProviderImpl(locator, requiredType, moreAnnotations, unqualified, originalInjectee, isIterable);
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.IterableProvider#handleIterator()
*/
@Override
public Iterable> handleIterator() {
justInTime();
List> handles = ReflectionHelper.>>cast(locator.getAllServiceHandles(requiredType,
requiredQualifiers.toArray(new Annotation[requiredQualifiers.size()])));
return new HandleIterable(handles);
}
private static class MyIterator implements Iterator {
private final LinkedList> handles;
private MyIterator(List> handles) {
this.handles = new LinkedList>(handles);
}
/* (non-Javadoc)
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
return !handles.isEmpty();
}
/* (non-Javadoc)
* @see java.util.Iterator#next()
*/
@Override
public U next() {
if (handles.isEmpty()) throw new NoSuchElementException();
ServiceHandle nextHandle = handles.removeFirst();
return nextHandle.getService();
}
/* (non-Javadoc)
* @see java.util.Iterator#remove()
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private static class HandleIterable implements Iterable> {
private final List> handles;
private HandleIterable(List> handles) {
this.handles = new LinkedList>(handles);
}
/* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator> iterator() {
return new MyHandleIterator(handles);
}
}
private static class MyHandleIterator implements Iterator> {
private final LinkedList> handles;
private MyHandleIterator(List> handles) {
this.handles = new LinkedList>(handles);
}
/* (non-Javadoc)
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
return !handles.isEmpty();
}
/* (non-Javadoc)
* @see java.util.Iterator#next()
*/
@Override
public ServiceHandle next() {
return handles.removeFirst();
}
/* (non-Javadoc)
* @see java.util.Iterator#remove()
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public String toString() {
return "IterableProviderImpl(" + Pretty.type(requiredType) + "," + Pretty.collection(requiredQualifiers) + "," +
System.identityHashCode(this) + ")";
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy