根据数组列表大小如何自动化函数

huangapple go评论82阅读模式
英文:

How to automate function according to Array list size

问题

抱歉,此问题标题不完全正确。因此,我将在这里解释我的情况。
我创建了一个函数,将4个数据集合并为一个返回格式。因为这是前端所需的格式。所以现在这个功能正常工作。

public ReturnFormat makeThribleLineChart(List<NameCountModel> totalCount, List<NameCountModel> p1Count, List<NameCountModel> p2Count, List<NameCountModel> average) {

    ReturnFormat returnFormat = new ReturnFormat(null, null);

    try {

        String[] totalData = new String[totalCount.size()];
        String[] p1Data = new String[p1Count.size()];
        String[] p2Data = new String[p2Count.size()];
        String[] averageData = new String[p2Count.size()];
        String[] lableList = new String[totalCount.size()];

        for (int x = 0; x < totalCount.size(); x++) {
            totalData[x] = totalCount.get(x).getCount();
            p1Data[x] = p1Count.get(x).getCount();
            p2Data[x] = p2Count.get(x).getCount();
            averageData[x] = average.get(x).getCount();
            lableList[x] = totalCount.get(x).getName();
        }

        FormatHelper<String[]> totalFormatHelper = new FormatHelper<String[]>();
        totalFormatHelper.setData(totalData);
        totalFormatHelper.setType("line");
        totalFormatHelper.setLabel("Uudet");
        totalFormatHelper.setyAxisID("y-axis-1");

        FormatHelper<String[]> p1FormatHelper = new FormatHelper<String[]>();
        p1FormatHelper.setData(p1Data);
        p1FormatHelper.setType("line");
        p1FormatHelper.setLabel("P1 päivystykseen heti");

        FormatHelper<String[]> p2FormatHelper = new FormatHelper<String[]>();
        p2FormatHelper.setData(p2Data);
        p2FormatHelper.setType("line");
        p2FormatHelper.setLabel("P2 päivystykseen muttei yöllä");

        FormatHelper<String[]> averageFormatHelper = new FormatHelper<String[]>();
        averageFormatHelper.setData(averageData);
        averageFormatHelper.setType("line");
        averageFormatHelper.setLabel("Jonotusaika keskiarvo");
        averageFormatHelper.setyAxisID("y-axis-2");

        List<FormatHelper<String[]>> formatHelpObj = new ArrayList<FormatHelper<String[]>>();
        formatHelpObj.add(totalFormatHelper);
        formatHelpObj.add(p1FormatHelper);
        formatHelpObj.add(p2FormatHelper);
        formatHelpObj.add(averageFormatHelper);

        returnFormat.setData(formatHelpObj);
        returnFormat.setLabels(lableList);
        returnFormat.setMessage(Messages.Success);
        returnFormat.setStatus(ReturnFormat.Status.SUCCESS);

    } catch (Exception e) {

        returnFormat.setData(null);
        returnFormat.setMessage(Messages.InternalServerError);
        returnFormat.setStatus(ReturnFormat.Status.ERROR);

    }
    return returnFormat;

}

因此,正如您在这里看到的,所有的格式化都是硬编码的。所以我的问题是如何自动化这段代码,以适应列表数量。假设下次我必须为五个数据集创建图表格式。那么我需要再创建一个函数。这是我想要减少的部分。希望您能理解我的问题。

谢谢。

英文:

I'm sorry this question header is not 100% correct. Because of that, I'll explain my scenario here.
I created a function to merge 4 data sets into one return format. Because that's the format front-end side needed. So this is working fine now.

