Example usage for java.util Set toArray

List of usage examples for java.util Set toArray

Introduction

In this page you can find the example usage for java.util Set toArray.

Prototype

<T> T[] toArray(T[] a);

Source Link

Document

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

Usage

From source file:ai.grakn.graph.internal.computer.GraknSparkExecutor.java

public static <M> JavaPairRDD<Object, ViewIncomingPayload<M>> executeVertexProgramIteration(
        final JavaPairRDD<Object, VertexWritable> graphRDD,
        final JavaPairRDD<Object, ViewIncomingPayload<M>> viewIncomingRDD, final GraknSparkMemory memory,
        final Configuration apacheConfiguration) {

    // the graphRDD and the viewRDD must have the same partitioner
    if (null != viewIncomingRDD)
        assert graphRDD.partitioner().get().equals(viewIncomingRDD.partitioner().get());
    final JavaPairRDD<Object, ViewOutgoingPayload<M>> viewOutgoingRDD = (((null == viewIncomingRDD)
            ? graphRDD.mapValues(/*from w  w w . j a va2 s.c o m*/
                    vertexWritable -> new Tuple2<>(vertexWritable, Optional.<ViewIncomingPayload<M>>absent()))
            : // first iteration will not have any views or messages
            graphRDD.leftOuterJoin(viewIncomingRDD)) // every other iteration may have views and messages
                    // for each partition of vertices emit a view and their outgoing messages
                    .mapPartitionsToPair(partitionIterator -> {
                        HadoopPools.initialize(apacheConfiguration);
                        final VertexProgram<M> workerVertexProgram = VertexProgram
                                .<VertexProgram<M>>createVertexProgram(HadoopGraph.open(apacheConfiguration),
                                        apacheConfiguration); // each partition(Spark)/worker(TP3) has a local copy of the vertex program (a worker's task)
                        final Set<String> elementComputeKeys = workerVertexProgram.getElementComputeKeys(); // the compute keys as a set
                        final String[] elementComputeKeysArray = elementComputeKeys.size() == 0 ? EMPTY_ARRAY
                                : elementComputeKeys.toArray(new String[elementComputeKeys.size()]); // the compute keys as an array
                        final SparkMessenger<M> messenger = new SparkMessenger<>();
                        workerVertexProgram.workerIterationStart(memory.asImmutable()); // start the worker
                        return () -> IteratorUtils.map(partitionIterator, vertexViewIncoming -> {
                            final StarGraph.StarVertex vertex = vertexViewIncoming._2()._1().get(); // get the vertex from the vertex writable
                            synchronized (vertex) {
                                // drop any computed properties that are cached in memory
                                if (elementComputeKeysArray.length > 0) {
                                    vertex.dropVertexProperties(elementComputeKeysArray);
                                }
                                final boolean hasViewAndMessages = vertexViewIncoming._2()._2().isPresent(); // if this is the first iteration, then there are no views or messages
                                final List<DetachedVertexProperty<Object>> previousView = hasViewAndMessages
                                        ? vertexViewIncoming._2()._2().get().getView()
                                        : Collections.emptyList();
                                final List<M> incomingMessages = hasViewAndMessages
                                        ? vertexViewIncoming._2()._2().get().getIncomingMessages()
                                        : Collections.emptyList();
                                previousView
                                        .forEach(property -> property.attach(Attachable.Method.create(vertex))); // attach the view to the vertex
                                // previousView.clear(); // no longer needed so kill it from memory
                                ///
                                messenger.setVertexAndIncomingMessages(vertex, incomingMessages); // set the messenger with the incoming messages
                                workerVertexProgram.execute(
                                        ComputerGraph.vertexProgram(vertex, workerVertexProgram), messenger,
                                        memory); // execute the vertex program on this vertex for this iteration
                                // incomingMessages.clear(); // no longer needed so kill it from memory
                                ///
                                final List<DetachedVertexProperty<Object>> nextView = elementComputeKeysArray.length == 0
                                        ? // not all vertex programs have compute keys
                                Collections.emptyList()
                                        : IteratorUtils.list(
                                                IteratorUtils.map(vertex.properties(elementComputeKeysArray),
                                                        property -> DetachedFactory.detach(property, true)));
                                final List<Tuple2<Object, M>> outgoingMessages = messenger
                                        .getOutgoingMessages(); // get the outgoing messages

                                // if no more vertices in the partition, end the worker's iteration
                                if (!partitionIterator.hasNext()) {
                                    workerVertexProgram.workerIterationEnd(memory.asImmutable());
                                }
                                return new Tuple2<>(vertex.id(),
                                        new ViewOutgoingPayload<>(nextView, outgoingMessages));
                            }
                        });
                    }, true)); // true means that the partition is preserved
    // the graphRDD and the viewRDD must have the same partitioner
    assert graphRDD.partitioner().get().equals(viewOutgoingRDD.partitioner().get());
    // "message pass" by reducing on the vertex object id of the view and message payloads
    final MessageCombiner<M> messageCombiner = VertexProgram
            .<VertexProgram<M>>createVertexProgram(HadoopGraph.open(apacheConfiguration), apacheConfiguration)
            .getMessageCombiner().orElse(null);
    final JavaPairRDD<Object, ViewIncomingPayload<M>> newViewIncomingRDD = viewOutgoingRDD
            .flatMapToPair(tuple -> () -> IteratorUtils.<Tuple2<Object, Payload>>concat(
                    IteratorUtils.of(new Tuple2<>(tuple._1(), tuple._2().getView())), // emit the view payload
                    IteratorUtils.map(tuple._2().getOutgoingMessages().iterator(),
                            message -> new Tuple2<>(message._1(), new MessagePayload<>(message._2()))))) // emit the outgoing message payloads one by one
            .reduceByKey(graphRDD.partitioner().get(), (a, b) -> { // reduce the view and outgoing messages into a single payload object representing the new view and incoming messages for a vertex
                if (a instanceof ViewIncomingPayload) {
                    ((ViewIncomingPayload<M>) a).mergePayload(b, messageCombiner);
                    return a;
                } else if (b instanceof ViewIncomingPayload) {
                    ((ViewIncomingPayload<M>) b).mergePayload(a, messageCombiner);
                    return b;
                } else {
                    final ViewIncomingPayload<M> c = new ViewIncomingPayload<>(messageCombiner);
                    c.mergePayload(a, messageCombiner);
                    c.mergePayload(b, messageCombiner);
                    return c;
                }
            }).filter(payload -> !(payload._2() instanceof MessagePayload)) // this happens if there is a message to a vertex that does not exist
            .filter(payload -> !((payload._2() instanceof ViewIncomingPayload)
                    && !((ViewIncomingPayload<M>) payload._2()).hasView())) // this happens if there are many messages to a vertex that does not exist
            .mapValues(payload -> payload instanceof ViewIncomingPayload ? (ViewIncomingPayload<M>) payload : // this happens if there is a vertex with incoming messages
                    new ViewIncomingPayload<>((ViewPayload) payload)); // this happens if there is a vertex with no incoming messages
    // the graphRDD and the viewRDD must have the same partitioner
    assert graphRDD.partitioner().get().equals(newViewIncomingRDD.partitioner().get());
    newViewIncomingRDD.foreachPartition(partitionIterator -> {
        HadoopPools.initialize(apacheConfiguration);
    }); // need to complete a task so its BSP and the memory for this iteration is updated
    return newViewIncomingRDD;
}

