Example usage for java.util Collections shuffle

List of usage examples for java.util Collections shuffle

Introduction

In this page you can find the example usage for java.util Collections shuffle.

Prototype

public static void shuffle(List<?> list) 

Source Link

Document

Randomly permutes the specified list using a default source of randomness.

Usage

From source file:com.evolveum.midpoint.repo.sql.util.OrgStructGenerator.java

@Test(enabled = false)
public void generateOrgStructure() throws Exception {
    List<OrgType> orgs = generateOrgStructure(0, new int[] { 1, 20, 25, 2 }, "Org", null);

    System.out.println(orgs.size());

    Collections.shuffle(orgs);

    File file = new File("./target/orgs.xml");
    if (file.exists()) {
        file.delete();//w  w  w  .  jav  a 2 s  .c  o m
    }
    file.createNewFile();
    try (Writer writer = new FileWriterWithEncoding(file, StandardCharsets.UTF_8)) {
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
        writer.write("<objects xmlns=\"http://midpoint.evolveum.com/xml/ns/public/common/common-3\">\n");

        for (OrgType org : orgs) {
            writer.write(PrismTestUtil.serializeObjectToString(org.asPrismObject()));
        }

        writer.write("</objects>");
    }
}

From source file:com.gs.collections.impl.jmh.domain.Positions.java

public Positions shuffle() {
    Collections.shuffle(this.gscPositions);
    Collections.shuffle(this.jdkPositions);
    return this;
}

From source file:org.jspringbot.keyword.expression.ELRunKeywordForRandom.java

@Override
public Object execute(final Object[] params) throws Exception {
    if (!List.class.isInstance(params[3])) {
        throw new IllegalArgumentException("Expecting list for first argument.");
    }//from   w w  w. j a  v  a 2 s  . c  o  m

    int random = Integer.parseInt(String.valueOf(params[1]));
    List items = (List) params[3];

    if (CollectionUtils.isEmpty(items)) {
        return null;
    }

    List<Integer> indices = new ArrayList<Integer>(items.size());
    for (int i = 0; i < items.size(); i++) {
        indices.add(i);
    }

    List<Long> excludeIndices = ELUtils.getExcludeIndices();

    Collections.shuffle(indices);
    for (int i = 0; i < indices.size() && i < random; i++) {
        LOG.keywordAppender().appendProperty("Random index", indices.get(i));

        if (excludeIndices.contains(Long.valueOf(indices.get(i)))) {
            defaultVariableProvider.add("excludedIndex", indices.get(i));
            continue;
        }

        Object item = items.get(indices.get(i));
        defaultVariableProvider.add("randomIndex", indices.get(i));
        defaultVariableProvider.add(String.valueOf(params[2]), item);
        ELRunKeyword.runKeyword(String.valueOf(params[0]));
    }

    return null;
}

From source file:com.simiacryptus.util.test.EnglishWords.java

