Чтобы скомпилировать мою программу, мне нужно знать тип возвращаемого значения метода find. Я не знаю, как получить тип Value с помощью следующего интерфейса IBinarySearch и класса Unit:
Итак, это интерфейс:
package u8a1;
import java.util.List;
/**
* Binary search on things with comparable keys.
*
* @param <Key>
* The type of the key of the things. Must implement the {@link Comparable} interface.
* @param <Value>
* The type of the things themselves
*/
public interface IBinarySearch<Key extends Comparable<Key>, Value> {
/**
* Performs a binary search on the vector of {@link Unit}s.
*
* @param needle
* We are looking for something whose key equals this needle with
* respect to the {@link Comparable} interface.
* @param haystack
* The vector of {@link Unit}s where we are searching for the
* needle. This vector must be sorted ascending with respect to
* the keys and the {@link Comparable} interface.
* @return A thing from the haystack whose key equals the needle or null
* if no such thing is in the haystack.
*/
public Value find(List<Unit<Key, Value>> haystack, Key needle);
}
А это класс:
package u8a1;
/**
* A key-value pair
*
* @param <Key>
* the type of the key. Must be {@link Comparable}
* @param <Value>
* the type of the value
*/
public class Unit<Key extends Comparable<Key>, Value> {
public Key key;
public Value value;
public Unit(Key key, Value value) {
this.key = key;
this.value = value;
}
}
Есть еще один интерфейс IMeasure:
package u8a1;
/**
* Support statistics of binary search algorithms
*/
public interface IMeasure {
/**
* Set the factor for the binary search.
*
* A factor of two means that the search space is split into half-half in each recursion step.
* A factor of three means that the search space is split into one third and two thirds in each recursion step.
* In each case integer division is assumed, which means fractions are rounded down.
*
* This method is called first after instantiation.
*
* @param factor
* an integer value
*/
public void setFactor(int factor);
/**
* Retrieve the statistics.
*
* @return the number of recursive calls of the binary search algorithm
* performed since instantiation.
*/
public int getNumberofCalls();
}
А вот класс BinarySearch, который реализует оба интерфейса:
package u8a1;
import java.util.List;
public class BinarySearch<Key extends Comparable<Key>, Value> implements IBinarySearch<Key, Value>, IMeasure {
public Value find(List<Unit<Key, Value>> haystack, Key needle)
{
return haystack;
}
public void setFactor(int factor)
{
return;
}
public int getNumberofCalls()
{
return 0;
}
}
Я хочу, чтобы программа была скомпилирована с правильным типом перед началом реализации методов.