From source file:org.camunda.bpm.spring.boot.starter.property.CamundaBpmProperties.java

static String[] initDeploymentResourcePattern() {
    final Set<String> suffixes = new HashSet<String>();
    suffixes.addAll(Arrays.asList(DEFAULT_DMN_RESOURCE_SUFFIXES));
    suffixes.addAll(Arrays.asList(DEFAULT_BPMN_RESOURCE_SUFFIXES));
    suffixes.addAll(Arrays.asList(DEFAULT_CMMN_RESOURCE_SUFFIXES));

    final Set<String> patterns = new HashSet<String>();
    for (String suffix : suffixes) {
        patterns.add(String.format("%s**/*.%s", CLASSPATH_ALL_URL_PREFIX, suffix));
    }/*  w ww.  j  a va  2 s .c  o m*/

    return patterns.toArray(new String[patterns.size()]);
}

From source file:py.una.pol.karaku.test.util.TestUtils.java

/**
 * Retorna la lista de entidades relacionadas a una base.
 * /*  ww w  .  ja v  a2 s.c  o  m*/
 * @param base
 *            entidad base
 * @return array con todas las entidades que se pueden acceder desde base.
 */
public static Class<?>[] getReferencedClasses(Class<?> base) {

    LOG.info("Scanning for classes with relations with '{}'", base.getName());
    Set<Class<?>> result = getReferencedClasses(base, new HashSet<Class<?>>(10));
    result.add(base);
    for (Class<?> c : result) {
        LOG.info("Found class {} ", c.getSimpleName());
    }
    LOG.info("Found '{}' classes with relations with '{}'", result.size(), base.getSimpleName());
    return result.toArray(new Class<?>[result.size()]);
}

From source file:com.hurence.logisland.classloading.PluginProxy.java

private static Object createProxy(Object object, Class superClass, Class[] interfaces, ClassLoader cl) {
    CglibProxyHandler handler = new CglibProxyHandler(object);

    Enhancer enhancer = new Enhancer();

    if (superClass != null && !AbstractConfigurableComponent.class.isAssignableFrom(superClass)) {
        enhancer.setSuperclass(superClass);
    }//from ww w.  j  av  a 2 s. c om

    enhancer.setCallback(handler);

    Set<Class<?>> il = new LinkedHashSet<>();
    il.add(SerializationMagik.class);
    il.add(DelegateAware.class);

    if (interfaces != null) {

        for (Class<?> i : interfaces) {
            if (i.isInterface()) {
                il.add(i);
            }
        }

        enhancer.setInterfaces(il.toArray(new Class[il.size()]));
    }

    enhancer.setClassLoader(cl == null ? Thread.currentThread().getContextClassLoader() : cl);

    return enhancer.create();
}

