Example usage for com.google.common.collect Sets newCopyOnWriteArraySet

List of usage examples for com.google.common.collect Sets newCopyOnWriteArraySet

Introduction

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

Prototype

@GwtIncompatible("CopyOnWriteArraySet")
public static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet(Iterable<? extends E> elements) 

Source Link

Document

Creates a CopyOnWriteArraySet instance containing the given elements.

Usage

From source file:org.apache.isis.core.metamodel.facets.CollectionUtils.java

/**
 * Copies the iterable into the specified type.
 *///w ww . jav a  2s  .  c o  m
public static Object copyOf(final Iterable<Object> iterable, final Class<?> requiredType) {

    if (iterable == null) {
        throw new IllegalArgumentException("Iterable must be provided");
    }
    if (requiredType == null) {
        throw new IllegalArgumentException("RequiredType must be provided");
    }

    // specific list implementations
    if (CopyOnWriteArrayList.class == requiredType) {
        return Lists.newCopyOnWriteArrayList(iterable);
    }
    if (LinkedList.class == requiredType) {
        return Lists.newLinkedList(iterable);
    }
    if (ArrayList.class == requiredType) {
        return Lists.newArrayList(iterable);
    }

    if (AbstractList.class == requiredType) {
        return Lists.newArrayList(iterable);
    }

    // specific set implementations
    if (CopyOnWriteArraySet.class == requiredType) {
        return Sets.newCopyOnWriteArraySet(iterable);
    }
    if (LinkedHashSet.class == requiredType) {
        return Sets.newLinkedHashSet(iterable);
    }
    if (HashSet.class == requiredType) {
        return Sets.newHashSet(iterable);
    }
    if (TreeSet.class == requiredType) {
        Iterable rawIterable = iterable;
        return Sets.newTreeSet(rawIterable);
    }

    if (AbstractSet.class == requiredType) {
        return Sets.newLinkedHashSet(iterable);
    }

    // interfaces
    if (List.class == requiredType) {
        return Lists.newArrayList(iterable);
    }
    if (SortedSet.class == requiredType) {
        Iterable rawIterable = iterable;
        return Sets.newTreeSet(rawIterable);
    }
    if (Set.class == requiredType) {
        return Sets.newLinkedHashSet(iterable);
    }
    if (Collection.class == requiredType) {
        return Lists.newArrayList(iterable);
    }

    // array
    if (requiredType.isArray()) {
        Class<?> componentType = requiredType.getComponentType();
        Iterable rawIterable = iterable;
        return Iterables.toArray(rawIterable, componentType);
    }

    // not recognized
    return null;
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.state.running.RunningService.java

/**
 * ??./*from w  w  w  . j a  v a  2 s  .  c o m*/
 */
public void start() {
    clear();
    List<String> jobKeys = regCenter.getChildrenKeys(RunningNode.ROOT);
    for (String each : jobKeys) {
        if (!configurationService.load(each).isPresent()) {
            remove(each);
            continue;
        }
        RUNNING_TASKS.put(each,
                Sets.newCopyOnWriteArraySet(
                        Lists.transform(regCenter.getChildrenKeys(RunningNode.getRunningJobNodePath(each)),
                                new Function<String, TaskContext>() {

                                    @Override
                                    public TaskContext apply(final String input) {
                                        return TaskContext
                                                .from(regCenter.get(RunningNode.getRunningTaskNodePath(
                                                        TaskContext.MetaInfo.from(input).toString())));
                                    }
                                })));
    }
}

From source file:org.apache.ambari.server.api.services.stackadvisor.StackAdvisorBlueprintProcessor.java

private Map<String, Set<String>> gatherHostGroupBindings(ClusterTopology clusterTopology) {
    Map<String, Set<String>> hgBindngs = Maps.newHashMap();
    for (Map.Entry<String, HostGroupInfo> hgEnrty : clusterTopology.getHostGroupInfo().entrySet()) {
        hgBindngs.put(hgEnrty.getKey(), Sets.newCopyOnWriteArraySet(hgEnrty.getValue().getHostNames()));
    }//w ww  .j  a va2s .  co  m
    return hgBindngs;
}

From source file:org.apache.ambari.server.api.services.stackadvisor.StackAdvisorBlueprintProcessor.java

private Map<String, Set<String>> gatherHostGroupComponents(ClusterTopology clusterTopology) {
    Map<String, Set<String>> hgComponentsMap = Maps.newHashMap();
    for (Map.Entry<String, HostGroup> hgEnrty : clusterTopology.getBlueprint().getHostGroups().entrySet()) {
        hgComponentsMap.put(hgEnrty.getKey(),
                Sets.newCopyOnWriteArraySet(hgEnrty.getValue().getComponentNames()));
    }//from   www. j  a v  a2 s . c  o  m
    return hgComponentsMap;
}

From source file:com.intellij.plugins.haxe.ide.HaxeControllingCompletionContributor.java

private static Set<CompletionResult> removeDuplicateCompletions(Set<CompletionResult> unfilteredCompletions) {
    // We sort the elements according to name, giving preference to compiler-provided results
    // if the two names are identical.
    CompletionResult sorted[] = unfilteredCompletions.toArray(new CompletionResult[] {});
    Arrays.sort(sorted, new Comparator<CompletionResult>() {
        @Override/* w w w . j a va 2  s  .c o m*/
        public int compare(CompletionResult o1, CompletionResult o2) {
            LookupElement el1 = o1.getLookupElement();
            LookupElement el2 = o2.getLookupElement();
            String fnName1 = null != el1 ? el1.getLookupString() : null;
            String fnName2 = null != el2 ? el2.getLookupString() : null;

            // Can't really throw a null pointer exception... It would be thrown past sort.
            if (null == fnName1)
                return -1;
            if (null == fnName2)
                return 1;

            int comp = fnName1.compareTo(fnName2);
            if (0 == comp) {
                Object obj1 = el1.getObject();
                Object obj2 = el2.getObject();

                comp = obj1 instanceof HaxeCompilerCompletionItem
                        ? (obj2 instanceof HaxeCompilerCompletionItem ? 0 : -1)
                        : (obj2 instanceof HaxeCompilerCompletionItem ? 1 : 0);
            }
            return comp;
        }
    });

    // Now remove duplicates by looping over the list, dropping any that match the entry prior.
    ArrayList<CompletionResult> deduped = new ArrayList<CompletionResult>();
    String lastName = null;
    for (CompletionResult next : sorted) {
        String nextName = getFunctionName(next);
        // In the long run, it's probably not good enough just to check the name.  Multiple argument types may
        // be present, and we may be able to filter based on the local variables available.
        if (null == lastName || !lastName.equals(nextName)) {
            deduped.add(next);
        }
        lastName = nextName;
    }
    return Sets.newCopyOnWriteArraySet(deduped);
}

From source file:com.tinspx.util.net.Request.java

/**
 * duplicate/copy constructor used by {@link #duplicate()}.
 *//*from   ww w  . j a  va2  s.  c  o  m*/
@SuppressWarnings("null")
private Request(Request r) throws IOException {
    //actual will be rebuilt
    asByteSource = r.asByteSource;
    asCharSequence = r.asCharSequence;
    asClass = r.asClass;
    if (r.body != null) {
        body = r.body.duplicate();
    }
    callbacks = NotNull.set(Sets.newCopyOnWriteArraySet(r.callbacks));
    cancel = new CancelList();
    cause = null;
    charsetFunction = r.charsetFunction;
    cookieHandler = r.cookieHandler;
    decodeCharset = r.decodeCharset;
    decoderFunction = r.decoderFunction;
    detectCharset = r.detectCharset;
    if (r.hasCharsetDetectors()) {
        detectors = NotNull
                .set(Sets.newHashSet(Iterables.transform(r.detectors, CharsetUtils.duplicateFunction())));
    }
    doInput = r.doInput;
    //errors not copied
    //errorsView not copied
    headers = new Headers(r.headers);
    id = ID_COUNTER.incrementAndGet();
    method = r.method;
    if (r.hasQueryParams()) {
        params = LinkedListMultimap.create(r.params);
    }
    //paramsView not copied
    properties = Maps.newHashMap(r.properties);
    properties.remove(SUBMISSION_MILLIS);
    //qb not copied
    //query will be rebuilt
    queryEscaper = r.queryEscaper;
    redirectHandler = r.redirectHandler;
    //requestContext not copied
    tag = r.tag;
    uri = r.uri;
}

From source file:org.apache.ambari.server.controller.internal.BlueprintConfigurationProcessor.java

/**
 * Drop every configuration property from advised configuration that is not found in the stack defaults.
 * @param advisedConfigurations advised configuration instance
 *//*w w  w .j av a 2  s  . co m*/
private void doFilterStackDefaults(Map<String, AdvisedConfiguration> advisedConfigurations) {
    Blueprint blueprint = clusterTopology.getBlueprint();
    Configuration stackDefaults = blueprint.getStack().getConfiguration(blueprint.getServices());
    Map<String, Map<String, String>> stackDefaultProps = stackDefaults.getProperties();
    for (Map.Entry<String, AdvisedConfiguration> adConfEntry : advisedConfigurations.entrySet()) {
        AdvisedConfiguration advisedConfiguration = adConfEntry.getValue();
        if (stackDefaultProps.containsKey(adConfEntry.getKey())) {
            Map<String, String> defaultProps = stackDefaultProps.get(adConfEntry.getKey());
            Map<String, String> outFilteredProps = Maps.filterKeys(advisedConfiguration.getProperties(),
                    Predicates.not(Predicates.in(defaultProps.keySet())));
            advisedConfiguration.getProperties().keySet()
                    .removeAll(Sets.newCopyOnWriteArraySet(outFilteredProps.keySet()));

            if (advisedConfiguration.getPropertyValueAttributes() != null) {
                Map<String, ValueAttributesInfo> outFilteredValueAttrs = Maps.filterKeys(
                        advisedConfiguration.getPropertyValueAttributes(),
                        Predicates.not(Predicates.in(defaultProps.keySet())));
                advisedConfiguration.getPropertyValueAttributes().keySet()
                        .removeAll(Sets.newCopyOnWriteArraySet(outFilteredValueAttrs.keySet()));
            }
        } else {
            advisedConfiguration.getProperties().clear();
        }
    }
}