简体   繁体   English

用另一个流中的字符串替换一个流的字符串

[英]Replace string of one stream with string in another stream

Fun with streams... I have a List<String> of tokenized URLs, and a List<UrlParametersConfig.UrlParameter> of all the parameter values for those tokens. 有趣的流...我有一个标记化URL的List<String> ,以及这些标记的所有参数值的List<UrlParametersConfig.UrlParameter> For example, my yaml file looks like: 例如,我的yaml文件看起来像:

urlParameterList:
  urlParameter:
  -
    parameter: propertyId
    value: c3329fc
 -
    parameter: cartId
    value: 123456
 ...

And the corresponding config class looks like this: 相应的配置类如下所示:

@Configuration
@ConfigurationProperties(prefix="urlParameterList")
public class UrlParametersConfig {

    private List<UrlParameter> urlParameters = new ArrayList<>();

    public UrlParametersConfig() {}

    public List<UrlParameter> getUrlParameter() { return this.urlParameters; }

    public static class UrlParameter {
        private String parameter;
        private String value;

        public String getParameter() {
            return parameter;
        }
        public void setParameter(String parameter) {
            this.parameter = parameter;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
}

So I have a list of URLs that contains something like www.comany.com/v2/rest/v2/property/{propertyId} www.comany.com/v2/rest/v2/sale/{propertyId}/{cartId} 所以我有一个URL列表,其中包含www.comany.com/v2/rest/v2/property/{propertyId} www.comany.com/v2/rest/v2/sale/{propertyId}/{cartId}

Everywhere {propertyId} is referenced I need to substitute c3329fc , and everywhere {cartId} is referenced I need to substitute 123456a . 引用{propertyId}到处我需要替换c3329fc ,并且引用{cartId}到处都需要替换123456a I figured out I'd be working with two streams, but I don't know which to stream first, the list of all URLs or the list of config parameters. 我想我将使用两个流,但我不知道首先流,哪个URL列表或配置参数列表。 Can anyone show me what this token replacement code should look like? 任何人都可以告诉我这个令牌替换代码应该是什么样子? So far this is what I have: 到目前为止,这就是我所拥有的:

List<String> parameterizeUrls = uris.stream()
        .forEach(it -> {
            if (it.contains(parameters.stream()
                  .map(UrlParametersConfig.UrlParameter::getParameter)
                  .collect(Collectors.toList()))
   // do something?
        });

If I understood correclty the idea, we want to obtain the following: 如果我理解了这个想法,我们想获得以下内容:

www.comany.com/v2/rest/v2/property/c3329fc
www.comany.com/v2/rest/v2/sale/c3329fc/123456

One stream is necessary to loop on the list of original url s with tokens. 需要一个流来循环使用令牌的原始url列表。 For each url , another stream will loop on the list of parameters, executing reduction of the url for each parameter. 对于每个url ,另一个流将在参数列表上循环,执行每个参数的url减少。

To be more clear and to avoid too much nesting, the algo is split in the following parts: 为了更清楚并避免过多的嵌套,算法分为以下几部分:

1) Main loop, transforming the original list of urls with tokens into urls with replaced values: 1)主循环,将带有标记的URL的原始列表转换为具有替换值的URL:

List<String> urlsTokenReplaced = urls.stream().map(url ->
  replaceTokens(url, params)
).collect(Collectors.toList());

2) For the specified url , replaces the occurrences of each token from the list. 2)对于指定的url ,从列表中替换每个标记的出现次数。

private static String replaceTokens(String url, List<UrlParametersConfig.UrlParameter> params) {
  return params.stream()
               .reduce(url, {ClassName}::replaceToken, (prev, cur) -> cur);
}

Update 更新

Details on how the reduction operation works: Java tutorials . 有关还原操作如何工作的详细信息: Java教程

For this particular case: 对于这个特殊情况:

  • url param is the starting value of the loop, for example www.comany.com/v2/rest/v2/sale/{propertyId}/{cartId} . url param是循环的起始值,例如www.comany.com/v2/rest/v2/sale/{propertyId}/{cartId}

  • {ClassName}::replaceToken references the static method below, it is the so-called accumulator function, which takes the previous (or starting) value and produces a new value by applying the current UrlParameter on it. {ClassName}::replaceToken引用下面的静态方法,它是所谓的累加器函数,它接受先前(或起始)值并通过在其上应用当前UrlParameter来生成新值。 In our case, on the first pass it will produce www.comany.com/v2/rest/v2/sale/c3329fc/{cartId} and www.comany.com/v2/rest/v2/sale/c3329fc/123456 on the second. 在我们的案例中,在第一次通过时,它将生成www.comany.com/v2/rest/v2/sale/c3329fc/{cartId}www.comany.com/v2/rest/v2/sale/c3329fc/123456第二。

  • (prev, cur) -> cur) is the so-called collector function. (prev, cur) -> cur)是所谓的收集函数。 It defines what to do with the prev ious and the cur rent (produced by the accumulator) values. 它定义了如何处理prev ious和cur rent(由累加器产生)值。 Here we just forget about the previous value and return the current, which will be reused by the accumulator on next iteration of the loop and so on. 在这里,我们忘记前一个值并返回当前的值,这将在循环的下一次迭代中由累加器重用,依此类推。 In our case, on the first pass it will return the second value between www.comany.com/v2/rest/v2/sale/{propertyId}/{cartId} and www.comany.com/v2/rest/v2/sale/c3329fc/{cartId} . 在我们的例子中,在第一遍中它将返回www.comany.com/v2/rest/v2/sale/{propertyId}/{cartId}www.comany.com/v2/rest/v2/sale/c3329fc/{cartId}之间的第二个值。 www.comany.com/v2/rest/v2/sale/c3329fc/{cartId} On the second pass, it will return the second value from www.comany.com/v2/rest/v2/sale/c3329fc/{cartId} and www.comany.com/v2/rest/v2/sale/c3329fc/123456 . 在第二遍,它将从www.comany.com/v2/rest/v2/sale/c3329fc/{cartId}www.comany.com/v2/rest/v2/sale/c3329fc/123456返回第二个值。

When all the UrlParameter s from the stream are passed through, the last value produced by the accumulator (and thus the collector here) is returned from the method. 当流中的所有UrlParameter都通过时,从该方法返回由累加器(以及此处的收集器)生成的最后一个值。 If the stream were empty, the starting value (first arg) would be returned. 如果流为空,则返回起始值(第一个arg)。

enf-of-update ENF-的更新

3) Replaces a single occurrence of the specified token in the url : 3)在url替换指定标记的单个匹配项:

private static String replaceToken(String url, UrlParametersConfig.UrlParameter param) {
  String replaceRegex = "\\{" + param.getParameter() + "\\}";
  return url.replaceAll(replaceRegex, param.getValue());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM