How to use StringBuilder class in PeopleCode

Unlike Java, C#.net and other object oriented languages (OOP) PeopleCode API does not deliver built in application class or a StringBuilder class for concatenating many strings together in a loop.
However, recently I found out that PeopleSoft has delivered few utility application classes in their diagnostic plug-in application packages.
The main class we are interesting here is StringBuffer Class which provides basic functionality to work with a large amount of string concatenation operations within PeopleCode.
The StringBuffer class is not as comprehensive as StringBuilder class in Java or C#.net but it is good enough to do the string operations you want.

Here is the class definition (note: the image is an indication only)

class StringBuffer
   method StringBuffer(&InitialValue As string, &MaxSize As integer);
   method Append(&New As string);
   method Reset();
   property string Value get set;
   property integer Length readonly;
private
   instance array of string &Pieces;
   instance integer &MaxLength;
end-class;

method StringBuffer
   /+ &InitialValue as String, +/
   /+ &MaxSize as Integer +/
   &Pieces = CreateArray(&InitialValue);
   &MaxLength = &MaxSize;
   &Length = 0;
end-method;

method Reset
   &Pieces = CreateArrayRept("", 0);
   &Length = 0;
end-method;

method Append
   /+ &New as String +/
   Local integer &TempLength = &Length + Len(&New);
   If &Length > &MaxLength Then
      throw CreateException(210, 44, "Message Not Found", String(&MaxLength));
   End-If;
   &Length = &TempLength;
   &Pieces.Push(&New);
end-method;

get Value
   /+ Returns String +/
   Local string &Temp = &Pieces.Join("", "", "", &Length);
   /* collapse array now */
   &Pieces = CreateArrayRept("", 0);
   &Pieces.Push(&Temp); /* start out with this combo string */
   Return &Temp;
end-get;

set Value
   /+ &NewValue as String +/
   /* Ditch our current value */
   %This.Reset();
   &Pieces.Push(&NewValue);
end-set;

The StringBuffer class is a simple (but useful) class which has

  • two private instances - a string array (&Pieces) and an integer variable (&MaxLength);
  • two properties - a read-write string property (Value) and read-only integer property (Length)
  • 2 methods plus constructor method.

The operation inside the class is self explanatory. The Append method can be used to add strings to the end of a string represented by the current StringBuffer. The following example initializes a StringBuffer to "Hello this is my first string!" and then appends some text to the end of the object. The &MaxSize in the constructor must specify a non-negative integer value
Before you can use this class in your PeopleCode program, you must import it into your program, using an import statement. The application package PT_DIAGNOSTICS contains the StringBuffer class. The import statement should be as follows:

import PT_DIAGNOSTICS:*;
Lets see an example;

try
   Local PT_DIAGNOSTICS:StringBuffer &MyStringBuffer = create PT_DIAGNOSTICS:StringBuffer("Hello this is my first string!", 1);
   &MyStringBuffer.Append(" This is really good.");
catch Exception &c1
   WinMessage(&c1.ToString(), 0);
end-try;

/* to get the value of string use &MyStringBuffer.Value */
WinMessage(&MyStringBuffer.Value, 0);

Even though StringBuffer class provides basic functionality to manipulate strings, it comes in handy if you are dealing with files in PeopleCode for example;
While &file.ReadLine(&line)
   &S.Append(&line);
   &S.Append(&separator);
SHARE

Unknown

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment