GetLogarithmicDistribution.java 文件源码

java
阅读 24 收藏 0 点赞 0 评论 0

项目:windup 作者:
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    if (arguments.size() < 2)
        throw new TemplateModelException(NAME + ": Expected 2 arguments, count and maximum count.");

    if (!(arguments.get(0) instanceof TemplateNumberModel))
        throw new TemplateModelException(NAME + ": Both arguments must be numbers, but the first was " + arguments.get(0).getClass().getName());
    if (!(arguments.get(1) instanceof TemplateNumberModel))
        throw new TemplateModelException(NAME + ": Both arguments must be numbers, but the second was " + arguments.get(1).getClass().getName());

    int count = ((TemplateNumberModel) arguments.get(0)).getAsNumber().intValue();
    if (count < 1)
        return new SimpleNumber(0);

    int maximum = ((TemplateNumberModel) arguments.get(1)).getAsNumber().intValue();
    if (maximum < 0)
        throw new TemplateModelException(NAME + "Maximum must be at least 0, " + maximum);

    if (count > maximum)
    {
        LOG.severe("Count " + count + " is larger than maximum " + maximum + ". Using the maximum as count.");
        count = maximum;
    }

    double ratio = ((double)count) / ((double)maximum + QUITE_A_LOT_FACTOR); //  <0..1>

    // Map it to scale 1..1000.
    double ratio2 = 1.0d + ratio * (998d * (1-FLATTENER));
    double log10 = Math.log10(ratio2) / 3D;    // 0..2.999  =>  0..0.999
    //LOG.info(String.format("count: %d, max: %d, ratio %f, ratio2 %f, log10 %f", count, maximum, ratio, ratio2, log10));
    return new NumberModel(Double.valueOf(log10), new DefaultObjectWrapper());
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号