Mon 16 Oct 2006
Java Vector in ILE RPG
Posted by datacrush under Techs
Here’s something that’s totally not recommended from performance perspective but nevertheless cool to explore: Use Java Vector function in RPG.
There are obvious disadvantages to this idea, but say we’re building a long running system that doesn’t care too much about performance. Wouldn’t a Vector work great in RPG just so that the average programmer doesn’t have to mess with pointers? No fixed size arrays and no risky clumsy pointers.
The first thing you’ve got to do is to declare the necessary procedures, referencing the Vector’s constructor and methods.
D NewVector... D Pr O ExtProc(*Java:'java.util.Vector': D *Constructor) D Class(*Java:'java.util.Vector') D AddElement... D Pr O ExtProc(*Java:'java.util.Vector': D 'addElement') D O Class(*Java:'java.lang.Object') D ElementAt... D Pr O ExtProc(*Java:'java.util.Vector': D 'elementAt') D O Class(*Java:'java.lang.Object') D 10I 0 Value D Clear... D Pr O ExtProc(*Java:'java.util.Vector': D 'clear')
Notice that we’re referring to each and every method from procedure perspective. The first parameter of a method procedure is the instance of a Vector created by calling constructor procedure.
Next, declare a runtime variable referencing a Vector instance.
D sVector... D S O Class(*Java:'java.util.Vector')
You would have to declare a new variable for every instance of a Vector that you wish to keep, but very soon you’d be able to appreciate Vector’s effectiveness at dealing with unknown array sizes.
In this example, let’s assume that the variable sVector would store a Java String type. The String is declarable by the same way of Vector. Let’s refer to this String variable as sString. We can now put sVector to work with sString.
/free …sVector = NewVector(); …sString = NewString(’this is a test’); …AddElement(sVector:sString); …sString = ElementAt(sVector:0); …Clear(sVector); /end-free
Of course, the code snippet here is just a mere example of how Java Vector can be integrated with ILE RPG. There are many other smart things you can do with it.
I like it because I don’t need to have a predefined array size. And I do have this running in production somewhere. And while I can replicate the same function with better performance using pointers, this is just so cool! With some effort, one could write a service program to make this feature available to the average code monkeys.
For the benefit of RPG-only programmers, a Vector is like an array except that is doesn’t have a fixed size. It grows and shrinks as you would want them to be.
Last words: If it works for you, great! If you don’t know what you’re doing, then go learn some Java.