FreeMarker代码分析(10)

FreeMarker代码分析(10),第1张

FreeMarker代码分析(10)

2021SC@SDUSC

**内容非常主观,可能出现错漏,慎重参考

_DelayedJoinWithComma.java

package freemarker.core;


public class _DelayedJoinWithComma extends _DelayedConversionToString {

    public _DelayedJoinWithComma(String[] items) {
        super(items);
    }

    @Override
    protected String doConversion(Object obj) {
        String[] items = (String[]) obj;
        
        int totalLength = 0;
        for (int i = 0; i < items.length; i++) {
            if (i != 0) totalLength += 2;
            totalLength += items[i].length();
        }
        
        StringBuilder sb = new StringBuilder(totalLength);
        for (int i = 0; i < items.length; i++) {
            if (i != 0) sb.append(", ");
            sb.append(items[i]);
        }
        
        return sb.toString();
    }

}

重写doConversion,用for循环记录items数组的长度,在每个元素后加“,”

_DelayedJQuote.java

package freemarker.core;

import freemarker.template.utility.StringUtil;


public class _DelayedJQuote extends _DelayedConversionToString {

    public _DelayedJQuote(Object object) {
        super(object);
    }

    @Override
    protected String doConversion(Object obj) {
        return StringUtil.jQuote(_ErrorDescriptionBuilder.toString(obj));
    }

}

引用父类的object,重写doConversion

_DelayedOrdinal.java

package freemarker.core;


public class _DelayedOrdinal extends _DelayedConversionToString {

    public _DelayedOrdinal(Object object) {
        super(object);
    }

    @Override
    protected String doConversion(Object obj) {
        if (obj instanceof Number) {
            long n = ((Number) obj).longValue();
            if (n % 10 == 1 && n % 100 != 11) {
                return n + "st";
            } else if (n % 10 == 2 && n % 100 != 12) {
                return n + "nd";
            } else if (n % 10 == 3 && n % 100 != 13) {
                return n + "rd";
            } else {
                return n + "th";
            }
        } else {
            return "" + obj;
        }
    }
    
}

是_DelayedConversionToString的子类,重写doConversion方法,用if语句来判断

_DelayedShortClassName.java

package freemarker.core;

import freemarker.template.utility.ClassUtil;

public class _DelayedShortClassName extends _DelayedConversionToString {

    public _DelayedShortClassName(Class pClass) {
        super(pClass);
    }

    @Override
    protected String doConversion(Object obj) {
        return ClassUtil.getShortClassName((Class) obj, true);
    }

}

是_DelayedConversionToString的子类,引用pClass,重写doConversion,返回ClassUtil的类名

_DelayedToString.java

package freemarker.core;

public class _DelayedToString extends _DelayedConversionToString {

    public _DelayedToString(Object object) {
        super(object);
    }

    public _DelayedToString(int object) {
        super(Integer.valueOf(object));
    }
    
    @Override
    protected String doConversion(Object obj) {
        return String.valueOf(obj);
    }
    
}

是_DelayedConversionToString的子类,引用object,重写doConversion

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5660719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存