Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone help deconstruct this terse Java function into plain English?

Tags:

java

xojo

I'm trying to port the PriorityQueue class from the OpenJDK implementation to another language (Xojo) that doesn't have a similar data structure. 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 cmp and what does the question mark mean on the first line?

like image 466
Garry Pettet Avatar asked Mar 02 '23 01:03

Garry Pettet


2 Answers

Does this mean "assign the array queue to the variable es"

Yes.

and access element 0 and do the following if it's not null?

Yes.

What does the result = (E) expression mean?

At the same time as the two expressions above, it also assigns queue[0] to result. The (E) is a cast to a type. So it's basically just:

result = queue[0]

With some extra stuff thrown in.

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?

Yes.

If so, what does x = (E) before this expression mean? I'm guessing it means to cast the element to type E?

Yes, again just a cast like before.

comparator is a class variable

Just to be pedantic, comparator is likely an instance variable, not a class variable. Check its definition.

Why assign it to a local variable cmp

I suppose to make a local variable copy. I don't see a good reason to do that in the code, so it might be a mistake or something that was left in after some previous code got changed.

and what does the question mark mean on the first line?

The question mark means the type of the Comparator is unknown, and can be anything as long as it's a super class of E. For example, if Integer doesn't have a Comparator but Number does, then that's OK, Number is a super class of Integer and that's good enough.

like image 81
markspace Avatar answered Apr 07 '23 16:04

markspace


1- if ((result = (E) ((es = queue)[0])) != null)
First it assigns the array queue to the variable esand gets element 0 from it the casts it t E generic type and assigns it to result then checks if result is not null.

2- final E x = (E) es[(n = --size)];
First java evaluates --size then assigns to int type n then gets nth from es array, casts it to E, and then assigns it to variable x.


I think the next two lines you asked are clear now!
like image 31
Omid.N Avatar answered Apr 07 '23 18:04

Omid.N