@@ -18,22 +18,48 @@ public interface CommandResult {
1818 * @return The combined result
1919 */
2020 default Object combine (Object other ) {
21- ListCommandResult list = new ListCommandResult ();
22- list .combine (this );
23- list .combine (other );
24- return list ;
21+ return ListCommandResult .from (this , other );
2522 }
2623
24+ /**
25+ * <p>Combine any objects, even if they are not Command Results.
26+ * Target is the object which will be mutated to contain
27+ * the combined target and source.
28+ *
29+ * <p>If either of the supplied objects are EmptyCommandResult, the
30+ * non-empty result will be returned.
31+ *
32+ * <p>If the target implements CommandResult, the `combine` method
33+ * will be called on the target: `target.combine(source)`
34+ *
35+ * <p>If the target and source are the same type of boxed primitive
36+ * number (ie Integer, Long, Double, etc.), they will be added together.
37+ *
38+ * <p>Otherwise, a new ListCommandResult containing both target and source
39+ * will be returned.
40+ *
41+ * @param target the object which the source will be combined into
42+ * @param source the object to combine into the target
43+ */
2744 static Object combine (Object target , Object source ) {
2845 if (target instanceof CommandResult ) {
2946 return ((CommandResult )target ).combine (source );
30- } else if (target instanceof Integer && source instanceof Integer ) {
31- // Backwards compatability
32- return (Integer )target + (Integer )source ;
3347 } else if (source instanceof EmptyCommandResult ) {
3448 return target ;
49+ } else if (target instanceof Byte && source instanceof Byte ) {
50+ return (byte ) target + (byte ) source ;
51+ } else if (target instanceof Short && source instanceof Short ) {
52+ return (short ) target + (short ) source ;
53+ } else if (target instanceof Integer && source instanceof Integer ) {
54+ return (int ) target + (int ) source ;
55+ } else if (target instanceof Long && source instanceof Long ) {
56+ return (long ) source + (long ) source ;
57+ } else if (source instanceof Float && source instanceof Float ) {
58+ return (float ) source + (float ) source ;
59+ } else if (source instanceof Double && target instanceof Double ) {
60+ return (double ) source + (double ) target ;
3561 } else {
36- return source ;
62+ return ListCommandResult . from ( target , source ) ;
3763 }
3864 }
3965}
0 commit comments