Unpacking some Java code to Xojo. A little lost

I really need some help porting a Java method to Xojo guys. I’m trying to port the PriorityQueue class from the OpenJDK implementation. I’m really struggling to break down the following method into pseudocode so I can translate it to Xojo:

public E poll() {
  final Object[] es;
  final E result;

  if ((result = (E) ((es = queue)[0])) != null) {
    modCount++;
    final int n;
    final E x = (E) es[(n = --size)];
    es[n] = null;
    if (n > 0) {
      final Comparator<? super E> cmp;
      if ((cmp = comparator) == null)
        siftDownComparable(0, x, es, n);
      else
        siftDownUsingComparator(0, x, es, n, cmp);
      }
    }
    return result;
}

The variable queue is an Object[] array defined on the class.

There are a couple of lines that are confusing me. Firstly:

if ((result = (E) ((es = queue)[0])) != null)

Does this mean “assign the array queue to the variable es and access element 0 and do the following if it’s not null?” What does the result = (E) expression mean? I know E is a generic type.

What is the order of operation of final E x = (E) es[(n = --size)];? Does this mean decrement size, assign that value to n and then access this index within the es array? If so, what does x = (E) before this expression mean? I’m guessing it means to cast the element to type E?

Lastly, these lines:

final Comparator<? super E> cmp;
if ((cmp = comparator) == null)

comparator is a class variable (holding a Comparator). Why assign it to a local variable cap and what does the question mark mean on the first line?

Can anyone help? I know we’ve got some B4X guys here (@Alwaysbusy)?

Hello,
a Java greenhorn here :wink: Let me try it:

  • The (E) in front of some argument/variable means that you are casting the result/variable to the type of E.

  • You are creating a „Comparator“ with name „cmp“. The expression between the <? super E> brackets tells what kind of objects you want to compare (in this case objects of type E or its supertypes).

2 Likes

Xojo has no way to do generics so this is tricky :slight_smile:

E is “some type” and the type that will be used in the queue
Why folks use single letters is such a pain but …

Interface QueueElementType
End Interface

public function poll() as QueueElementType 

  dim es() as QueueElementType
  dim result as QueueElementType

   sq = queue
   result = es(0) 
   if (result <> nil) then
    modCount++;
    dim n as integer = size - 1
    
    dim x as QueueElementType = es(n)
    es(n) = nil
    if (n > 0) then
      final Comparator<? super E> cmp; // <<<<<< not sure here !!!!!!
            // but this looks like a delegate to do comparisons 
            // I just dont know the signature ?
    
      // if the delegate is nil we use one comparison
      // else we use the other
      if ((cmp = comparator) == null)
        siftDownComparable(0, x, es, n);
      else
        siftDownUsingComparator(0, x, es, n, cmp);
      end if
    end if
    return result
end function
1 Like

Correct @cm_rb

Could be expanded to:

es = queue ' put queue in a temporary var
result = (E) es[0] ' get the first element and cast it to an E type
if (result != null) {

}

and

cmd = compartor
if (cmd == null) {

}

Correct

The ? is a wildcard and it should be something of the type E or one of its parents.

e.g.
Green Appel extends Appel extends Fruits extends Food

<? super Appel> will accept Appel, Fruits and Food, but not Green Appel. If it would've been <? extends Appel> then it only accepts Appel and Green Appel

Thanks for the help guys. Once I’ve got the class working I’ll make it public as it turns out PriorityQueues are quite helpful data structures.

Man I wish Xojo supported generics. This code I’m porting with is full of them. Variants are not an equivalent. I’m finding that I can get part way there using class interfaces (without any methods) but it’s a hack.