DateFormatter.java
/*
** Module : DateFormatter.java
** Abstract : Format Progress date objects.
**
** Copyright (c) 2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description---------------------------------
** 001 VVT 20210413 Created initial version.
** VVT 20210426 The class API re-designed.
** VVT 20210428 Import statements fixed.
** VVT 20210624 Javadoc fixed
** VVT 20210917 Javadoc fixed.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.util;
import java.text.NumberFormat;
import com.goldencode.p2j.ui.client.format.*;
/**
* Progress date formatter.
*
*/
public class DateFormatter
extends DateFormatParser
{
/**
* The constructor.
*
* @param value
* the date instance to format
* @param format
* the date format to use
* @param order
* The date component order spec: array of 3 elements. Values can be {@link date#YEAR},
* {@link date#MONTH}, or {@link date#DAY}.
*/
public DateFormatter(final date value, final String format, final byte[] order)
{
this.value = value;
this.format = format;
this.order = order;
}
/** The date to format */
private final date value;
/** The output formatted value collector */
private final StringBuilder sb = new StringBuilder();
/** The date format, used for error reporting. */
private final String format;
/** Set to true when the first "value cannot be formatted" error is occurred.
* This deferred value error handling is required to assert the format is always parsed completely,
* and any format errors are reported first, to match the OE behavior. */
private boolean deferredValueError;
/** The date component order spec: array of 3 elements. Values can be {@link date#YEAR},
* {@link date#MONTH}, or {@link date#DAY} */
private final byte[] order;
/**
* Format date using the specified format string.
*
* @return the formatted date
*/
public String format()
{
sb.setLength(0);
parse(format);
if (deferredValueError)
{
DisplayFormat.genValueCannotBeDisplayedError(value.toStringExport(), format);
return null;
}
return sb.toString();
}
/**
* The the year component of the date spec.
* <p>
* Note: the length of the first date component <b>can be zero</b>.
*
* @param startIdx
* the component start index, zero based
* @param endIdx
* the component end index, zero based, exclusive
*/
protected void visitYear(final int startIdx, final int endIdx)
{
// this method can be called from the parse method
// with no value provided.
// If the year spec is empty, just do nothing
if (deferredValueError || value == null || startIdx == endIdx)
{
return;
}
final int len = endIdx - startIdx;
final String yr = toYearString(len);
if (yr == null)
{
deferredValueError = true;
return;
}
sb.append(yr);
}
/**
* The the month component of the date spec.
* <p>
* Note: the length of the component <b>can be zero</b>.
*
* @param startIdx
* the component start index, zero based
* @param endIdx
* the component end index, zero based, exclusive
*/
protected void visitMonth(final int startIdx, final int endIdx)
{
// this method can be called from the parse method
// with no value provided.
if (deferredValueError || value == null)
{
return;
}
formatDayOrMonth(startIdx, endIdx, value.getMonth());
}
/**
* The day component of the date spec.
* <p>
* Note: the length of the component <b>can be zero</b>.
*
* @param startIdx
* the component start index, zero based
* @param endIdx
* the component end index, zero based, exclusive
*/
protected void visitDay(final int startIdx, final int endIdx)
{
// this method can be called from the parse method
// with no value provided.
if (deferredValueError || value == null)
{
return;
}
formatDayOrMonth(startIdx, endIdx, value.getDay());
}
/**
* Dispatch the date component to a matching callback.
*
* @param componentIndex
* the number of the component in the format spec
* @param startIdx
* the component start index, zero based
* @param endIdx
* the component end index, zero based, exclusive
*/
@Override
protected final void visitDateComponent(final int componentIndex, final int startIdx,
final int endIdx)
{
switch (order[componentIndex])
{
case date.YEAR:
visitYear(startIdx, endIdx);
break;
case date.MONTH:
visitMonth(startIdx, endIdx);
break;
case date.DAY:
visitDay(startIdx, endIdx);
break;
default:
}
}
/**
* The first date component separator was parsed: one of '.', '-' or '/'.
* <p>
* Subclasses are expected to overload this method,
* default implementation does nothing.
* <p>
* Note: if date format has no separators, this callback is never called.
*
* @param sep
* the first separator character
*/
@Override
protected void visitSep1(final char sep)
{
sb.append(sep);
}
/**
* The second date component separator was parsed: one of '.', '-' or '/'.
* <p>
* Subclasses are expected to overload this method,
* default implementation does nothing.
* <p>
* Note: if date format has no separators, this callback is never called.
*
* @param sep
* the second separator character
*/
@Override
protected void visitSep2(final char sep)
{
sb.append(sep);
}
/**
* Format day or month component.
*
* @param startIdx
* the component start index in the format string
* @param endIdx
* the component end index in the format string
* @param number
* the value to format
*/
private void formatDayOrMonth(final int startIdx, final int endIdx, final int number)
{
if (deferredValueError)
{
return;
}
final int len = endIdx - startIdx;
if (len == 0 || (len == 1 && number > 9))
{
deferredValueError = true;
return;
}
sb.append(formatField(number, len));
}
/**
* Generate a string representation of the year based on the unusual
* rules of the Progress 4GL.
*
* @param max
* Specifies the maximum number of output characters. If the
* result cannot fit into this number of characters, then
* a <code>null</code> string will be returned. As a special
* case, years within the Y2K window will be truncated to
* 1 (if possible without losing data) or 2 characters if
* max is set to these values respectively. All other values
* must 'naturally' fit inside the maximum.
*
* @return The formatted string or <code>null</code> if the result is
* too large to fit.
*/
private String toYearString(final int max)
{
// make sure the laws of logic hold
assert max >= 1;
final int yy = value.getYear();
// if we have a negative number, we can only pad up to min - 1 chars
// except when force is enabled
final int actual = yy >= 0 ? max : max - 1;
final String year = formatField(yy, actual);
// non-export mode windowing allows truncation
if (value.inY2KWindow() && max < 3)
{
// even truncation doesn't help if we lose data
if ((yy % 100) > 9 && max == 1)
{
return null;
}
// truncate
if (year.length() > max)
{
return year.substring(year.length() - max);
}
return year;
}
// test if we fit
return year.length() > max ? null : year;
}
/**
* Format one date component.
*
* @param number
* the number to format
* @param minimumLen
* the minimum number of digits
*/
private final static String formatField(final int number, final int minimumLen)
{
final NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
nf.setMinimumIntegerDigits(minimumLen);
return nf.format(number);
}
}