|
Letzte
Bearbeitung dieses Dokuments: |
Code
Erklärungen
und Anwendungsbeispiele
Verwandte
Dokumentation
package
js_base.utilities;
import
java.math.*;
/**
*
* @author kurt@javascout.biz
*
@date 2007-05-15
*
* @description
* Class
with static methods to convert data of various types to
strings.
* Also methods to process a string; e.g.
replacing parts of the string.
*
* @change-log
*
when who why
*
--------------------------------------------------------
*
*/public
class
JSBS_Formatter
{
/*
* --------------------
* Method
to expand a string representing a numeric value with decimal places.
* This method can be used if a column of numbers
should be presented aligned
* and the field-formatting
does not provide a 'right-justified' presentation.
* The
parameters parmDecimalPlacesBeforeComma and
DecimalPlacesAfterComma
* are provided for easier
calculation of the final length.
* This method just
provides a padding with spaces at the beginning of the string !
*/
private
static
String
expandToFinalLength(String
parmInput,
int
parmDecimalPlacesBeforeComma,
int
parmDecimalPlacesAfterComma)
{
/* Check
for null-parameter. */
if
(parmInput ==
null
)
return
""
;
/* Define
the internal variables. */
int
intFinalLength
= 0;
String strFormattedString
= parmInput;/* If
places after the comma are defined, add 1 for the decimal character.
*/
if
(parmDecimalPlacesAfterComma
> 0) intFinalLength = parmDecimalPlacesAfterComma + 1;
/* If
the Decimal Places before the comma are given, adde them to the final
length. */
if
(parmDecimalPlacesBeforeComma
> 0) intFinalLength+= parmDecimalPlacesBeforeComma;
/* Add
spaces at the beginning until the final length is reached.
*/
while
(strFormattedString.length()
< intFinalLength) {
strFormattedString
= "
"
+ strFormattedString;
}/* Return
the expanded string */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert a BigDecimal-value to a string.
* The
decimal places after the comma are rounded according to the
* parameter 'parmDecimalPlacesAfterComma'.
*/
public
static
String
toFormattedString(BigDecimal
parmInputValue,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Transfer
the InputValue to a local string so that the parameter stays
unchanged. */
BigDecimal
locInput =
new
BigDecimal(0);
locInput
= locInput.add(parmInputValue);/* Round
according to the decimals after comma passed as input-parameters.
*/
locInput
= locInput.setScale(parmDecimalPlacesAfterComma,
BigDecimal.
ROUND_HALF_UP
);
/* Convert
to string and replace the comma-character. */
String
strFormattedString =
locInput.toString().trim();
strFormattedString
= strFormattedString.replace('.'
,
parmCommaCharacter);
/* Return
the string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert a BigDecimal-value to a string of a certain
length.
* This method is useful if numeric values
should be displayed in a column
* where the strings are
'left-bound' – as in a standard-JTable.
* The
result-string is padded with blanks to reach the number of decimal
places before the comma. */
public
static
String
toFormattedString(BigDecimal
parmInputValue,
int
parmDecimalPlacesBeforeComma,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Let
the above method to the work of building the String. */
String
strFormattedString =
toFormattedString(parmInputValue,
parmCommaCharacter, parmDecimalPlacesAfterComma);/* Expand
to final length. */
strFormattedString
= expandToFinalLength(strFormattedString,
parmDecimalPlacesBeforeComma,
parmDecimalPlacesAfterComma);
/* Return
the expanded string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert an Integer-value to a string. */
public
static
String
toFormattedString(Integer parmInputValue) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Convert
to string. */
String
strFormattedString = parmInputValue.toString().trim();
/* Return
the string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert an Integer-value to a string of a minimum length.
* This
method is useful if numeric values should be displayed in a
column
* where the strings are 'left-bound' – as
in a standard-JTable.
* The result-string is padded
with blanks to reach the number of decimal places before the comma.
*/
public
static
String
toFormattedString(Integer
parmInputValue,
int
parmDecimalPlacesBeforeComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Let
the above method to the work of building the String. */
String
strFormattedString =
toFormattedString(parmInputValue);/* Expand
to final length. */
strFormattedString
= expandToFinalLength(strFormattedString,
parmDecimalPlacesBeforeComma,
0);
/* Return
the expanded string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert an Integer-value to a string with a comma
* and
decimal places after the comma-character.
* This method
is useful if a value should be displayed with a comma and
* several
zeroes for compatibility reasons. */
public
static
String
toFormattedString(Integer
parmInputValue,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue.intValue());
return
toFormattedString(locInput,
parmCommaCharacter, parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert an Integer-value to a string with a comma
* and
decimal places after the comma-character.
* The result
string has a minimum length. */
public
static
String
toFormattedString(Integer
parmInputValue,
int
parmDecimalPlacesBeforeComma,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue.intValue());
return
toFormattedString(locInput,
parmDecimalPlacesBeforeComma,
parmCommaCharacter,
parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert an int-value to a string. */
public
static
String
toFormattedString(
int
parmInputValue)
{
/* Convert
the InputValue to an Integer and let the method for it do the job.
*/
Integer
locInput =
new
Integer(parmInputValue);
String
strFormattedString =
toFormattedString(locInput);/* Return
the string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert an int-value to a string of a minimum length.
* This
method is useful if numeric values should be displayed in a
column
* where the strings are 'left-bound' – as
in a standard-JTable.
* The result-string is padded
with blanks to reach the number of decimal places before the comma.
*/
public
static
String
toFormattedString(
int
parmInputValue,
int
parmDecimalPlacesBeforeComma) {
/* Let
the above method do the work of building the String. */
String
strFormattedString =
toFormattedString(parmInputValue);/* Expand
to final length. */
strFormattedString
= expandToFinalLength(strFormattedString,
parmDecimalPlacesBeforeComma,
0);
/* Return
the expanded string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert an int-value to a string with a comma
* and
decimal places after the comma-character.
* This method
is useful if a value should be displayed with a comma and
* several
zeroes for compatibility reasons. */
public
static
String
toFormattedString(
int
parmInputValue,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue);
return
toFormattedString(locInput,
parmCommaCharacter, parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert an int-value to a string with a comma
* and
decimal places after the comma-character.
* The result
string has a minimum length. */
public
static
String
toFormattedString(
int
parmInputValue,
int
parmDecimalPlacesBeforeComma,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue);
return
toFormattedString(locInput,
parmDecimalPlacesBeforeComma,
parmCommaCharacter,
parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert a Short-value to a string. */
public
static
String
toFormattedString(Short parmInputValue) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Convert
to string. */
String
strFormattedString = parmInputValue.toString().trim();
/* Return
the string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert a Short-value to a string of a minimum length.
* This
method is useful if numeric values should be displayed in a
column
* where the strings are 'left-bound' – as
in a standard-JTable.
* The result-string is padded
with blanks to reach the number of decimal places before the comma.
*/
public
static
String
toFormattedString(Short
parmInputValue,
int
parmDecimalPlacesBeforeComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Let
the above method to the work of building the String. */
String
strFormattedString =
toFormattedString(parmInputValue);/* Expand
to final length. */
strFormattedString
= expandToFinalLength(strFormattedString,
parmDecimalPlacesBeforeComma,
0);
/* Return
the expanded string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert a Short-value to a string with a comma
* and
decimal places after the comma-character.
* This method
is useful if a value should be displayed with a comma and
* several
zeroes for compatibility reasons. */
public
static
String
toFormattedString(Short
parmInputValue,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue.intValue());
return
toFormattedString(locInput,
parmCommaCharacter, parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert a Short-value to a string with a comma
* and
decimal places after the comma-character.
* The result
string has a minimum length. */
public
static
String
toFormattedString(Short
parmInputValue,
int
parmDecimalPlacesBeforeComma,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Check
for null-parameter; return 'null'. */
if
(parmInputValue
==
null
)
return
null
;
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue.intValue());
return
toFormattedString(locInput,
parmDecimalPlacesBeforeComma,
parmCommaCharacter,
parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert a short-value to a string. */
public
static
String
toFormattedString(
short
parmInputValue) {
/* Convert
the InputValue to a Short and let the method for it do the job.
*/
Short
locInput =
new
Short(parmInputValue);
String
strFormattedString =
toFormattedString(locInput);/* Return
the string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert a short-value to a string of a minimum length.
* This
method is useful if numeric values should be displayed in a
column
* where the strings are 'left-bound' – as
in a standard-JTable.
* The result-string is padded
with blanks to reach the number of decimal places before the comma.
*/
public
static
String
toFormattedString(
short
parmInputValue,
int
parmDecimalPlacesBeforeComma) {
/* Let
the above method do the work of building the String. */
String
strFormattedString =
toFormattedString(parmInputValue);/* Expand
to final length. */
strFormattedString
= expandToFinalLength(strFormattedString,
parmDecimalPlacesBeforeComma,
0);
/* Return
the expanded string. */
return
strFormattedString;
}
/*
* --------------------
* Method
to convert a short-value to a string with a comma
* and
decimal places after the comma-character.
* This method
is useful if a value should be displayed with a comma and
* several
zeroes for compatibility reasons. */
public
static
String
toFormattedString(
short
parmInputValue,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue);
return
toFormattedString(locInput,
parmCommaCharacter, parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to convert a short-value to a string with a comma
* and
decimal places after the comma-character.
* The result
string has a minimum length. */
public
static
String
toFormattedString(
short
parmInputValue,
int
parmDecimalPlacesBeforeComma,
char
parmCommaCharacter,
int
parmDecimalPlacesAfterComma) {
/* Convert
the InputValue to a BigDecimal and let the method for it do the job.
*/
BigDecimal
locInput =
new
BigDecimal(parmInputValue);
return
toFormattedString(locInput,
parmDecimalPlacesBeforeComma,
parmCommaCharacter,
parmDecimalPlacesAfterComma);
}
/*
* --------------------
* Method
to replace a substring within a string by another substring.
* This
method was written as a system-defined 'New-Line'-character is
not
* recognized and replaced by the method
'replaceAll' within the String-library. */
public
static
String
replaceAllSubstrings(String
parmStringToProcess,
String
parmStringToBeReplaced,
String
parmReplacement) {/* Check
for 'null'-parameters to avoid a dump. */
if
(parmStringToProcess
==
null
)
return
null
;
if
(parmStringToBeReplaced
==
null
)
return
parmStringToProcess;
if
(parmReplacement
==
null
)
parmReplacement =
""
;
/* Create
the value for the string to be returned. */
String
strToBeReturned =
""
;
/* Substring
to split up the parmStringToProcess and reassemble with the
parmReplacement. */
String
strLeftSide;
String
strRightSide = parmStringToProcess;/* Index
of the substring parmStringToBeReplaces. */
int
intSplitIndex;
/* Loop
to split the 'Right-Side' and reassemble the 'left-Side' with the
parmReplacement. */
do
{
/* See
if there is a substring 'parmStringToBeReplaced' within the
'Right-Side'. */
intSplitIndex
= strRightSide.indexOf(parmStringToBeReplaced);
if
(intSplitIndex
>= 0) {
/* 'parmStringToBeReplaced'
Found; split into 'LeftSide' and new 'RightSide'.
*/
strLeftSide
= strRightSide.substring(0, intSplitIndex);
strRightSide
= strRightSide.substring(intSplitIndex +
parmStringToBeReplaced.length());
/* Add
the 'LeftSide' and 'parmReplacement' to the Return-String.
*/ strToBeReturned
+= strLeftSide + parmReplacement;
}
}
while
(intSplitIndex
>= 0);
/* Add
the remaining part of the 'parmStringToProcess' ('RightSide') to the
Return-String. */
strToBeReturned
+= strRightSide;
/* Return
the new built String. */
return
strToBeReturned;
}
/*
* --------------------
* Method
to 'pack' an Array of Strings into a single Strin.
* This
method gis useful when several strings – where mot of them are
empty -
* should be stored on the database.
* To
save some space those Strings within the array are packed into a
single String. */
public
static
String
packStringArray(String[]
parmStringArray,
String
parmDelimiter) {/* Check
for 'null'-parameters to avoid a dump. */
if
(parmStringArray
==
null
)
return
""
;
if
(parmDelimiter
==
null
)
/* Parameter
with the delimiter-String contains 'null' value;
* Call
the method using the default-value for the delimiter.
*/
return
packStringArray(parmStringArray);
/* Determine
the number of the elements within the array passed as parameter.
*/
int
intArraySize = parmStringArray.
length
;
/* Determine
if at least one element containes a valid value.
* If
all elements are 'null', not delimiter-strings are put into the
'packed' string. */
int
intArrayIndex;
boolean
bolAllNull =
true
;
for
(intArrayIndex = 0; intArrayIndex < intArraySize; intArrayIndex++)
{
/* Check
for each element of the array if it contains a value. */
if
(parmStringArray[intArrayIndex]
!=
null
)
{
/* At
least one element with a value found; terminate further checking.
*/
bolAllNull
=
false
;
break
;
}
}
/* Check
if all elements of the array contained 'null' values;
* if
so, then return an empty string. */
if
(bolAllNull)
return
""
;
/*
* Construct
the String to be returned.
* Then transfer all elements
of the array (which are not 'null') to the new String.
*/ String
strToBeReturned =
""
;
for
(intArrayIndex = 0; intArrayIndex < intArraySize; intArrayIndex++)
{
/* Check
for each element of the array if it contains a value.
* If
it is not 'null' add it to the String with 'packed' information.
*/
if
(parmStringArray[intArrayIndex]
!=
null
)
strToBeReturned
+= parmStringArray[intArrayIndex];
/* If
the processed array-element was not the last one then add the
delimiter-string. */
if
(intArrayIndex
< (intArraySize - 1))
strToBeReturned
+= parmDelimiter;
}
/* Return
the new built String. */
return
strToBeReturned;
}
/*
* --------------------
* Derivation
of the above method.
* The 'delimiter' is not passed;
so '::' is chosen as delimiter. */
public
static
String
packStringArray(String[] parmStringArray) {
/* Call
the method doing the work with '::' as delimiter-string.
*/
return
packStringArray(parmStringArray,
"::"
);
}/*
* --------------------
* Method
to 'unpack' a String to an array of Strings.
* This
method reverses the 'packing' of the method above to re-get
* the
array with the single strings.
* With the parameter
'parmDelimiter' the String that is separating
* the
values of the array is specified. */
public
static
String[]
unpackStringArray(String
parmPackedString,
String
parmDelimiter) {/* Check
for 'null'-parameters to avoid a dump. */
if
(parmPackedString
==
null
)
{
/* String
that should represent the packed array is 'null'.
* Return
an array containing only one empty String. */ String[]
arrayReturnString =
new
String[1];
arrayReturnString[0]
= ""
;
return
arrayReturnString;
}
if
(parmDelimiter
==
null
)
/* Parameter
with the delimiter-String contains 'null' value;
* Call
the method using the default-value for the delimiter.
*/
return
unpackStringArray(parmPackedString);
/* Count
the number of array-elements presented within the string.
*/
int
intElementCount = 1;
/* Strings
that hold the substrings before the delimiter (LeftSide) and
after
* the delimiter (RightSide). */ String
strLeftSide;
String
strRightSide = parmPackedString;/* Index
of the delimiter-string. */
int
intSplitIndex;
/* Loop
to split the packed String and count the number of delimiters.
* As
the number of 'array-elements' is one more is reflected within the
start-value
* of 'intElementCount'. */
do
{
/* See
if there is a substring with the delimiter within the 'Right-Side'.
*/
intSplitIndex
= strRightSide.indexOf(parmDelimiter);
if
(intSplitIndex
>= 0) {
/* Increase
the number of 'array-elements'.
*/
intElementCount++;
/* Split
into 'LeftSide' and new 'RightSide' that is inspected again.
*/ strLeftSide
= strRightSide.substring(0, intSplitIndex);
strRightSide
= strRightSide.substring(intSplitIndex +
parmDelimiter.length());
}
}
while
(intSplitIndex
>= 0);
/* Build
a new array of Strings according to the number of elements.
*/
String[]
arrayToBeReturned =
new
String[intElementCount];
/* Loop
to separate the array-elements from the packed String.
*/ strRightSide
= parmPackedString;
for
(
int
intArrayIndex
= 0; intArrayIndex < intElementCount; intArrayIndex++) {
/* See
if there is a substring with the delimiter within the 'Right-Side'.
*/
intSplitIndex
= strRightSide.indexOf(parmDelimiter);
if
(intSplitIndex
>= 0) {
/* Delimiter-String
found; put theLeftSide into the array with the String-elements
* and
use the RightSide for further inspection. */ strLeftSide
= strRightSide.substring(0,
intSplitIndex);
arrayToBeReturned[intArrayIndex]
= strLeftSide; strRightSide
= strRightSide.substring(intSplitIndex +
parmDelimiter.length());
}
else
{
/* No
more delimiter found; RightSide contains final element of the array.
*/ arrayToBeReturned[intElementCount
- 1] = strRightSide;
}
}
/* Return
the new built array. */
return
arrayToBeReturned;
}
/*
* --------------------
* Derivation
of the above method.
* The 'delimiter' is not passed;
so '::' is chosen as delimiter. */
public
static
String[]
unpackStringArray(String parmPackedString) {
/* Call
the method doing the work with '::' as delimiter-string.
*/
return
unpackStringArray(parmPackedString,
"::"
);
}}
xxx
Dokument |
Inhalt |
Dieser
Leitfaden enthält die notwendigen Tätigkeiten für
die Entwicklung eines StartFrame (auch als Command-Center
bekannt). |