Creating a comparator for two classes that extend super

My apologies for my limited generic knowledge in advance.

I have the following situation:

Grade 1:

public class PostProcess implements Serializable {
    public int          order;
    // Several other variables.

    // Constructor setting vars
    public PostProcess(){

    }

    /* Getters and setters for variables */
}

Grade 2:

public class Application extends PostProcess{
    public int      subOrder;
    // Several other variables.

    // Constructor setting vars
    public Application(){

    }

    /* Getters and setters for variables */

}

Grade 3:

public class FileOperation extends PostProcess{
    public int      subOrder;
    // Several other variables (different from class 'Application').

    // Constructor setting vars
    public FileOperation(){

    }

    /* Getters and setters for variables */

}

What I'm trying to achieve in another class is to sort a list containing a combination of FileOperation and Application objects, defined as:

private ArrayList<? extends PostProcess> processList = new ArrayList<PostProcess>();

This view should be in two fields of both of these objects, namely: "order" and "suborder". "order" is inherited from PostProcess, and "subOrder" is defined in both the "Application" and "FileOperation" classes.

Throughout my reading journey on Generics, Comparable, Comparators, and Interfaces, I think I got it all messed up.

I am trying to apply sorting using:

Collections.sort(processList, new PostProcessComparator());

PostProcessComparator is defined as:

public class PostProcessComparator implements Comparator<? extends PostProcess> {

    @Override
    public int compare(Object p1, Object p2) {

      int mainOrderCompare = p1.getOrder().compareTo(p2.getOrder());
      if (mainOrderCompare != 0) {
        return mainOrderCompare;
      } else {
        return p1.getSubOrder().compareTo(p2.getSubOrder());
      }
    }
}

Questions:

, (, , ) , , . Im , ;)

  • , "processList" . "FileOperation" "Application" List, " add (Application)" ( FileOperation). , generics processList? , PostProcess ?
  • PostProcessComparator , , PostProcess (, , ).
  • Comparator p1 p2 ( :

    @Override
    public int compare(<What to put here?> p1, <And here?> p2) {
    
    }
    

, , , ! - , .

!

NickJ Winzu ArrayList.

  • subOrder FileOperation ( )
  • :

    public class PostProcessComparator<T extends PostProcess> implements Comparator<T> 
    
  • Integer.compare(p1.getOrder(), p2.getOrder()) .

( ) :

    Collections.sort(processList, new PostProcessComparator());

: - [unchecked] unchecked invoke : sort

    required:  List<T>,Comparator<? super T>
    found: ArrayList<PostProcess>,PostProcessComparator

, , .

?

+4
3

, , PostProcessComparator :

public class PostProcessComparator<T extends PostProcess> implements Comparator<T> {

  @Override
  public int compare(T p1, T p2) {

    int mainOrderCompare = p1.getOrder().compareTo(p2.getOrder());
    if (mainOrderCompare != 0) {
      return mainOrderCompare;
    } else {
      return p1.getSubOrder().compareTo(p2.getSubOrder());
    }
  }
}

compare() ( PostProcess), PostProcess comlpare()

, . , , .

+4

NickJ , .

, ,

private ArrayList<? extends PostProcess> processList = new ArrayList<PostProcess>();.

ArrayList<PostProcess> processList = new ArrayList<PostProcess>();

Application FileOperation .

. ,

Java

, SubOrder .

import java.io.Serializable;

public class PostProcess implements Serializable {
private int          order;
private int      subOrder;
// Several other variables.

public int getSubOrder() {
    return subOrder;
}

public void setSubOrder(int subOrder) {
    this.subOrder = subOrder;
}

public int getOrder() {
    return order;
}

public void setOrder(int order) {
    this.order = order;
}

// Constructor setting vars
public PostProcess(){

}

}

, , :

 Collections.sort(processList, new PostProcessComparator<PostProcess>());
+2

:

public class PostProcessComparator implements Comparator<PostProcess> {
    @Override
    public int compare(PostProcess p1, PostProcess p2) {
      //...
    }
}

. , PostProcess ( PostProcess PostProcess).

0

All Articles