Problems using generics and type inference in Java

JohnQ

In this period I am working on a project which requires some kind of abstraction, but I don't know how to solve some problems related to java generics and type inference.

This is a simplified architecture of the source code:

public abstract class BaseDataset
{
    // Some data
    // Some methods
}

public class DerivedDataset1 extends BaseDataset
{
    // Some data
    // Implementations of some BaseDataset methods
    // Some additional methods
}

public class DerivedDataset2 extends BaseDataset
{
    // Some data
    // Implementations of some BaseDataset methods
    // Some additional methods
}

public interface BaseMeasure<T extends BaseDataset>
{
    public float evaluate(T dataset);
}

public class DerivedMeasure1 implements BaseMeasure<DerivedDataset1>
{
    @Override
    public float evaluate(DerivedDataset1 dataset)
    {
        // evaluate some measure using DerivedDataset1 methods
        return the evaluated measure;
    }
}

public class DerivedMeasure2 implements BaseMeasure<DerivedDataset2>
{
    @Override
    public float evaluate(DerivedDataset1 dataset)
    {
        // evaluate some measure using DerivedDataset2 methods
        return the evaluated measure;
    }
}

public class SocialNetworkBuilder
{
    public <T extends BaseDataset> void build(T dataset, BaseMeasure<T> measure)
    {
        float weight = measure.evaluate(dataset);

        // ...
    }
}

My problem is that if, in my main class, I write something like this:

public class Application
{
    public static void main(String [] args)
    {
        BaseDataset dataset = new DerivedDataset1();
        BaseMeasure<? extends BaseDataset> measure = new DerivedMeasure1();
        SocialNetworkBuilder socialNetworkBuilder = new SocialNetworkBuilder();
        socialNetworkBuilder.build(dataset, measure);
    }
}

Eclipse gives me the error: "The method build(T, BaseMeasure) in the type SocialNetworkBuilder is not applicable for the arguments (BaseDataset, BaseMeasure)"

I think the problem is that the "build" method has to be sure that dataset and measure are of the same type T.

If I do something like this:

public class Application
{
    public static void main(String [] args)
    {
        BaseDataset dataset = new DerivedDataset1();
        BaseMeasure<? extends BaseDataset> measure = new DerivedMeasure1();
        SocialNetworkBuilder socialNetworkBuilder = new SocialNetworkBuilder();
        socialNetworkBuilder.build((DerivedDataset1) dataset, (DerivedMeasure1) measure);
    }
}

it works, but I can't solve my problem in this way because I don't know the derived type of my "dataset" and "measure" instances at compile-time; "dataset" and "measure" should be instanced depending on some parameters given at runtime.

I need to instance my "measure" and "dataset" variables at runtime and still be able to call the "build" method on them.

Do you have some ideas to solve this problem?

Thank you.

isnot2bad

Your application will compile when you change it as follows:

public class Application
{
    public static void main(String [] args)
    {
        DerivedDataset1 dataset = new DerivedDataset1();
        BaseMeasure<DerivedDataset1> measure = new DerivedMeasure1();
        SocialNetworkBuilder socialNetworkBuilder = new SocialNetworkBuilder();
        socialNetworkBuilder.build(dataset, measure);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Problems using generics and type inference in Java

From Dev

Java Generics, Type Inference, Inheritance?

From Dev

Java ternary operator influence on generics type inference

From Dev

Java/Scala Bounded Generics and type inference mismatch

From Dev

Java6, Guava, generics, type inference

From Dev

Type inference with Generics in Swift

From Dev

Java Bounded Generics: Type inference bug? (Method invocation, JLS 15.12.2.7)

From Dev

Why does Java generics type inference break in chained method calls?

From Dev

TypeScript generics: argument type inference

From Dev

Generics and type inference for a generic factory

From Dev

Swift Generics Type Inference Extensions

From Dev

Generics and type inference for a generic factory

From Dev

C# 5.0 Generics: param type inference

From Dev

C# Generics type inference for comman

From Dev

C# type inference, generics and interfaces

From Dev

Java type inference with erasure

From Dev

Type inference in java

From Dev

Problems with Java generics and collections

From Dev

Java / Generics / Array problems

From Dev

Using Java Generics without a type - Generic erasure?

From Dev

Kotlin type inference compile error when using Akka java API

From Dev

Restrictions on using Local-Variable Type Inference in java 10

From Dev

Using Nominal Roles for Type Inference

From Dev

Java var and inference type ambiguity

From Dev

Java Type Inference in Static Methods

From Dev

Generic type inference limits in Java

From Dev

Java var and inference type ambiguity

From Dev

Type error using generics under java 8, but not java 7

From Dev

java generics bounds type