From source file:info.magnolia.importexport.Bootstrapper.java

/**
 * Bootstrap a specific repository.//from w  w  w.ja  v  a2 s .co  m
 */
public static boolean bootstrapRepository(String[] bootdirs, String repositoryName, BootstrapFilter filter) {
    Set<File> xmlfileset = getBootstrapFiles(bootdirs, repositoryName, filter);

    if (xmlfileset.isEmpty()) {
        log.debug("No bootstrap files found for repository [{}], skipping...", repositoryName);
        return true;
    }

    log.info("Trying to import content from {} files into repository [{}]", Integer.toString(xmlfileset.size()),
            repositoryName);

    final File[] files = xmlfileset.toArray(new File[xmlfileset.size()]);
    return bootstrapFiles(repositoryName, files);
}

From source file:com.walmartlabs.mupd8.application.Config.java

private static HashMap<String, JSONObject> extractWorkerJSONs(JSONObject configuration) {
    HashMap<String, JSONObject> performers = new HashMap<String, JSONObject>();

    JSONObject applications = (JSONObject) getScopedValue(configuration,
            new String[] { "mupd8", "application" });
    if (applications == null) {
        // Lost cause: No applications found.
        System.err.println("No mupd8:application found; Config.workerJSONs not set.");
        return performers;
    }//  w w w . j a v a  2  s .  c o m
    Set<?> applicationNames = applications.keySet();
    if (applicationNames.size() != 1) {
        System.err.println("Exactly one application definition expected, but got "
                + Integer.toString(applicationNames.size()) + " instead; Config.workerJSONs not set.");
        return performers;
    }
    String applicationName = applicationNames.toArray(new String[] {})[0];
    JSONObject performerSection = (JSONObject) getScopedValue(applications,
            new String[] { applicationName, "performers" });

    for (Object k : performerSection.keySet()) {
        String key = (String) k;
        performers.put(key, (JSONObject) performerSection.get(key));
    }

    return performers;
}

From source file:ezbake.security.permissions.PermissionUtils.java

/**
 * Validate Accumulo-style visibility expression against a set of authorizations.
 *
 * @param auths Formal authorizations of the user
 * @param visibilityExpression Accumulo-style visibility expression
 * @return true if validation authorizations against visibility expression, false otherwise
 *///from  w  w  w  . j av a 2s . com
public static boolean validateVisibilityExpression(Set<String> auths, String visibilityExpression) {
    if (StringUtils.isBlank(visibilityExpression)) {
        return true; // No visibility to check
    }

    if (auths == null || auths.isEmpty()) {
        return false; // Has visibility but no auths
    }

    final VisibilityEvaluator visEval = new VisibilityEvaluator(
            new org.apache.accumulo.core.security.Authorizations(auths.toArray(new String[] {})));

    try {
        return visEval.evaluate(new ColumnVisibility(visibilityExpression));
    } catch (final VisibilityParseException e) {
        return false;
    }
}

From source file:com.asakusafw.runtime.stage.output.StageOutputDriver.java

private static void addOutput(Job job, String name, Class<?> formatClass, Class<?> keyClass,
        Class<?> valueClass) {
    assert job != null;
    assert name != null;
    assert formatClass != null;
    assert keyClass != null;
    assert valueClass != null;
    if (isValidName(name) == false) {
        throw new IllegalArgumentException(MessageFormat.format("Output name \"{0}\" is not valid", name));
    }// ww w. j  a  va  2s  .  c o  m
    Configuration conf = job.getConfiguration();
    Set<String> names = new TreeSet<>(conf.getStringCollection(K_NAMES));
    if (names.contains(name)) {
        throw new IllegalArgumentException(
                MessageFormat.format("Output name \"{0}\" is already declared", name));
    }
    names.add(name);
    conf.setStrings(K_NAMES, names.toArray(new String[names.size()]));
    conf.setClass(getPropertyName(K_FORMAT_PREFIX, name), formatClass, OutputFormat.class);
    conf.setClass(getPropertyName(K_KEY_PREFIX, name), keyClass, Object.class);
    conf.setClass(getPropertyName(K_VALUE_PREFIX, name), valueClass, Object.class);
}

From source file:controllers.Common.java

@Util
public static void flashParamsExcept(String... paramNames) {
    Set<String> names = Sets.newHashSet(params.all().keySet());
    names.removeAll(Arrays.asList(paramNames));
    String[] array = new String[names.size()];
    params.flash(names.toArray(array));
}

From source file:com.act.reachables.Network.java

private static JSONArray nodeListObj(MongoDB db, Set<Node> treenodes, HashMap<Node, Integer> nodeOrder)
        throws JSONException {
    JSONArray a = new JSONArray();
    Node[] nodesAr = treenodes.toArray(new Node[0]);
    for (int i = 0; i < nodesAr.length; i++) {
        Node n = nodesAr[i];//ww w. j  a  va 2 s.  c o  m
        a.put(i, JSONHelper.nodeObj(db, n)); // put the object at index i in the array
        nodeOrder.put(n, i);
    }
    return a;
}