简体   繁体   中英

What's the equivalent Java implementation of this C# generic method pattern

I am re writing a library originally written in C# into Java and am trying my best to follow it as closely as possible. This, however, has me stumped.

This is the C# method I want to recreate in Java

public T With<TV>(Func<T, IEditable> func, TV value)
        {
            var pageElement = func(TypedThis);
            pageActions.Add(new WebDriverValuePageAction<TV>(pageElement, value));
            return TypedThis;
        }

This is a generic method which is used as part of a Selenium Page Object framework where T is a BasePage and the Method takes any page element inheriting the IEditable interface.

What is stumping me is the With<TV> part of the method and how to recreate this in Java. Everything else is pretty much done. I have been able to recreate T but cannot work out how to also pass in TV (essentially a generic value) along with the function.

Direct equivalent will be

    public <TV> T With(Function<T, IEditable> func, TV value)
    {
        IEditable pageElement = func.apply(TypedThis);
        pageActions.Add(new WebDriverValuePageAction<TV>(pageElement, value));
        return TypedThis;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM