How to pass different object type for same method instead of writing two methods with same functionality and return type in java

huangapple go评论69阅读模式
英文:

How to pass different object type for same method instead of writing two methods with same functionality and return type in java

问题

以下是我想要使其动态的方法,它应该接受类类型作为参数,然后我可以在我的断言中使用它。我尝试过使用 instanceof,但没有成功。

public static <T> String setStatus(List<T> s, Predicate<T> ifAllCompleted, Predicate<T> ifAllNotStarted) {
    String finalStatus = "";

    if (s.stream().allMatch(ifAllCompleted)) {
        finalStatus = "Complete";
    } else if (s.stream().allMatch(ifAllNotStarted)) {
        finalStatus = "Not Started";
    } else {
        finalStatus = "In-progress";
    }
    return finalStatus;
}

使用示例:

Predicate<SmeObject> smeAllCompleted = x -> x.getStatus().equals("Completed");
Predicate<SmeObject> smeAllNotStarted = x -> x.getStatus().equals("Not Started");
String smeStatus = setStatus(smeList, smeAllCompleted, smeAllNotStarted);

Predicate<ControllerObject> controllerAllCompleted = x -> x.getStatus().equals("Completed");
Predicate<ControllerObject> controllerAllNotStarted = x -> x.getStatus().equals("Not Started");
String controllerStatus = setStatus(controllerList, controllerAllCompleted, controllerAllNotStarted);
英文:

here is my methods i want to make it dynamic which should accept class type as parameter and then i can use that in my predicate i have tried using instanceof but didnt work

public static String setStatus(List&lt;SmeObject&gt; s) {
	String finalStatus = &quot;&quot;;
	Predicate&lt;SmeObject&gt; ifAllCompleted = x.getStatus().equals(&quot;Completed&quot;);
	Predicate&lt;SmeObject&gt; ifAllNotStarted = x.getStatus().equals(&quot;Not Started&quot;);

	if (s.stream().allMatch(ifAllCompleted)) {
		finalStatus = &quot;Complete&quot;;
	} else if (s.stream().allMatch(ifAllNotStarted)) {
		finalStatus = &quot;Not Started&quot;;
	} else {
		finalStatus = &quot;In-progress&quot;;
	}
	return finalStatus;
}

public static String setStatus(List&lt;ControllerObject&gt; s) {
	String finalStatus = &quot;&quot;;
	Predicate&lt;ControllerObject&gt; ifAllCompleted = x.getStatus().equals(&quot;Completed&quot;);
	Predicate&lt;ControllerObject&gt; ifAllNotStarted = x.getStatus().equals(&quot;Not Started&quot;);

	if (s.stream().allMatch(ifAllCompleted)) {
		finalStatus = &quot;Complete&quot;;
	} else if (s.stream().allMatch(ifAllNotStarted)) {
		finalStatus = &quot;Not Started&quot;;
	} else {
		finalStatus = &quot;In-progress&quot;;
	}
	return finalStatus;
}

答案1

得分: 0

如果 ControllerObjectSmeObject 共享一个公共父类,该父类公开了 getStatus() 方法,那么您可能希望使您的方法接受一个带有 List&lt;? extends CommonObjectSuperType&gt; 的列表:

public static String setStatus(List&lt;? extends CommonObjectSuperType&gt; s) {
    String finalStatus = &quot;&quot;;
    Predicate&lt;CommonObjectSuperType&gt; ifAllCompleted = x.getStatus().equals(&quot;Completed&quot;);
    Predicate&lt;CommonObjectSuperType&gt; ifAllNotStarted = x.getStatus().equals(&quot;Not Started&quot;);

    ...
}

然而,如果 ControllerObjectSmeObject 没有公共父类来公开 getStatus() 方法,并且您不能更改它们,那么您只能通过使方法泛型化来让调用者传递一个谓词:

public static &lt;T&gt; String setStatus(List&lt;T&gt; s, Predicate&lt;T&gt; allCompletedTest,
        Predicate&lt;T&gt; allNotStartedTest) {
    // 在方法中使用传入的谓词,而不是在方法中创建它们
    ...
}

然后调用者将在实际类型的上下文中创建谓词。

英文:

If ControllerObject and SmeObject share a common parent that exposes the getStatus() method, then you may want to make your method take a list with a List&lt;? extends CommonObjectSuperType&gt;:

public static String setStatus(List&lt;? extends CommonObjectSuperType&gt; s) {
    String finalStatus = &quot;&quot;;
    Predicate&lt;CommonObjectSuperType&gt; ifAllCompleted = x.getStatus().equals(&quot;Completed&quot;);
    Predicate&lt;CommonObjectSuperType&gt; ifAllNotStarted = x.getStatus().equals(&quot;Not Started&quot;);

    ...
}

If, however, ControllerObject and SmeObject do not have a common parent that exposes getStatus() and you can't change them, then you're left with the option of making the caller send in a predicate by making the method generic:

public static &lt;T&gt; String setStatus(List&lt;T&gt; s, Predicate&lt;T&gt; allCompletedTest,
        Predicate&lt;T&gt; allNotStartedTest) {
    //use incoming predicates instead of creating them in the method
    ...
}

Then the caller will create the predicates in the context of the actual type.

huangapple
  • 本文由 发表于 2020年8月21日 16:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定