List of usage examples for org.apache.commons.lang3.tuple ImmutablePair ImmutablePair
public ImmutablePair(final L left, final R right)
From source file:com.yahoo.pulsar.broker.loadbalance.impl.DeviationShedder.java
/** * Recommend that all of the returned bundles be unloaded based on observing excessive standard deviations according * to some metric.//w ww . j a v a2s. c o m * * @param loadData * The load data to used to make the unloading decision. * @param conf * The service configuration. * @return A map from all selected bundles to the brokers on which they reside. */ @Override public Map<String, String> findBundlesForUnloading(final LoadData loadData, final ServiceConfiguration conf) { final Map<String, String> result = new HashMap<>(); bundleTreeSetCache.clear(); metricTreeSetCache.clear(); double sum = 0; double squareSum = 0; final Map<String, BrokerData> brokerDataMap = loadData.getBrokerData(); // Treating each broker as a data point, calculate the sum and squared // sum of the evaluated broker metrics. // These may be used to calculate the standard deviation. for (Map.Entry<String, BrokerData> entry : brokerDataMap.entrySet()) { final double value = brokerValue(entry.getValue(), conf); sum += value; squareSum += value * value; metricTreeSetCache.add(new ImmutablePair<>(value, entry.getKey())); } // Mean cannot change by just moving around bundles. final double mean = sum / brokerDataMap.size(); double standardDeviation = Math.sqrt(squareSum / brokerDataMap.size() - mean * mean); final double deviationThreshold = getDeviationThreshold(conf); String lastMostOverloaded = null; // While the most loaded broker is above the standard deviation // threshold, continue to move bundles. while ((metricTreeSetCache.last().getKey() - mean) / standardDeviation > deviationThreshold) { final Pair<Double, String> mostLoadedPair = metricTreeSetCache.last(); final double highestValue = mostLoadedPair.getKey(); final String mostLoaded = mostLoadedPair.getValue(); final Pair<Double, String> leastLoadedPair = metricTreeSetCache.first(); final double leastValue = leastLoadedPair.getKey(); final String leastLoaded = metricTreeSetCache.first().getValue(); if (!mostLoaded.equals(lastMostOverloaded)) { // Reset the bundle tree set now that a different broker is // being considered. bundleTreeSetCache.clear(); for (String bundle : brokerDataMap.get(mostLoaded).getLocalData().getBundles()) { if (!result.containsKey(bundle)) { // Don't consider bundles that are already going to be // moved. bundleTreeSetCache.add(new ImmutablePair<>( bundleValue(bundle, brokerDataMap.get(mostLoaded), conf), bundle)); } } lastMostOverloaded = mostLoaded; } boolean selected = false; while (!(bundleTreeSetCache.isEmpty() || selected)) { Pair<Double, String> mostExpensivePair = bundleTreeSetCache.pollLast(); double loadIncurred = mostExpensivePair.getKey(); // When the bundle is moved, we want the now least loaded server // to have lower overall load than the // most loaded server does not. Thus, we will only consider // moving the bundle if this condition // holds, and otherwise we will try the next bundle. if (loadIncurred + leastValue < highestValue) { // Update the standard deviation and replace the old load // values in the broker tree set with the // load values assuming this move took place. final String bundleToMove = mostExpensivePair.getValue(); result.put(bundleToMove, mostLoaded); metricTreeSetCache.remove(mostLoadedPair); metricTreeSetCache.remove(leastLoadedPair); final double newHighLoad = highestValue - loadIncurred; final double newLowLoad = leastValue - loadIncurred; squareSum -= highestValue * highestValue + leastValue * leastValue; squareSum += newHighLoad * newHighLoad + newLowLoad * newLowLoad; standardDeviation = Math.sqrt(squareSum / brokerDataMap.size() - mean * mean); metricTreeSetCache.add(new ImmutablePair<>(newLowLoad, leastLoaded)); metricTreeSetCache.add(new ImmutablePair<>(newHighLoad, mostLoaded)); selected = true; } } if (!selected) { // Move on to the next broker if no bundle could be moved. metricTreeSetCache.pollLast(); } } return result; }
From source file:com.microsoft.tooling.msservices.helpers.ServiceCodeReferenceHelper.java
public void fillOffice365Resource(String activityName, String appId, String name, String clientId, Object module) {//from w ww. j a v a 2 s . co m DefaultLoader.getIdeHelper().replaceInFile(module, new ImmutablePair<String, String>(">$O365_APP_ID_" + activityName + "<", ">" + appId + "<"), new ImmutablePair<String, String>(">$O365_NAME_" + activityName + "<", ">" + name + "<"), new ImmutablePair<String, String>(">$O365_CLIENTID_" + activityName + "<", ">" + clientId + "<")); }
From source file:com.sludev.commons.vfs2.provider.s3.SS3FileObject.java
/** * Convenience method that returns the container ( i.e. "bucket ) and path from the current URL. * /*from www . j a va 2 s. com*/ * @return A tuple containing the bucket name and the path. */ private Pair<String, String> getContainerAndPath() { Pair<String, String> res = null; try { URLFileName currName = (URLFileName) getName(); String currPathStr = currName.getPath(); currPathStr = StringUtils.stripStart(currPathStr, "/"); if (StringUtils.isBlank(currPathStr)) { log.warn(String.format("getContainerAndPath() : Path '%s' does not appear to be valid", currPathStr)); return null; } // Deal with the special case of the container root. if (StringUtils.contains(currPathStr, "/") == false) { // Container and root return new ImmutablePair<>(currPathStr, "/"); } String[] resArray = StringUtils.split(currPathStr, "/", 2); res = new ImmutablePair<>(resArray[0], resArray[1]); } catch (Exception ex) { log.error(String.format("getContainerAndPath() : Path does not appear to be valid"), ex); } return res; }
From source file:com.streamsets.pipeline.stage.destination.hive.HiveMetastoreTargetIT.java
@Test public void testCreateNonExistingTable() throws Exception { HiveMetastoreTarget hiveTarget = new HiveMetastoreTargetBuilder().build(); TargetRunner runner = new TargetRunner.Builder(HiveMetastoreTarget.class, hiveTarget) .setOnRecordError(OnRecordError.STOP_PIPELINE).build(); runner.runInit();//from w w w. j av a2 s.c o m LinkedHashMap<String, HiveTypeInfo> columns = new LinkedHashMap<>(); columns.put("name", HiveType.STRING.getSupport().generateHiveTypeInfoFromResultSet("STRING")); LinkedHashMap<String, HiveTypeInfo> partitions = new LinkedHashMap<>(); partitions.put("dt", HiveType.STRING.getSupport().generateHiveTypeInfoFromResultSet("STRING")); Field newTableField = HiveMetastoreUtil.newSchemaMetadataFieldBuilder("default", "tbl", columns, partitions, true, BaseHiveIT.getDefaultWareHouseDir(), HiveMetastoreUtil.generateAvroSchema(columns, "tbl")); Record record = RecordCreator.create(); record.set(newTableField); Assert.assertTrue(HiveMetastoreUtil.isSchemaChangeRecord(record)); runner.runWrite(ImmutableList.of(record)); runner.runDestroy(); assertTableStructure("default.tbl", new ImmutablePair("tbl.name", Types.VARCHAR), new ImmutablePair("tbl.dt", Types.VARCHAR)); }
From source file:android.databinding.tool.util.XmlEditor.java
public static String strip(File f, String newTag) throws IOException { ANTLRInputStream inputStream = new ANTLRInputStream(new FileReader(f)); XMLLexer lexer = new XMLLexer(inputStream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); XMLParser parser = new XMLParser(tokenStream); XMLParser.DocumentContext expr = parser.document(); XMLParser.ElementContext root = expr.element(); if (root == null || !"layout".equals(nodeName(root))) { return null; // not a binding layout }/* ww w .j av a 2 s. co m*/ List<? extends ElementContext> childrenOfRoot = elements(root); List<? extends XMLParser.ElementContext> dataNodes = filterNodesByName("data", childrenOfRoot); if (dataNodes.size() > 1) { L.e("Multiple binding data tags in %s. Expecting a maximum of one.", f.getAbsolutePath()); } ArrayList<String> lines = new ArrayList<>(); lines.addAll(FileUtils.readLines(f, "utf-8")); for (android.databinding.parser.XMLParser.ElementContext it : dataNodes) { replace(lines, toPosition(it.getStart()), toEndPosition(it.getStop()), ""); } List<? extends XMLParser.ElementContext> layoutNodes = excludeNodesByName("data", childrenOfRoot); if (layoutNodes.size() != 1) { L.e("Only one layout element and one data element are allowed. %s has %d", f.getAbsolutePath(), layoutNodes.size()); } final XMLParser.ElementContext layoutNode = layoutNodes.get(0); ArrayList<Pair<String, android.databinding.parser.XMLParser.ElementContext>> noTag = new ArrayList<>(); recurseReplace(layoutNode, lines, noTag, newTag, 0); // Remove the <layout> Position rootStartTag = toPosition(root.getStart()); Position rootEndTag = toPosition(root.content().getStart()); replace(lines, rootStartTag, rootEndTag, ""); // Remove the </layout> ImmutablePair<Position, Position> endLayoutPositions = findTerminalPositions(root, lines); replace(lines, endLayoutPositions.left, endLayoutPositions.right, ""); StringBuilder rootAttributes = new StringBuilder(); for (AttributeContext attr : attributes(root)) { rootAttributes.append(' ').append(attr.getText()); } Pair<String, XMLParser.ElementContext> noTagRoot = null; for (Pair<String, XMLParser.ElementContext> pair : noTag) { if (pair.getRight() == layoutNode) { noTagRoot = pair; break; } } if (noTagRoot != null) { ImmutablePair<String, XMLParser.ElementContext> newRootTag = new ImmutablePair<>( noTagRoot.getLeft() + rootAttributes.toString(), layoutNode); int index = noTag.indexOf(noTagRoot); noTag.set(index, newRootTag); } else { ImmutablePair<String, XMLParser.ElementContext> newRootTag = new ImmutablePair<>( rootAttributes.toString(), layoutNode); noTag.add(newRootTag); } //noinspection NullableProblems Collections.sort(noTag, new Comparator<Pair<String, XMLParser.ElementContext>>() { @Override public int compare(Pair<String, XMLParser.ElementContext> o1, Pair<String, XMLParser.ElementContext> o2) { Position start1 = toPosition(o1.getRight().getStart()); Position start2 = toPosition(o2.getRight().getStart()); int lineCmp = Integer.compare(start2.line, start1.line); if (lineCmp != 0) { return lineCmp; } return Integer.compare(start2.charIndex, start1.charIndex); } }); for (Pair<String, android.databinding.parser.XMLParser.ElementContext> it : noTag) { XMLParser.ElementContext element = it.getRight(); String tag = it.getLeft(); Position endTagPosition = endTagPosition(element); fixPosition(lines, endTagPosition); String line = lines.get(endTagPosition.line); String newLine = line.substring(0, endTagPosition.charIndex) + " " + tag + line.substring(endTagPosition.charIndex); lines.set(endTagPosition.line, newLine); } return StringUtils.join(lines, System.getProperty("line.separator")); }
From source file:com.netflix.spinnaker.halyard.config.validate.v1.DeploymentConfigurationValidator.java
@Override public void validate(ConfigProblemSetBuilder p, DeploymentConfiguration n) { String timezone = n.getTimezone(); if (Arrays.stream(TimeZone.getAvailableIDs()).noneMatch(t -> t.equals(timezone))) { p.addProblem(Problem.Severity.ERROR, "Timezone " + timezone + " does not match any known canonical timezone ID").setRemediation( "Pick a timezone from those listed here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"); }// www. j ava 2s . co m String version = n.getVersion(); Versions versions = versionsService.getVersions(); if (StringUtils.isEmpty(version)) { p.addProblem(Problem.Severity.WARNING, "You have not yet selected a version of Spinnaker to deploy.", "version"); return; } Optional<Versions.IllegalVersion> illegalVersion = versions.getIllegalVersions().stream() .filter(v -> v.getVersion().equals(version)).findAny(); if (illegalVersion.isPresent()) { p.addProblem(Problem.Severity.ERROR, "Version \"" + version + "\" may no longer be deployed with Halyard: " + illegalVersion.get().getReason()); return; } try { versionsService.getBillOfMaterials(version); } catch (HalException e) { p.extend(e); return; } catch (Exception e) { p.addProblem(Problem.Severity.FATAL, "Unexpected error trying to validate version \"" + version + "\": " + e.getMessage(), "version"); return; } boolean isReleased = versions.getVersions().stream().anyMatch(v -> Objects.equals(v.getVersion(), version)); if (!isReleased) { // Checks if version is of the form X.Y.Z if (version.matches("\\d+\\.\\d+\\.\\d+")) { String majorMinor = Versions.toMajorMinor(version); Optional<Versions.Version> patchVersion = versions.getVersions().stream() .map(v -> new ImmutablePair<>(v, Versions.toMajorMinor(v.getVersion()))) .filter(v -> v.getRight() != null).filter(v -> v.getRight().equals(majorMinor)) .map(ImmutablePair::getLeft).findFirst(); if (patchVersion.isPresent()) { p.addProblem(Problem.Severity.WARNING, "Version \"" + version + "\" was patched by \"" + patchVersion.get().getVersion() + "\". Please upgrade when possible.") .setRemediation("https://spinnaker.io/setup/install/upgrades/"); } else { p.addProblem(Problem.Severity.WARNING, "Version \"" + version + "\" is no longer supported by the Spinnaker team. Please upgrade when possible.") .setRemediation("https://spinnaker.io/setup/install/upgrades/"); } } else { p.addProblem(Problem.Severity.WARNING, "Version \"" + version + "\" is not a released (validated) version of Spinnaker.", "version"); } } }
From source file:com.microsoft.tooling.msservices.serviceexplorer.azure.storage.StorageModule.java
@Override protected void refreshItems() throws AzureCmdException { List<Pair<String, String>> failedSubscriptions = new ArrayList<>(); try {/*from www . ja va 2s.c om*/ AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager(); // not signed in if (azureManager == null) { return; } SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager(); Set<String> sidList = subscriptionManager.getAccountSidList(); for (String sid : sidList) { try { Azure azure = azureManager.getAzure(sid); List<com.microsoft.azure.management.storage.StorageAccount> storageAccounts = azure .storageAccounts().list(); for (StorageAccount sm : storageAccounts) { addChildNode(new StorageNode(this, sid, sm)); } } catch (Exception ex) { failedSubscriptions.add(new ImmutablePair<>(sid, ex.getMessage())); continue; } } } catch (Exception ex) { DefaultLoader.getUIHelper() .logError("An error occurred when trying to load Storage Accounts\n\n" + ex.getMessage(), ex); } // public List<ArmStorageAccount> execute(@NotNull Azure azure) throws Throwable { // List<ArmStorageAccount> storageAccounts = new ArrayList<>(); // for (StorageAccount storageAccount : azure.storageAccounts().list()){ // ArmStorageAccount sa = new ArmStorageAccount(storageAccount.name(), subscriptionId, storageAccount); // // sa.setProtocol("https"); // sa.setType(storageAccount.sku().name().toString()); // sa.setLocation(Strings.nullToEmpty(storageAccount.regionName())); // List<StorageAccountKey> keys = storageAccount.keys(); // if (!(keys == null || keys.isEmpty())) { // sa.setPrimaryKey(keys.get(0).value()); // if (keys.size() > 1) { // sa.setSecondaryKey(keys.get(1).value()); // } // } // sa.setResourceGroupName(storageAccount.resourceGroupName()); // storageAccounts.add(sa); // } // return storageAccounts; // } //TODO // load External Accounts for (ClientStorageAccount clientStorageAccount : ExternalStorageHelper.getList(getProject())) { ClientStorageAccount storageAccount = StorageClientSDKManager.getManager() .getStorageAccount(clientStorageAccount.getConnectionString()); // addChildNode(new ExternalStorageNode(this, storageAccount)); } if (!failedSubscriptions.isEmpty()) { StringBuilder errorMessage = new StringBuilder( "An error occurred when trying to load Storage Accounts for the subscriptions:\n\n"); for (Pair error : failedSubscriptions) { errorMessage.append(error.getKey()).append(": ").append(error.getValue()).append("\n"); } DefaultLoader.getUIHelper().logError( "An error occurred when trying to load Storage Accounts\n\n" + errorMessage.toString(), null); } }
From source file:de.flashpixx.rrd_antlr4.antlr.CASTVisitorPCRE.java
@Override public final Object visitElement(final PCREParser.ElementContext p_context) { // negation is part of a shared-literal, so a check is needed later, // return the atom and a quatifier of the atom return new ImmutablePair<>(this.visitAtom(p_context.atom()), p_context.quantifier() != null ? p_context.quantifier().getText() : ""); }
From source file:ezbake.frack.common.workers.WarehausWorkerTest.java
@Test @SuppressWarnings("unchecked") public void testOutputToPipesShouldGetCalledWithTheArticle() throws Exception { final List<ImmutablePair<Visibility, StreamEvent>> actual = newArrayList(); final StreamEvent event = TestHelper.buildEvent(); final Visibility tupleVisibility = new Visibility().setFormalVisibility("U"); final Visibility documentVisibility = new Visibility().setFormalVisibility("TS"); IThriftConverter<StreamEvent, Visibility> classificationConverter = when( mock(IThriftConverter.class).convert(event)).thenReturn(documentVisibility).getMock(); final Repository repository = TestHelper.buildRepository(); IThriftConverter<StreamEvent, Repository> repositoryConverter = when( mock(IThriftConverter.class).convert(event)).thenReturn(repository).getMock(); final WarehausService.Client mockWarehaus = mock(WarehausService.Client.class); final ThriftClientPool mockPool = when( mock(ThriftClientPool.class).getClient("warehaus", WarehausService.Client.class)) .thenReturn(mockWarehaus).getMock(); final EzSecurityTokenWrapper token = getEzSecurityToken(); final EzbakeSecurityClient mockClient = when(mock(EzbakeSecurityClient.class).fetchAppToken(SECURITY_ID)) .thenReturn(token).getMock(); WarehausWorker<StreamEvent> warehausWorker = new WarehausWorker<StreamEvent>(StreamEvent.class, repositoryConverter, classificationConverter) { @Override/* ww w .j a va 2s .c o m*/ public void initialize(Properties props) { this.warehausSecurityId = SECURITY_ID; this.pool = mockPool; this.securityClient = mockClient; } @Override protected void outputToPipes(Visibility visibility, Serializable thriftStruct) throws IOException { StreamEvent outputEvent = (StreamEvent) thriftStruct; actual.add(new ImmutablePair<>(visibility, outputEvent)); } }; warehausWorker.initialize(new Properties()); warehausWorker.process(tupleVisibility, event); assertTrue("Document was not output to pipe", actual.contains(new ImmutablePair<>(documentVisibility, event))); verify(mockWarehaus).insert(repository, documentVisibility, token); verify(mockPool).getClient("warehaus", WarehausService.Client.class); verify(mockPool).returnToPool(mockWarehaus); }
From source file:net.malisis.blocks.item.MixedBlockBlockItem.java
public static Pair<IBlockState, IBlockState> readNBT(NBTTagCompound nbt) { return new ImmutablePair(MBlockState.fromNBT(nbt, "block1", "metadata1"), MBlockState.fromNBT(nbt, "block2", "metadata2")); }