protected String toString()

in velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/ComparisonDateTool.java [707:803]


        protected String toString(long diff, int maxUnitDepth)
        {
            long value = 0;
            long remainder = 0;
            String unitKey = null;

            // can't go any deeper than we have units
            if (maxUnitDepth > timeUnits.size())
            {
                maxUnitDepth = timeUnits.size();
            }

            if (type == EXACT_TYPE)
            {
                // diff is a time unit index
                for (remainder = diff; remainder < CALENDAR_FIELDS.length; ++remainder)
                {
                    long val = getExactDifference()[(int)remainder];
                    if (value == 0)
                    {
                        value = val;
                        unitKey = UNIT_KEYS[(int)remainder];
                    }
                    else if (val > 0)
                    {
                        break;
                    }
                }
                remainder %= CALENDAR_FIELDS.length;

                // these cases should be handled elsewhere
                if (value == 0)
                {
                    return null;
                }
            }
            else
            {
                // these cases should be handled elsewhere
                if (diff <= 0)
                {
                    return null;
                }

                // determine the largest unit and calculate the value and remainder
                Iterator<String> i = timeUnits.keySet().iterator();
                unitKey = i.next();
                long unit = timeUnits.get(unitKey);
                while (i.hasNext())
                {
                    // get the next unit
                    String nextUnitKey = (String)i.next();
                    long nextUnit = timeUnits.get(nextUnitKey);

                    // e.g. if diff < <nextUnit>
                    if (diff < nextUnit)
                    {
                        // then we're working with <unit>
                        value = diff / unit;
                        remainder = diff % unit;
                        break;
                    }

                    // shift to the next unit
                    unitKey = nextUnitKey;
                    unit = nextUnit;
                }

                // if it was years, then we haven't done the math yet
                if (unitKey.equals(YEAR_KEY))
                {
                    value = diff / unit;
                    remainder = diff % unit;
                }
            }

            // select proper pluralization
            if (value != 1)
            {
                unitKey += PLURAL_SUFFIX;
            }

            if (abbreviate)
            {
                unitKey += ABBR_SUFFIX;
            }

            // combine the value and the unit
            String output = value + " " + getText(unitKey, locale);

            // recurse over the remainder if it exists and more units are allowed
            if (maxUnitDepth > 1 && remainder > 0)
            {
                output += " " + toString(remainder, maxUnitDepth - 1);
            }
            return output;
        }