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

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

Introduction

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

Prototype

protected ForwardingMap() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.elasticsearch.common.collect.XMaps.java

/**
 * Wraps the given map and prevent adding of <code>null</code> keys.
 *///from w  w w.  jav  a 2 s .c  o  m
public static <K, V> Map<K, V> ensureNoNullKeys(final Map<K, V> delegate) {
    return new ForwardingMap<K, V>() {
        @Override
        public V put(K key, V value) {
            if (key == null) {
                throw new ElasticSearchIllegalArgumentException("Map key must not be null");
            }
            return super.put(key, value);
        }

        @Override
        protected Map<K, V> delegate() {
            return delegate;
        }
    };
}

From source file:com.mgmtp.jfunk.data.DefaultDataSet.java

private void init(final Map<String, String> map) {
    data = new ForwardingMap<String, String>() {
        @Override/*w w  w.j a v  a2  s .c o m*/
        protected Map<String, String> delegate() {
            return map;
        }

        @Override
        public String get(final Object key) {
            String ov = fixedValues.get(key);
            return ov == null ? delegate().get(key) : ov;
        }

        @Override
        public boolean containsKey(final Object key) {
            return fixedValues.containsKey(key) || delegate().containsKey(key);
        }

        @Override
        public Set<Entry<String, String>> entrySet() {
            Set<String> fixedValueKeys = fixedValues.keySet();
            Set<Entry<String, String>> result = Sets.newHashSet(fixedValues.entrySet());
            for (Entry<String, String> entry : delegate().entrySet()) {
                if (!fixedValueKeys.contains(entry.getKey())) {
                    result.add(entry);
                }
            }
            return result;
        }

        @Override
        public Set<String> keySet() {
            Set<String> keys = Sets.newHashSet(fixedValues.keySet());
            keys.addAll(delegate().keySet());
            return keys;
        }
    };
}

From source file:org.summer.dsl.model.types.descriptions.JvmTypesResourceDescriptionStrategy.java

protected Map<String, String> createLazyUserData(final EObject eObject) {
    return new ForwardingMap<String, String>() {
        private Map<String, String> delegate;

        @Override/*from  w w w.  j  a v a  2s  .  c om*/
        protected Map<String, String> delegate() {
            if (delegate == null) {
                Builder<String, String> userData = ImmutableMap.builder();
                createUserData(eObject, userData);
                delegate = userData.build();
            }
            return delegate;
        }
    };
}

From source file:eu.numberfour.n4js.resource.N4JSResourceDescriptionStrategy.java