public ReturnFormat makeThribleLineChart(List&lt;NameCountModel&gt; totalCount, List&lt;NameCountModel&gt;,p1Count, List&lt;NameCountModel&gt; p2Count, List&lt;NameCountModel&gt; average) {
ReturnFormat returnFormat = new ReturnFormat(null,null);
try {
String[] totalData = new String[totalCount.size()];
String[] p1Data = new String[p1Count.size()];
String[] p2Data = new String[p2Count.size()];
String[] averageData = new String[p2Count.size()];
String[] lableList = new String[totalCount.size()];
for (int x = 0; x &lt; totalCount.size(); x++) {
totalData[x] = totalCount.get(x).getCount();
p1Data[x] = p1Count.get(x).getCount();
p2Data[x] = p2Count.get(x).getCount();
averageData[x] = average.get(x).getCount();
lableList[x] = totalCount.get(x).getName();
}
FormatHelper&lt;String[]&gt; totalFormatHelper= new FormatHelper&lt;String[]&gt;();
totalFormatHelper.setData(totalData);
totalFormatHelper.setType(&quot;line&quot;);
totalFormatHelper.setLabel(&quot;Uudet&quot;);
totalFormatHelper.setyAxisID(&quot;y-axis-1&quot;);
FormatHelper&lt;String[]&gt; p1FormatHelper= new FormatHelper&lt;String[]&gt;();
p1FormatHelper.setData(p1Data);
p1FormatHelper.setType(&quot;line&quot;);
p1FormatHelper.setLabel(&quot;P1 p&#228;ivystykseen heti&quot;);
FormatHelper&lt;String[]&gt; p2FormatHelper= new FormatHelper&lt;String[]&gt;();
p2FormatHelper.setData(p2Data);
p2FormatHelper.setType(&quot;line&quot;);
p2FormatHelper.setLabel(&quot;P2 p&#228;ivystykseen muttei y&#246;ll&#228;&quot;);
FormatHelper&lt;String[]&gt; averageFormatHelper= new FormatHelper&lt;String[]&gt;();
averageFormatHelper.setData(averageData);
averageFormatHelper.setType(&quot;line&quot;);
averageFormatHelper.setLabel(&quot;Jonotusaika keskiarvo&quot;);
averageFormatHelper.setyAxisID(&quot;y-axis-2&quot;);
List&lt;FormatHelper&lt;String[]&gt;&gt; formatHelpObj = new ArrayList&lt;FormatHelper&lt;String[]&gt;&gt;();
formatHelpObj.add(totalFormatHelper);
formatHelpObj.add(p1FormatHelper);
formatHelpObj.add(p2FormatHelper);
formatHelpObj.add(averageFormatHelper);
returnFormat.setData(formatHelpObj);
returnFormat.setLabels(lableList);
returnFormat.setMessage(Messages.Success);
returnFormat.setStatus(ReturnFormat.Status.SUCCESS);
} catch (Exception e) {
returnFormat.setData(null);
returnFormat.setMessage(Messages.InternalServerError);
returnFormat.setStatus(ReturnFormat.Status.ERROR);
}
return returnFormat;
}

so, as you can see here, all the formatting is hardcoded. So my question is how to automate this code for list count. Let's assume next time I have to create chart formatting for five datasets. So I have to create another function to it. That's the thing I want to reduce. So I hope you can understand my question.

Thank you.

答案1

得分: 2

你正在尝试解决更一般的问题,即基于动态信息来组合结果对象(在本例中是ReturnFormat)。此外,每个数据集都设置了一些元数据 - 类型、标签等。在你所提供的示例中,你已经硬编码了数据集与这些元数据之间的关系,但如果这里有可变数量的参数,你需要一种方法来动态建立这种关系。

因此,你有几个选项:

  • makeThribleLineChart 设为 varargs 方法,以接受代表你的数据的可变数量参数。现在你面临的问题是将元数据与参数关联起来 - 最好的选择可能是将数据和元数据一起包装在某个新对象中,然后作为 makeThribleLineChart 的每个参数提供。因此,你最终会得到一个签名,看起来有点像 ReturnFormat makeThribleLineChart(DataMetadataWrapper... allDatasets),其中 DataMetadataWrapper 包含构建一个 FormatHelper 实例所需的一切。
  • 使用生成器模式,类似于 guava 中的集合生成器,例如下面这样:

class ThribbleLineChartBuilder {
    List&lt;FormatHelper&lt;String[]&gt;&gt; formatHelpObj = new ArrayList&lt;&gt;();

    ThribbleLineChartBuilder addDataSet(String describeType, String label, String yAxisId, List&lt;NameCountModel&gt; data) {
        String[] dataArray = ... ; // 构建数据数组

        FormatHelper&lt;String[]&gt; formatHelper = new FormatHelper&lt;String[]&gt;();
        formatHelper.setData(dataArray);
        formatHelper.setType(describeType);
        ... // 在这里设置 FormatHelper 需要的任何其他参数

        formatHelpObj.add(formatHelper);

        return this;
    }

    ReturnFormat build() {
        ReturnFormat returnFormat = new ReturnFormat(null, null);
        returnFormat.setData(this.formatHelpObj);
        ... // 设置 ReturnFormat 中你需要的任何其他字段

        return returnFormat;
    }
}

// 使用示例:
new ThribbleLineChartBuilder()
 .addDataSet(&quot;line&quot;, &quot;Uudet&quot;, &quot;y-axis-1&quot;, totalCount)
 .addDataSet(&quot;line&quot;, &quot;P1 p&#228;ivystykseen heti&quot;, null, p1Count)
 ... // 设置其他数据源
 .build()
英文:

You're trying to solve the more general problem of composing a result object (in this case ReturnFormat) based on dynamic information. In addition, there's some metadata being setup along with each dataset - the type, label, etc. In the example that you've posted, you've hardcoded the relationship between a dataset and this metadata, but you'd need some way to establish this relationship for data dynamically if you have a variable number of parameters here.

Therefore, you have a couple of options:

  • Make makeThribleLineChart a varargs method to accept a variable number of parameters representing your data. Now you have the problem of associating metadata with your parameters - best option is probably to wrap the data and metadata together in some new object that is provided as each param of makeThribleLineChart.
    So you'll end up with a signature that looks a bit like ReturnFormat makeThribleLineChart(DataMetadataWrapper... allDatasets), where DataMetadataWrapper contains everything required to build one FormatHelper instance.
  • Use a builder pattern, similar to the collection builders in guava, for example something like so:

class ThribbleLineChartBuilder {
    List&lt;FormatHelper&lt;String[]&gt;&gt; formatHelpObj = new ArrayList&lt;&gt;();

    ThribbleLineChartBuilder addDataSet(String describeType, String label, String yAxisId, List&lt;NameCountModel&gt; data) {
        String[] dataArray = ... ; // build your array of data

        FormatHelper&lt;String[]&gt; formatHelper = new FormatHelper&lt;String[]&gt;();
        formatHelper.setData(dataArray);
        formatHelper.setType(describeType);
        ... // set any other parameters that the FormatHelper requires here

        formatHelpObj.add(formatHelper);

        return this;
    }

    ReturnFormat build() {
        ReturnFormat returnFormat = new ReturnFormat(null, null);
        returnFormat.setData(this.formatHelpObj);
        ... // setup any other fields you need in ReturnFormat

        return returnFormat;
    }
}

// usage:
new ThribbleLineChartBuilder()
 .addDataSet(&quot;line&quot;, &quot;Uudet&quot;, &quot;y-axis-1&quot;, totalCount)
 .addDataSet(&quot;line&quot;, &quot;P1 p&#228;ivystykseen heti&quot;, null, p1Count)
 ... // setup your other data sources
 .build()

huangapple
  • 本文由 发表于 2020年4月6日 14:00:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61053760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定