private static void read() {
    try {/*from   www . ja v  a 2 s.  c  o m*/
        InputStream in = Util.cache(url, file);
        String txt = new String(IOUtils.toByteArray(in), "UTF-8").replaceAll("\r", "");
        List<String> list = Arrays.stream(txt.split("\n")).map(x -> x.replaceAll("[^\\w]", ""))
                .collect(Collectors.toList());
        Collections.shuffle(list);
        for (String paragraph : list) {
            queue.add(new EnglishWords(paragraph));
        }
    } catch (final IOException e) {
        // Ignore... end of stream
    } catch (final RuntimeException e) {
        if (!(e.getCause() instanceof InterruptedException))
            throw e;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.addthis.hydra.kafka.KafkaUtils.java

public static Map<Integer, Node> getSeedKafkaBrokers(CuratorFramework zkClient, int brokerCount) {
    Map<Integer, Node> brokers = new HashMap<>();
    List<Integer> ids = getKafkaBrokerIds(zkClient);
    Collections.shuffle(ids);
    brokerCount = (brokerCount == -1) ? ids.size() : Math.min(brokerCount, ids.size());
    for (int id : ids.subList(0, brokerCount)) {
        try {/*from  w w w.  j  a va 2s  .c  o m*/
            byte[] brokerInfo = zkClient.getData().forPath(BROKERS_PATH + '/' + id);
            brokers.put(id, parseBrokerInfo(id, brokerInfo));
        } catch (Exception e) {
            log.warn("failed to get/parse info for (ignored) broker: {}", id, e);
        }
    }
    return brokers;
}

From source file:org.flite.cach3.test.KeyPrefixCacheTest.java

@Test
public void test() {
    final List<Long> allIds = new ArrayList<Long>();
    final long base = System.currentTimeMillis();
    for (int ix = 0; ix < 30; ix++) {
        allIds.add(base + (ix * 5));// w  w w .j a  v a  2 s.  c  o  m
    }
    final Long singleId = allIds.get(0);
    final List<Long> multiIds = new ArrayList<Long>();
    multiIds.add(allIds.get(1));
    multiIds.add(allIds.get(2));
    multiIds.add(allIds.get(3));
    Collections.shuffle(allIds);

    final Map<Long, Integer> positions = new HashMap<Long, Integer>();
    positions.put(singleId, allIds.indexOf(singleId));
    for (final Long id : allIds) {
        positions.put(id, allIds.indexOf(id));
    }

    final TestSvc test = (TestSvc) context.getBean("testSvc");
    final StubInvalidateSingleCacheListenerImpl listener = (StubInvalidateSingleCacheListenerImpl) context
            .getBean("stubIS");

    // Test the ReadThrough's
    final String sResult1 = test.getDwarf(singleId);
    final List<String> mResult1 = test.getDwarves(allIds);

    final int singleLoc = allIds.indexOf(singleId);
    assertEquals(sResult1, mResult1.get(singleLoc));

    // Testing the invalidations.
    final int previous = listener.getTriggers().size();
    test.invalidateDwarf(singleId);
    // Make sure the listener is getting triggered.
    // Testing that the listener got invoked as required.
    assertTrue("Doesn't look like the listener got called.", listener.getTriggers().size() == previous + 1);
    final String expected = StubInvalidateSingleCacheListenerImpl.formatTriggers(TestDAOImpl.PREFIX_NAMESPACE,
            TestDAOImpl.PREFIX_STRING, singleId.toString(), null, new Object[] { singleId });
    assertEquals(expected, listener.getTriggers().get(listener.getTriggers().size() - 1));

    final List<String> mResult2 = test.getDwarves(allIds);
    final int pos1 = positions.get(singleId);
    assertFalse(sResult1.equals(mResult2.get(pos1)));

    test.invalidateDwarves(multiIds);

    final List<String> mResult3 = test.getDwarves(allIds);
    for (final Long id : multiIds) {
        final int pos = positions.get(id);
        assertFalse(mResult2.get(pos).equals(mResult3.get(pos)));
    }

    // Now, test the updates.
    final String sResult2 = test.updateDwarf(singleId);
    final List<String> mResult4 = test.getDwarves(allIds);
    assertTrue(sResult2.equals(mResult4.get(pos1)));

    final List<String> uResult1 = test.updateDwarves(multiIds);
    final List<String> mResult5 = test.getDwarves(allIds);
    for (int ix = 0; ix < multiIds.size(); ix++) {
        final Long id = multiIds.get(ix);
        final int pos = positions.get(id);
        assertEquals(uResult1.get(ix), mResult5.get(pos));
    }
}

From source file:org.flite.cach3.test.l2.L2UpdateSingleTest.java

@Test
public void test() {
    final TestSvc test = (TestSvc) context.getBean("testSvc");

    final String g1 = RandomStringUtils.randomAlphabetic(4) + "-";

    final List<Long> ids = new ArrayList<Long>();
    final long base = System.currentTimeMillis();
    for (int ix = 0; ix < 10; ix++) {
        ids.add(base + ix);//  w  ww. j  a  va2s  .  c  o  m
    }

    final List<Long> addls = new ArrayList<Long>();
    addls.addAll(ids);
    for (int ix = 0; ix < 10; ix++) {
        addls.add(1000L + ix);
    }
    Collections.shuffle(addls);

    // Set the base expectations
    final List<String> first = test.getL2MultiAlpha(addls, g1);
    for (final String out : first) {
        assertTrue(out.startsWith(g1));
    }

    // Now call the update
    final String g2 = RandomStringUtils.randomAlphabetic(6) + "-";
    for (final Long id : ids) {
        final String out = test.getL2SingleEcho(id, g2);
        assertTrue(out.startsWith(g2));
    }

    // Only the updated ones should be different.
    Collections.shuffle(addls);
    final String g3 = RandomStringUtils.randomAlphabetic(8) + "-";
    final List<String> results = test.getL2MultiAlpha(addls, g3);
    assertTrue(results.size() > 0);
    for (int ix = 0; ix < addls.size(); ix++) {
        final Long key = addls.get(ix);
        final String result = results.get(ix);
        assertTrue(StringUtils.contains(result, key.toString()));
        assertTrue("Key: " + key, result.startsWith(ids.contains(key) ? g2 : g1));
    }
}

From source file:org.flite.cach3.test.l2.L2InvalidateSingleTest.java

@Test
public void test() {
    final TestSvc test = (TestSvc) context.getBean("testSvc");

    final String g1 = RandomStringUtils.randomAlphabetic(4) + "-";
    final List<Long> ids = new ArrayList<Long>();
    final long base = System.currentTimeMillis() - 200000;
    for (int ix = 0; ix < 10; ix++) {
        ids.add(base + ix);/*from w ww.j a  va2  s  . co m*/
    }

    final List<Long> addls = new ArrayList<Long>();
    addls.addAll(ids);
    for (int ix = 0; ix < 10; ix++) {
        addls.add(1000L + ix);
    }
    Collections.shuffle(addls);

    // Set the base expectations
    final List<String> first = test.getL2MultiAlpha(addls, g1);
    for (final String out : first) {
        assertTrue(out.startsWith(g1));
    }

    // Now call the invalidate
    for (final Long id : ids) {
        test.invalidateL2SingleFoxtrot(id);
    }

    // Only the invalidated ones should be different.
    Collections.shuffle(addls);
    final String g2 = RandomStringUtils.randomAlphabetic(8) + "-";
    final List<String> results = test.getL2MultiAlpha(addls, g2);
    assertTrue(results.size() > 0);
    for (int ix = 0; ix < addls.size(); ix++) {
        final Long key = addls.get(ix);
        final String result = results.get(ix);
        assertTrue(StringUtils.contains(result, key.toString()));
        assertTrue("Key: " + key, result.startsWith(ids.contains(key) ? g2 : g1));
    }
}

From source file:com.github.pmerienne.cf.testing.dataset.DatasetUtils.java

public static List<Rating> generateSparseRatings(double sparsity, long userOffset, long itemOffset,
        long userCount, long itemCount, boolean asc) {
    List<Rating> allRatings = new ArrayList<>();

    for (long i = userOffset; i < userCount + userOffset; i++) {
        for (long j = itemOffset; j < itemOffset + itemCount; j++) {
            if (RandomUtils.nextDouble() > sparsity) {
                double value = asc ? (double) j / (double) itemCount + itemOffset
                        : 1 - (double) j / ((double) itemCount + (double) itemOffset);
                allRatings.add(new Rating(i, j, value));
            }//from  ww w .ja v a 2  s .  c  o  m
        }
    }

    Collections.shuffle(allRatings);
    return allRatings;
}

From source file:org.flite.cach3.test.l2.L2UpdateMultiTest.java

@Test
public void test() {

    final TestSvc test = (TestSvc) context.getBean("testSvc");

    final String g1 = RandomStringUtils.randomAlphabetic(4) + "-";

    final List<Long> ids = new ArrayList<Long>();
    final long base = System.currentTimeMillis() - 200000;
    for (int ix = 0; ix < 10; ix++) {
        ids.add(base + ix);/*from w  w  w  . j  av a  2  s .  com*/
    }

    final List<Long> addls = new ArrayList<Long>();
    addls.addAll(ids);
    for (int ix = 0; ix < 10; ix++) {
        addls.add(1000L + ix);
    }
    Collections.shuffle(addls);

    // Set the base expectations
    final List<String> first = test.getL2MultiAlpha(addls, g1);
    for (final String out : first) {
        assertTrue(out.startsWith(g1));
    }

    // Now call the update
    final String g2 = RandomStringUtils.randomAlphabetic(6) + "-";
    final List<String> second = test.getL2MultiBeta(ids, g2);
    for (final String out : second) {
        assertTrue(out.startsWith(g2));
    }

    // Only the updated ones should be different.
    Collections.shuffle(addls);
    final String g3 = RandomStringUtils.randomAlphabetic(8) + "-";
    final List<String> results = test.getL2MultiAlpha(addls, g3);
    assertTrue(results.size() > 0);
    for (int ix = 0; ix < addls.size(); ix++) {
        final Long key = addls.get(ix);
        final String result = results.get(ix);
        assertTrue(StringUtils.contains(result, key.toString()));
        assertTrue("Key: " + key, result.startsWith(ids.contains(key) ? g2 : g1));
    }

}