Example usage for com.google.common.collect ForwardingMultimap ForwardingMultimap

List of usage examples for com.google.common.collect ForwardingMultimap ForwardingMultimap

Introduction

In this page you can find the example usage for com.google.common.collect ForwardingMultimap ForwardingMultimap.

Prototype

protected ForwardingMultimap() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.summer.dsl.model.types.util.TypeConformanceComputer.java

protected JvmTypeReference getTypeParametersForSupertype(final Multimap<JvmType, JvmTypeReference> all,
        final JvmType rawType, List<JvmTypeReference> initiallyRequested) {
    if (rawType instanceof JvmTypeParameterDeclarator) {
        List<JvmTypeParameter> typeParameters = ((JvmTypeParameterDeclarator) rawType).getTypeParameters();
        // if we do not declare any parameters it is safe to return the first candidate
        if (typeParameters.isEmpty()) {
            return getFirstForRawType(all, rawType);
        }//w ww. ja  va  2 s  .c om
        List<JvmTypeReference> parameterSuperTypes = Lists.newArrayList();
        for (int i = 0; i < typeParameters.size(); i++) {
            List<JvmTypeReference> parameterReferences = Lists.newArrayList();
            for (JvmTypeReference reference : all.get(rawType)) {
                if (reference instanceof JvmParameterizedTypeReference) {
                    JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) reference;
                    if (parameterized.getArguments().isEmpty()) { // raw type candidate - best result
                        JvmParameterizedTypeReference result = factory.createJvmParameterizedTypeReference();
                        result.setType(rawType);
                        return result;
                    }
                    JvmTypeReference parameterReference = parameterized.getArguments().get(i);
                    parameterReferences.add(parameterReference);
                } else {
                    return null;
                }
            }
            JvmTypeReference parameterSuperType = getCommonParameterSuperType(parameterReferences,
                    initiallyRequested);
            if (parameterSuperType == null) {
                return null;
            } else {
                parameterSuperTypes.add(parameterSuperType);
            }
        }
        JvmParameterizedTypeReference result = factory.createJvmParameterizedTypeReference();
        result.setType(rawType);
        for (JvmTypeReference parameterSuperType : parameterSuperTypes) {
            result.getArguments().add(EcoreUtil2.clone(parameterSuperType));
        }
        return result;
    } else if (rawType instanceof JvmArrayType) {
        final JvmComponentType componentType = ((JvmArrayType) rawType).getComponentType();
        final Function<JvmTypeReference, JvmTypeReference> getComponentType = new Function<JvmTypeReference, JvmTypeReference>() {
            public JvmTypeReference apply(JvmTypeReference from) {
                if (from instanceof JvmGenericArrayTypeReference)
                    return ((JvmGenericArrayTypeReference) from).getComponentType();
                return from;
            }
        };
        Multimap<JvmType, JvmTypeReference> decorated = new ForwardingMultimap<JvmType, JvmTypeReference>() {
            @Override
            protected Multimap<JvmType, JvmTypeReference> delegate() {
                return all;
            }

            @Override
            public Collection<JvmTypeReference> get(JvmType key) {
                if (key == componentType) {
                    Collection<JvmTypeReference> result = all.get(rawType);
                    return Collections2.transform(result, getComponentType);
                }
                return super.get(key);
            }
        };
        JvmTypeReference componentTypeReference = getTypeParametersForSupertype(decorated, componentType,
                Lists.transform(initiallyRequested, getComponentType));
        if (componentTypeReference != null) {
            if (componentTypeReference.eContainer() instanceof JvmGenericArrayTypeReference) {
                return (JvmTypeReference) componentTypeReference.eContainer();
            }
            JvmGenericArrayTypeReference result = factory.createJvmGenericArrayTypeReference();
            result.setComponentType(componentTypeReference);
            return result;
        }
    }
    return null;
}

From source file:org.summer.dsl.model.types.util.TypeArgumentContextProvider.java

protected Multimap<JvmTypeParameter, ResolveInfo> createTemporaryMultimap() {
    return new ForwardingMultimap<JvmTypeParameter, ResolveInfo>() {
        final Multimap<JvmTypeParameter, ResolveInfo> delegate = LinkedHashMultimap.create(2, 1);

        @Override//from  w  w  w. j av  a2s  .c  o  m
        protected Multimap<JvmTypeParameter, ResolveInfo> delegate() {
            return delegate;
        }

        @Override
        public boolean put(JvmTypeParameter key, ResolveInfo value) {
            if (value != null) {
                if (isRecursive(key, value.reference)) {
                    return false;
                }
                //    TODO improve
                if (value.reference instanceof JvmParameterizedTypeReference) {
                    value = new ResolveInfo(primitives.asWrapperTypeIfPrimitive(value.reference), value.kind,
                            value.hint);
                }
            }
            return super.put(key, value);
        }
        // TODO provide a better toString, too
    };
}