View Javadoc

1   package com.sharkysoft.printf;
2   
3   import com.sharkysoft.printf.engine.StringFormatter;
4   
5   final class Formatter_s extends Formatter_Field
6   {
7   
8     final int mnPrecision;
9   
10    Formatter_s(final FormatSpecifier ipPfs)
11    {
12      super(ipPfs, new StringFormatter());
13      mnPrecision = ipPfs.mnPrecision;
14    }
15    
16    void format(final PrintfState ipPs)
17    {
18      adjustWidth(ipPs);
19      int vnPrec = mnPrecision;
20      if (vnPrec == FormatSpecifier.PRECISION_ARGUMENT)
21      {
22        vnPrec = ((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).intValue();
23        if (vnPrec < 0)
24          throw new PrintfTemplateException("variable precision=" + mnPrecision);
25      }
26      final Object vpO = ipPs.mapArgs[ipPs.mnArgIndex++];
27      String vsS;
28      if (vpO != null)
29        vsS = vpO.toString();
30      else
31        vsS = "null";
32      if (vnPrec >= 0)
33      {
34        if (vsS.length() > vnPrec)
35          vsS = vsS.substring(0, mnPrecision);
36      }
37      ipPs.mpOutput.append(mpFormatter.format(vsS));
38    }
39    
40    final int argsRequired()
41    {
42      return 1
43        + (mnPrecision == FormatSpecifier.PRECISION_ARGUMENT ? 1 : 0)
44        + (mzVariableWidth ? 1 : 0);
45    }
46  
47      Class[] argTypes() {
48          if (mzVariableWidth && mnPrecision == FormatSpecifier.PRECISION_ARGUMENT) {
49                  return new Class[] { Number.class, Number.class, Object.class };
50          } else if (!mzVariableWidth && mnPrecision != FormatSpecifier.PRECISION_ARGUMENT) {
51              return new Class[] { Object.class };
52          } else {
53              return new Class[] { Number.class, Object.class };
54          }
55      }
56  
57  }
58