Skip to content

Commit

Permalink
[Java API] Fix equality checks on PojoTypeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanEwen committed Dec 16, 2014
1 parent 4820eba commit fb96f12
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ public Class<T> getTypeClass() {
public boolean isKeyType() {
return Comparable.class.isAssignableFrom(typeClass);
}


@Override
public String toString() {
List<String> fieldStrings = new ArrayList<String>();
for (PojoField field : fields) {
fieldStrings.add(field.field.getName() + ": " + field.type.toString());
}
return "PojoType<" + typeClass.getCanonicalName()
+ ", fields = [" + Joiner.on(", ").join(fieldStrings) + "]"
+ ">";
}

@Override
public void getKey(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {
Expand Down Expand Up @@ -238,4 +226,26 @@ public TypeSerializer<T> createSerializer() {
return new PojoSerializer<T>(this.typeClass, fieldSerializers, reflectiveFields);
}

// --------------------------------------------------------------------------------------------

@Override
public boolean equals(Object obj) {
return (obj instanceof PojoTypeInfo) && ((PojoTypeInfo<?>) obj).typeClass == this.typeClass;
}

@Override
public int hashCode() {
return typeClass.hashCode() + 1387562934;
}

@Override
public String toString() {
List<String> fieldStrings = new ArrayList<String>();
for (PojoField field : fields) {
fieldStrings.add(field.field.getName() + ": " + field.type.toString());
}
return "PojoType<" + typeClass.getCanonicalName()
+ ", fields = [" + Joiner.on(", ").join(fieldStrings) + "]"
+ ">";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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
*
* https://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.api.java.typeutils;

import static org.junit.Assert.*;

import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.junit.Test;

public class PojoTypeInfoTest {

@Test
public void testEquals() {
try {
TypeInformation<TestPojo> info1 = TypeExtractor.getForClass(TestPojo.class);
TypeInformation<TestPojo> info2 = TypeExtractor.getForClass(TestPojo.class);

assertTrue(info1 instanceof PojoTypeInfo);
assertTrue(info2 instanceof PojoTypeInfo);

assertTrue(info1.equals(info2));
assertTrue(info1.hashCode() == info2.hashCode());
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}

public static final class TestPojo {

public int someInt;

private String aString;

public Double[] doubleArray;


public void setaString(String aString) {
this.aString = aString;
}

public String getaString() {
return aString;
}
}
}

0 comments on commit fb96f12

Please sign in to comment.