Java class hierarchies with Generics, Comparator, and sort errors

I was looking to see if I could find anything that would help me with my problem, but so far no luck. I have the following class:

  public interface ISort<T> {
      public List<T> sort(List<T> initialList);
  }


  public abstract class Sort<T> implements ISort<T> {
    private Comparator<? super T> comparator;

    public Sort(Comparator<? super T> comparator) {
        this.comparator = comparator;
    }

    @Override
    public List<T> sort(List<T> initialList) {
        ArrayList<T> list = new ArrayList<T>(initialList);
        Collections.sort(list, comparator);

        return list;
    }
  }


public abstract class InternalTreeItem<T> {   
    public abstract String getValue();
}

public class D extends InternalTreeItem<Integer> {
   private Integer i;

   public D(Integer i) {
       this.i = i;
   }

   @Override
   public String getValue() {
       return i.toString();
   }

   public Integer getInteger() {
       return i;
   }
}

public class DComparator implements Comparator<D> {
    @Override
    public int compare(D o1, D o2) {
        return o1.getInteger() - o2.getInteger();
    }
}

public class DSort extends Sort<D> {
    public DSort(Comparator<D> comparator) {
        super(comparator);
    }

    public DSort() {
        super(new DComparator());
    }
}

And the test class:

public class TestClass {
    @Test
    public void test1() {
        List<InternalTreeItem<?>> list= new ArrayList<InternalTreeItem<?>>();

        list.add(new D(1));
        list.add(new D(10));
        list.add(new D(5));

        ISort<?> sorter = new DSort();

        sorter.sort(list);       
    }
}

The compiler gives an error in the line

sorter.sort(list);

and conditions

The method sort(List<capture#2-of ?>)
in the type ISort<capture#2-of ?>
is not applicable for the arguments
 (List<InternalTreeItem<?>>)

Well, after a couple of hours and help from a friend, we realized that the problem is with # collections sort(List<T> list, Comparator<? super T> c)in the abstract Sort class, as I use Comparator<? extends T>.

I use generics, since I have 2 models, one model supercomputer is a general abstract class, subclassed by 35 classes, and the second model actually has 2 different superclasses that are combined, subclassed again by 35 classes. These hierarchies are given; I cannot do anything to change them.

, . , factory, T .

- ( , ).

,

+3
3

- - , .

, D Integer. , , , .

, , :

private interface SortableListItem<T> extends Comparable<SortableListItem<T>> {
    public T getValue();
}

- D:

public class DWrapper implements SortableListItem<Integer> {
    private D item;

    public DWrapper(D item) {
        this.item = item;
    }

    public Integer getValue() {
        return item.getInteger();
    }

    public int compareTo(SortableListItem<Integer> o) {
        return getValue().compareTo(o.getValue());
    }
}

:

    D item1= new D(1);
    D item2= new D(10);
    D item3= new D(5);

    DWrapper wrapper1 = new DWrapper(item1);
    DWrapper wrapper2= new DWrapper(item2);
    DWrapper wrapper3= new DWrapper(item3);

    List<SortableListItem<Integer>> sortableList = new  ArrayList<SortableListItem<Integer>>();
    sortableList.add(wrapper1 );
    sortableList.add(wrapper2);
    sortableList.add(wrapper3);
    Collections.sort(sortableList);

, - - , ( Integer), List.

+2

sorter ISort<?>. , , ISort<String>, . sort List<T>, T String. , List<InternalTreeItem<?>> List<String>, , , .

(. , . I .)

+1

, , , , ,

ISort<?> sorter = new DSort();

>

, wild card - , ( ).

, , DSort ,
DSort :
The type DSort is not generic; it cannot be parameterized with arguments

, (, DSort ..).
, generics ISort.
(1,5,10)

List<InternalTreeItem<?>> list= new ArrayList<InternalTreeItem<?>>();
list.add(new D(1));
list.add(new D(10));
list.add(new D(5));

// no generic arguments
ISort sorter = new DSort();

List<InternalTreeItem<?>> sortedList = sorter.sort(list);

for(InternalTreeItem i:sortedList) {
    System.out.println(i.getValue());
}

code> but results in a form warning. ISort is a raw type. References to the generic ISort type should be parameterized . But code that uses a common one and has a warning about this form is not good practice. This warning implies that the compiler cannot give a cast-iron guarantee about the implicit throws that it uses to use generics.

If possible, I think the best solution would be to see how the module class can be redesigned.

0
source

Source: https://habr.com/ru/post/1729357/


All Articles