private Map<String, String> createUserData(final TModule module) {
    if (module.isPreLinkingPhase()) {
        return getTypeUserdataMapper().createTimestampUserData(module);
    }/*  w ww  . j  ava 2  s. c  o  m*/
    return new ForwardingMap<String, String>() {

        private Map<String, String> delegate;

        @Override
        protected Map<String, String> delegate() {
            if (delegate == null) {
                try {
                    delegate = getTypeUserdataMapper().createUserData(module);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return delegate;
        }
    };

}

From source file:com.mgmtp.jfunk.web.CapabilitiesProvider.java

@Override
public Map<String, DesiredCapabilities> get() {
    Configuration config = configProvider.get();

    Map<String, Map<String, List<JFunkCapability>>> capabilitiesMap = newHashMap();
    for (Entry<String, String> entry : config.entrySet()) {
        String key = entry.getKey();
        Matcher matcher = CAPABILITIES_PREFIX_PATTERN.matcher(key);
        if (matcher.find()) {
            String driverType = matcher.groupCount() == 1 && matcher.group(1) != null ? matcher.group(1)
                    : "global";
            String capabilityString = key.substring(matcher.end() + 1);
            int lastDotIndex = capabilityString.lastIndexOf('.');
            String value = entry.getValue();

            JFunkCapability capability;/*from   ww  w  .j a v  a 2s .  co m*/
            if (lastDotIndex != -1) {
                JFunkCapabilityType type = JFunkCapabilityType.LIST;
                try {
                    Integer.parseInt(capabilityString.substring(lastDotIndex + 1));
                    capabilityString = capabilityString.substring(0, lastDotIndex);
                } catch (NumberFormatException ex) {
                    // not a list capability
                    type = JFunkCapabilityType.STRING;
                }
                capability = new JFunkCapability(capabilityString, value, type);
            } else {
                capability = new JFunkCapability(capabilityString, value, JFunkCapabilityType.STRING);
            }

            Map<String, List<JFunkCapability>> map = capabilitiesMap.get(driverType);
            if (map == null) {
                map = newHashMapWithExpectedSize(5);
                capabilitiesMap.put(driverType, map);
            }
            List<JFunkCapability> list = map.get(capability.name);
            if (list == null) {
                list = newArrayListWithExpectedSize(1);
                map.put(capability.name, list);
            }
            list.add(capability);
        }
    }

    Map<String, List<JFunkCapability>> tmpGlobals = capabilitiesMap.remove("global");
    final Map<String, Object> globalCapabilities = tmpGlobals == null ? ImmutableMap.<String, Object>of()
            : transformCapabilities(tmpGlobals);

    final Proxy proxy = createProxyFromConfig(config);

    // transform in to map of capabilities for each webdriver type
    final Map<String, DesiredCapabilities> byDriverTypeCapabilities = transformEntries(capabilitiesMap,
            new EntryTransformer<String, Map<String, List<JFunkCapability>>, DesiredCapabilities>() {
                @Override
                public DesiredCapabilities transformEntry(final String key,
                        final Map<String, List<JFunkCapability>> value) {
                    Map<String, Object> capabilities = newHashMap(globalCapabilities);
                    Map<String, Object> transformedCapabilities = transformCapabilities(value);
                    capabilities.putAll(transformedCapabilities);

                    DesiredCapabilities result = new DesiredCapabilities(capabilities);
                    if (proxy != null) {
                        result.setCapability(CapabilityType.PROXY, proxy);
                    }
                    return result;
                }
            });

    // wrap, so we get empty capabilities instead of nulls
    return new ForwardingMap<String, DesiredCapabilities>() {
        @Override
        protected Map<String, DesiredCapabilities> delegate() {
            return byDriverTypeCapabilities;
        }

        @Override
        public DesiredCapabilities get(final Object key) {
            DesiredCapabilities capabilities = super.get(key);
            if (capabilities == null) {
                DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
                if (proxy != null) {
                    desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
                }
                capabilities = desiredCapabilities;
            }
            return capabilities;
        }
    };
}

From source file:org.iternine.jeppetto.dao.mongodb.enhance.DBObjectUtil.java

private static Map<Object, Object> wrapMap(final Type valueType, final Map<Object, Object> delegate) {
    return new ForwardingMap<Object, Object>() {

        @Override// w w  w  .j  a v a 2s .  com
        protected Map<Object, Object> delegate() {
            return delegate;
        }

        @Override
        public Object get(Object key) {
            return toDBObject((Class<?>) valueType, super.get(key));
        }

        @Override
        public Set<Entry<Object, Object>> entrySet() {
            return ImmutableSet.copyOf(Iterables.transform(super.entrySet(),
                    new Function<Entry<Object, Object>, Entry<Object, Object>>() {
                        @Override
                        public Entry<Object, Object> apply(Entry<Object, Object> from) {
                            return new AbstractMap.SimpleEntry<Object, Object>(from.getKey(),
                                    toDBObject((Class<?>) valueType, from.getValue()));
                        }
                    }));
        }
    };
}

From source file:org.eclipse.xtext.ui.editor.findrefs.DefaultReferenceFinder.java

@Deprecated
protected Map<EObject, URI> createExportedElementsMap(final Resource resource) {
    return new ForwardingMap<EObject, URI>() {

        private Map<EObject, URI> delegate;

        @Override/*from   ww w  .j a v  a  2 s . c o  m*/
        protected Map<EObject, URI> delegate() {
            if (delegate != null) {
                return delegate;
            }
            URI uri = EcoreUtil2.getPlatformResourceOrNormalizedURI(resource);
            IResourceServiceProvider resourceServiceProvider = getServiceProviderRegistry()
                    .getResourceServiceProvider(uri);
            if (resourceServiceProvider == null) {
                return delegate = Collections.emptyMap();
            }
            IResourceDescription.Manager resourceDescriptionManager = resourceServiceProvider
                    .getResourceDescriptionManager();
            if (resourceDescriptionManager == null) {
                return delegate = Collections.emptyMap();
            }
            IResourceDescription resourceDescription = resourceDescriptionManager
                    .getResourceDescription(resource);
            Map<EObject, URI> exportedElementMap = newIdentityHashMap();
            if (resourceDescription != null) {
                for (IEObjectDescription exportedEObjectDescription : resourceDescription
                        .getExportedObjects()) {
                    EObject eObject = resource
                            .getEObject(exportedEObjectDescription.getEObjectURI().fragment());
                    if (eObject != null)
                        exportedElementMap.put(eObject, exportedEObjectDescription.getEObjectURI());
                }
            }
            return delegate = exportedElementMap;
        }

    };
}

From source file:org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl.java

private void clearCache(IJavaProject project, Set<PackageFragmentRootData> toBeKept) {
    Collection<PackageFragmentRootData> values;
    synchronized (cachedPackageFragmentRootData) {
        values = newArrayList(cachedPackageFragmentRootData.values());
    }//from   w ww.  java 2 s  .  c o m
    List<PackageFragmentRootData> toBeRemoved = newArrayList();
    for (PackageFragmentRootData data : values) {
        if (toBeKept.contains(data)) {
            continue;
        }
        // create a copy of the known associated roots to avoid concurrent modification
        // and conflicts with other readers
        Map<String, IPackageFragmentRoot> copy = newLinkedHashMap(data.associatedRoots);
        Iterator<IPackageFragmentRoot> i = copy.values().iterator();
        IPackageFragmentRoot someRoot = null;
        boolean didChange = false;
        while (i.hasNext()) {
            IPackageFragmentRoot root = i.next();
            if (project.equals(root.getJavaProject())) {
                i.remove();
                didChange = true;
            } else if (someRoot == null) {
                someRoot = root;
            }
        }
        if (copy.size() == 0) {
            toBeRemoved.add(data);
        } else if (didChange) {
            // get rid of cached storages that still point to roots / projects that are no longer available
            // and recompute them lazily on demand
            data.associatedRoots = copy;
            final IPackageFragmentRoot rootToProcess = someRoot;
            data.uri2Storage = new ForwardingMap<URI, IStorage>() {
                Map<URI, IStorage> delegate;

                @Override
                protected Map<URI, IStorage> delegate() {
                    if (delegate == null) {
                        PackageFragmentRootData newlyCollected = initializeData(rootToProcess);
                        return delegate = newlyCollected.uri2Storage;
                    }
                    return delegate;
                }
            };
        }
    }
    if (!toBeRemoved.isEmpty()) {
        synchronized (cachedPackageFragmentRootData) {
            cachedPackageFragmentRootData.values().removeAll(toBeRemoved);
        }
    }
}