从Java代码转换时出现Kotlin错误

时间:2017-10-21 19:24:57

标签: java kotlin

我有一个在Java中看起来像这样的ArraysHelper类:

public class ArraysHelper<T> {

  public final Iterable<T> iterable;

  private ArraysHelper(Iterable<T> iterable) {
    this.iterable = iterable;
  }

  public static <T> T[] concat(T[] array, @Nullable T element) {
    T[] result = Arrays.copyOf(array, array.length + 1);
    result[array.length] = element;
    return result;
  }

  public final ArraysHelper<T> filter(Prediction<T> prediction) {
    return from(filter(iterable, prediction));
  }

  private static <T> List<T> filter(Iterable<T> iterable, Prediction<T> predicate) {
    ArrayList<T> list = new ArrayList<>();
    for (T object : iterable) {
        if (predicate.apply(object)) {
            list.add(object);
        }
    }

    return list;
  }

  public final List<T> toList() {
    List<T> list = new ArrayList<>();
    for (T item : iterable) {
        list.add(item);
    }
    return list;
  }

  public static <T> ArraysHelper<T> from(final Iterable<T> iterable) {
    return new ArraysHelper<>(iterable);
  }
}

我把它转换成Kotlin:

class ArraysHelper<T> private constructor(val iterable: Iterable<T>) {

  fun filter(prediction: Prediction<T>): ArraysHelper<T> {
    return from(filter(iterable, prediction))
  }

  fun toList(): ArrayList<T> {
    val list = ArrayList<T>()
    for (item in iterable) {
        list.add(item)
    }
    return list
  }

  companion object {

    fun <T> concat(array: Array<T>, element: T?): Array<T> {
        val result = Arrays.copyOf(array, array.size + 1)
        result[array.size] = element
        return result
    }

    private fun <T> filter(iterable: Iterable<T>, predicate: Prediction<T>): ArrayList<T> {
        val list = ArrayList<T>()
        for (`object` in iterable) {
            if (predicate.apply(`object`)) {
                list.add(`object`)
            }
        }

        return list
    }


    fun <T> from(iterable: Iterable<T>): ArraysHelper<T> {
        return ArraysHelper(iterable)
    }
  }
}

我也转换了一个我正在使用的界面。在java中:

public interface Prediction<T> {

  boolean apply(@Nullable T input);

  @Override
  boolean equals(@Nullable Object object);
}

在Kotlin:

interface Prediction<T> {

  fun apply(input: T?): Boolean

  override fun equals(`object`: Any?): Boolean
}

现在到了这一点 - 我想在这里使用它们:

 val rssiEvents = locationRSSIEvents
    if (rssiEvents.isNotEmpty()) {
        val latestRSSIEvent = rssiEvents.last()

        val cleanedGeoEvents = ArraysHelper.from<LocationGeoEvent>(locationGeoEvents).filter(object : Prediction<LocationGeoEvent> {
            override fun apply(input: LocationGeoEvent): Boolean {
                return Math.abs(input.timestamp - latestRSSIEvent.timestamp) < maxTimeDifferenceSeconds * 1000
            }
        }).toList()

    }

但是在IDE(Android Studio 3.0)中强调了过滤方法:

object : Prediction<LocationGeoEvent>

它说:

Required: Prediction<LocationGeoEvent> Found: 

那么exatcly有什么问题呢?

更新

当然,在Kotlin,我可以做这样的事情:

    val cleanedGeoEvents = locationGeoEvents
                .filter { locationGeoEvent -> Math.abs(locationGeoEvent.timestamp - latestRSSIEvent.timestamp) < maxTimeDifferenceSeconds * 1000 }
                .toList()

但是,对于此前版本

的确切问题仍然有用

0 个答案:

没有答案