List of usage examples for java.util Collections shuffle
public static void shuffle(List<?> list)
From source file:edu.cmu.tetrad.graph.LayeredDrawing.java
private List<List<Node>> placeInTiers(Graph graph) { List<List<Node>> connectedComponents = GraphUtils.connectedComponents(graph); List<List<Node>> tiers = new ArrayList<List<Node>>(); for (List<Node> component : connectedComponents) { // Recursively map each node to its tier inside the component, // starting with the first node. These tiers are relative and // can be negative. Node firstNode = component.get(0); Map<Node, Integer> componentTiers = new HashMap<Node, Integer>(); placeNodes(firstNode, componentTiers, graph); // Reverse the map. The domain of this map is now possibly negative // tiers. Map<Integer, Node> reversedMap = new MultiHashMap(); for (Node _node : component) { Integer _tier = componentTiers.get(_node); reversedMap.put(_tier, _node); }/*from ww w . j a v a 2 s. c om*/ List<Integer> indices = new ArrayList<Integer>(reversedMap.keySet()); Collections.sort(indices); // Add these tiers low to high to the list of all tiers. Note that // connected components are appended top to bottom in the list of // tiers. int start = tiers.size(); for (int i : indices) { Collection<Node> collection = (Collection<Node>) reversedMap.get(i); tiers.add(new ArrayList<Node>(collection)); } // Do some heuristic uncrossing of edges in successive tiers. for (int i = start; i < tiers.size() - 1; i++) { List<Node> tier1 = tiers.get(i); List<Node> tier2 = tiers.get(i + 1); List<Node> saveArray = new ArrayList<Node>(); int saveCrossings = Integer.MAX_VALUE; for (int j = 0; j < 4 * tier2.size(); j++) { Collections.shuffle(tier2); int numCrossings = numCrossings(tier1, tier2, graph); if (numCrossings < saveCrossings) { saveArray = new ArrayList<Node>(tier2); saveCrossings = numCrossings; } } tiers.set(i + 1, saveArray); } } return tiers; }
From source file:com.liveramp.cascading_ext.flow.LoggingFlow.java
private String logJobErrors() { boolean exceptions = false; StringBuilder jobErrors = new StringBuilder(); final String divider = StringUtils.repeat("-", 80); logAndAppend(jobErrors, divider);/* w ww . java2s .co m*/ try { List<FlowStepStats> stepStats = getFlowStats().getFlowStepStats(); Set<String> jobFailures = new HashSet<String>(); for (FlowStepStats stat : stepStats) { try { RunningJob job = ((HadoopStepStats) stat).getRunningJob(); TaskCompletionEvent[] events = job.getTaskCompletionEvents(0); ArrayList<TaskCompletionEvent> failures = new ArrayList<TaskCompletionEvent>(); for (TaskCompletionEvent event : events) { if (event.getTaskStatus() == Status.FAILED) { failures.add(event); } } // We limit the number of potential logs being pulled to spare the jobtracker if (failures.size() > 0) { Collections.shuffle(failures); for (int i = 0; i < FAILURES_TO_QUERY; i++) { jobFailures.add(getFailureLog(failures.get(i))); } } } catch (Exception e) { exceptions = true; } } if (exceptions) { logAndAppend(jobErrors, "unable to retrieve failures from all completed steps!"); logAndAppend(jobErrors, "successfully retrieved job failures: " + StringUtils.join(jobFailures, ", ")); } else { logAndAppend(jobErrors, "step attempt failures: " + StringUtils.join(jobFailures, ", ")); } } catch (Exception e) { logAndAppend(jobErrors, "unable to retrieve any failures from steps"); logAndAppend(jobErrors, e.toString()); } logAndAppend(jobErrors, divider); return jobErrors.toString(); }
From source file:com.wso2telco.dep.mediator.impl.smsmessaging.southbound.RetrieveSMSSouthboundHandler.java
@Override public boolean handle(MessageContext context) throws CustomException, AxisFault, Exception { SOAPBody body = context.getEnvelope().getBody(); Gson gson = new GsonBuilder().serializeNulls().create(); Properties prop = new Properties(); String reqType = "retrive_sms"; String requestid = UID.getUniqueID(Type.SMSRETRIVE.getCode(), context, executor.getApplicationid()); int batchSize = 100; URL retrieveURL = new URL("http://example.com/smsmessaging/v1" + executor.getSubResourcePath()); String urlQuery = retrieveURL.getQuery(); if (urlQuery != null) { if (urlQuery.contains("maxBatchSize")) { String queryParts[] = urlQuery.split("="); if (queryParts.length > 1) { if (Integer.parseInt(queryParts[1]) < 100) { batchSize = Integer.parseInt(queryParts[1]); }//from w w w. ja va 2 s .c o m } } } List<OperatorEndpoint> endpoints = occi.getAPIEndpointsByApp(API_TYPE, executor.getSubResourcePath(), executor.getValidoperators()); log.info("Endpoints size: " + endpoints.size()); Collections.shuffle(endpoints); int perOpCoLimit = batchSize / (endpoints.size()); log.info("Per OpCo limit :" + perOpCoLimit); JSONArray results = new JSONArray(); int execCount = 0; int forLoopCount = 0; boolean retryFlag = true; FileReader fileReader = new FileReader(); String file = CarbonUtils.getCarbonConfigDirPath() + File.separator + FileNames.MEDIATOR_CONF_FILE.getFileName(); Map<String, String> mediatorConfMap = fileReader.readPropertyFile(file); Boolean retry = false; retry = Boolean.valueOf(mediatorConfMap.get("retry_on_fail")); Integer retryCount = Integer.valueOf(mediatorConfMap.get("retry_count")); ArrayList<String> responses = new ArrayList<String>(); while ((results.length() < batchSize) && (retryFlag == true)) { execCount++; log.info("SB aEndpoint : " + endpoints.size()); for (int i = 0; i < endpoints.size(); i++) { forLoopCount++; log.info("Default forLoopCount : " + forLoopCount); OperatorEndpoint aEndpoint = endpoints.remove(0); log.info("SB aEndpoint : " + aEndpoint.getEndpointref().getAddress()); endpoints.add(aEndpoint); String url = aEndpoint.getEndpointref().getAddress(); APICall ac = apiUtil.setBatchSize(url, body.toString(), reqType, perOpCoLimit); JSONObject obj = ac.getBody(); String retStr = null; log.info("Retrieving messages of operator: " + aEndpoint.getOperator()); if (context.isDoingGET()) { log.info("Doing makeRequest"); retStr = executor.makeRequest(aEndpoint, ac.getUri(), obj.toString(), true, context, false); } else { continue; } log.info("Retrieved messages of " + aEndpoint.getOperator() + " operator: " + retStr); if (retStr != null) { JSONArray resList = apiUtil.getResults(reqType, retStr); if (resList != null) { for (int t = 0; t < resList.length(); t++) { results.put(resList.get(t)); } responses.add(retStr); } } if (results.length() >= batchSize) { break; } } if (retry == false) { retryFlag = false; log.info("11 Final value of retryFlag :" + retryFlag); } if (execCount >= retryCount) { retryFlag = false; log.info("22 Final value of retryFlag :" + retryFlag); } log.info("Final value of count :" + execCount); log.info("Results length of retrieve messages: " + results.length()); } JSONObject paylodObject = apiUtil.generateResponse(context, reqType, results, responses, requestid); String strjsonBody = paylodObject.toString(); /*add resourceURL to the southbound response*/ SouthboundRetrieveResponse sbRetrieveResponse = gson.fromJson(strjsonBody, SouthboundRetrieveResponse.class); if (sbRetrieveResponse != null && sbRetrieveResponse.getInboundSMSMessageList() != null) { String resourceURL = sbRetrieveResponse.getInboundSMSMessageList().getResourceURL(); InboundSMSMessage[] inboundSMSMessageResponses = sbRetrieveResponse.getInboundSMSMessageList() .getInboundSMSMessage(); for (int i = 0; i < inboundSMSMessageResponses.length; i++) { String messageId = inboundSMSMessageResponses[i].getMessageId(); inboundSMSMessageResponses[i].setResourceURL(resourceURL + "/" + messageId); } sbRetrieveResponse.getInboundSMSMessageList().setInboundSMSMessage(inboundSMSMessageResponses); } executor.removeHeaders(context); executor.setResponse(context, gson.toJson(sbRetrieveResponse)); ((Axis2MessageContext) context).getAxis2MessageContext().setProperty("messageType", "application/json"); return true; }
From source file:com.cloudera.oryx.ml.param.HyperParamRanges.java
/** * @param ranges ranges of hyperparameters to try, one per hyperparameters * @param howMany how many combinations of hyperparameters to return * @param perParam how many different hyperparameter values to try per hyperparameter * @return combinations of concrete hyperparameter values. For example, for 5 parameters each * with 3 values to try, the total number of combinations returned could be up to pow(3,5) * or 243. The number could be less if the ranges do not actually have that many distinct * values. If {@code howMany} is smaller than the total number of combinations, a random * subset of all combinations are returned. The order is shuffled randomly. If no parameters * are specified or {@code perParam} is 0, a single empty combination is returned. *//* w w w . j av a 2 s. co m*/ public static List<List<Number>> chooseHyperParameterCombos(Collection<HyperParamRange> ranges, int howMany, int perParam) { Preconditions.checkArgument(howMany > 0); Preconditions.checkArgument(perParam >= 0); int numParams = ranges.size(); if (numParams == 0 || perParam == 0) { return Collections.singletonList(Collections.<Number>emptyList()); } // Put some reasonable upper limit on the number of combos Preconditions.checkArgument(Math.pow(perParam, numParams) <= MAX_COMBOS); int howManyCombos = 1; List<List<Number>> paramRanges = new ArrayList<>(numParams); for (HyperParamRange range : ranges) { List<Number> values = range.getTrialValues(perParam); paramRanges.add(values); howManyCombos *= values.size(); } List<List<Number>> allCombinations = new ArrayList<>(howManyCombos); for (int combo = 0; combo < howManyCombos; combo++) { List<Number> combination = new ArrayList<>(numParams); for (int param = 0; param < numParams; param++) { int whichValueToTry = combo; for (int i = 0; i < param; i++) { whichValueToTry /= paramRanges.get(i).size(); } whichValueToTry %= paramRanges.get(param).size(); combination.add(paramRanges.get(param).get(whichValueToTry)); } allCombinations.add(combination); } if (howMany >= howManyCombos) { Collections.shuffle(allCombinations); return allCombinations; } RandomDataGenerator rdg = new RandomDataGenerator(RandomManager.getRandom()); int[] indices = rdg.nextPermutation(howManyCombos, howMany); List<List<Number>> result = new ArrayList<>(indices.length); for (int i = 0; i < indices.length; i++) { result.add(allCombinations.get(i)); } Collections.shuffle(result); return result; }
From source file:com.google.cloud.bigtable.hbase.TestPut.java
/** * Test inserting multiple rows at one time. * * @throws IOException/*from w w w .j a v a 2 s.c o m*/ */ public void testPutGetDeleteMultipleRows() throws IOException { // Initialize interface Table table = getConnection().getTable(TABLE_NAME); byte[][] rowKeys = dataHelper.randomData("testrow-", NUM_ROWS); byte[][] qualifiers = dataHelper.randomData("testQualifier-", NUM_ROWS); byte[][] values = dataHelper.randomData("testValue-", NUM_ROWS); // Do puts List<Put> puts = new ArrayList<Put>(NUM_ROWS); List<String> keys = new ArrayList<String>(NUM_ROWS); Map<String, QualifierValue> insertedKeyValues = new TreeMap<String, QualifierValue>(); for (int i = 0; i < NUM_ROWS; ++i) { Put put = new Put(rowKeys[i]); put.addColumn(COLUMN_FAMILY, qualifiers[i], values[i]); puts.add(put); String key = Bytes.toString(rowKeys[i]); keys.add(key); insertedKeyValues.put(key, new QualifierValue(qualifiers[i], values[i])); } table.put(puts); // Get List<Get> gets = new ArrayList<Get>(NUM_ROWS); Collections.shuffle(keys); // Retrieve in random order for (String key : keys) { Get get = new Get(Bytes.toBytes(key)); get.addColumn(COLUMN_FAMILY, insertedKeyValues.get(key).qualifier); gets.add(get); } Result[] result = table.get(gets); Assert.assertEquals(NUM_ROWS, result.length); for (int i = 0; i < NUM_ROWS; ++i) { String rowKey = keys.get(i); Assert.assertEquals(rowKey, Bytes.toString(result[i].getRow())); QualifierValue entry = insertedKeyValues.get(rowKey); String descriptor = "Row " + i + " (" + rowKey + ": "; Assert.assertEquals(descriptor, 1, result[i].size()); Assert.assertTrue(descriptor, result[i].containsNonEmptyColumn(COLUMN_FAMILY, entry.qualifier)); Assert.assertEquals(descriptor, entry.value, CellUtil.cloneValue(result[i].getColumnCells(COLUMN_FAMILY, entry.qualifier).get(0))); } // Delete List<Delete> deletes = new ArrayList<Delete>(NUM_ROWS); for (byte[] rowKey : rowKeys) { Delete delete = new Delete(rowKey); deletes.add(delete); } table.delete(deletes); // Confirm they are gone boolean[] checks = table.existsAll(gets); for (Boolean check : checks) { Assert.assertFalse(check); } table.close(); }
From source file:com.algolia.search.saas.APIClient.java
/** * Algolia Search initialization/* ww w. j a v a2 s. c o m*/ * @param applicationID the application ID you have in your admin interface * @param apiKey a valid API key for the service */ public APIClient(String applicationID, String apiKey) { this(applicationID, apiKey, Arrays.asList(applicationID + "-1.algolia.net", applicationID + "-2.algolia.net", applicationID + "-3.algolia.net")); Collections.shuffle(this.buildHostsArray); this.buildHostsArray.add(0, applicationID + ".algolia.net"); this.buildHostsEnabled.add(0L); Collections.shuffle(this.queryHostsArray); this.queryHostsArray.add(0, applicationID + "-dsn.algolia.net"); this.queryHostsEnabled.add(0L); }
From source file:org.jtalks.poulpe.model.dao.hibernate.ComponentHibernateDaoTest.java
@Test public void testSectionPositions() { List<PoulpeSection> expected = forum.getSections(); Collections.shuffle(expected); dao.saveOrUpdate(forum);/*from www .ja va 2s . c o m*/ forum = ObjectRetriever.retrieveUpdated(forum, session); List<PoulpeSection> actual = forum.getSections(); assertEquals(actual, expected); }
From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationDownloader.java
private List<ConfigurationLocation> getLocations(ConfigurationSource source) { List<ConfigurationLocation> result = new ArrayList<>(); List<ConfigurationLocation> randomized = new ArrayList<>(); preferLastSuccessLocation(source, result); randomized.addAll(source.getLocations()); Collections.shuffle(randomized); result.addAll(randomized);/*from w w w . ja va2 s. co m*/ result.removeIf(Objects::isNull); return result; }
From source file:com.linkedin.pinot.tools.query.comparison.StarTreeQueryGenerator.java
/** * Randomly generate the GROUP BY section, may return empty string. * @return/*from w w w. ja v a 2 s . c o m*/ */ private String generateGroupBys() { List<String> groupBys = new ArrayList<>(); int numDimensions = _dimensionColumns.size(); int numGroupBys = _random.nextInt(numDimensions + 1); numGroupBys = Math.min(numGroupBys, MAX_NUM_GROUP_BYS); if (numGroupBys == 0) { return EMPTY_STRING; } Collections.shuffle(_dimensionColumns); for (int i = 0; i < numGroupBys; i++) { groupBys.add(_dimensionColumns.get(i)); } return GROUP_BY + " " + StringUtils.join(groupBys, ", "); }
From source file:com.arcusapp.soundbox.fragment.SongsListFragment.java
private void addRandomButton() { // create the button Button myButton = new Button(getActivity()); myButton.setId(19);/*from ww w . j a v a 2s.c o m*/ myButton.setText(this.getString(R.string.LabelPlaySongsRandom)); myButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.icon_random_shuffled), null); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //start the playActivity Intent playActivityIntent = new Intent(); playActivityIntent.setAction(SoundBoxApplication.ACTION_MAIN_ACTIVITY); playActivityIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); getActivity().startActivity(playActivityIntent); //call the service to play new songs Intent serviceIntent = new Intent(MediaPlayerService.PLAY_NEW_SONGS, null, SoundBoxApplication.getContext(), MediaPlayerService.class); Bundle b = new Bundle(); Collections.shuffle(songsIDs); b.putStringArrayList(BundleExtra.SONGS_ID_LIST, new ArrayList<String>(songsIDs)); b.putString(BundleExtra.CURRENT_ID, BundleExtra.DefaultValues.DEFAULT_ID); serviceIntent.putExtras(b); getActivity().startService(serviceIntent); } }); // add the button to the header of the list myListView.addHeaderView(myButton); }