igalshilman commented on a change in pull request #7872: [FLINK-11420][core] Fixed duplicate method of TraversableSerializer URL: https://github.com/apache/flink/pull/7872#discussion_r261715744
########## File path: flink-core/src/test/java/org/apache/flink/testutils/TupleEqualityMatcher.java ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.testutils; + +import org.apache.flink.api.java.tuple.Tuple; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +import java.util.Arrays; + +public class TupleEqualityMatcher extends BaseMatcher<Object> { + + private final Object wanted; + + /** + * This matcher performs similar comparison to {@link org.hamcrest.core.IsEqual}, which resembles + * {@link java.util.Objects#deepEquals(Object, Object)} logic. The only difference here is that {@link Tuple}s + * are treated similarly to arrays. + * + * <p>This means that if we compare two Tuples that contain arrays, those arrays will + * be compared with {@link Arrays#deepEquals(Object[], Object[])} rather than with reference comparison. + * + * @param item expected value + */ + public static TupleEqualityMatcher deeplyEquals(Object item) { + return new TupleEqualityMatcher(item); + } + + private TupleEqualityMatcher(Object wanted) { + this.wanted = wanted; + } + + @Override + public boolean matches(Object item) { + return deepEquals(item, wanted); + } + + @Override + public void describeTo(Description description) { + description.appendValue(wanted); + } + + private static boolean deepEquals(Object o1, Object o2) { Review comment: I think that this logic can be simplified with something like ``` private static Stream<Object> flatten(Object o) { if (o == null) { return Stream.empty(); } if (o.getClass().isArray()) { return Arrays.stream((Object[]) o) .flatMap(e -> flatten(e)); } if (o instanceof Tuple) { Tuple t = (Tuple) o; return IntStream.range(0, t.getArity()) .mapToObj(t::getField) .flatMap(e -> flatten(e)); } return Stream.of(o); } ``` and then ``` private static boolean deepEquals(Object left ,Object right) { Iterator<Object> l = flatten(left).iterator(); Iterator<Object> r = flatten(right).iterator(); ... } ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
