public static <T> void deleteAfterAllowedMaximum(@NotNull LinkedListWithSum<T> data, int maxCount, int maxMemory,
                                                   @NotNull Function<? super T, ? extends T> purgedItemFactory) {
    int smallItemSizeLimit = maxMemory / maxCount / 10;

    if (data.size() > maxCount) {
      data.subList(maxCount, data.size()).clear();
    }

    LinkedListWithSum<T>.ListIterator it = data.listIterator(data.size());
    while (data.getSum() > maxMemory && it.hasPrevious() && it.previousIndex() > 0) {
      T purgedItem = it.previous();
      if (it.getValue() > smallItemSizeLimit) {
        it.set(purgedItemFactory.apply(purgedItem));
      }
    }
  }
public static <T> void deleteAfterAllowedMaximum(@NotNull LinkedListWithSum<T> data, int maxCount, int maxMemory,
                                                   @NotNull Function<? super T, ? extends T> purgedItemFactory)