org.apache.thrift.partial.PartialThriftComparer Maven / Gradle / Ivy
Show all versions of libthrift Show documentation
/*
* 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.thrift.partial;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.thrift.TBase;
import org.apache.thrift.protocol.TType;
/**
* Enables comparison of two TBase instances such that the comparison is limited to the subset of
* fields defined by the supplied metadata.
*
* This comparer is useful when comparing two instances where: -- one is generated by full
* deserialization. -- the other is generated by partial deserialization.
*
*
The typical use case is to establish correctness of partial deserialization.
*/
public class PartialThriftComparer {
private enum ComparisonResult {
UNKNOWN,
EQUAL,
NOT_EQUAL
}
// Metadata that defines the scope of comparison.
private ThriftMetadata.ThriftStruct metadata;
/**
* Constructs an instance of {@link PartialThriftComparer}.
*
* @param metadata defines the scope of comparison.
*/
public PartialThriftComparer(ThriftMetadata.ThriftStruct metadata) {
this.metadata = metadata;
}
/**
* Compares thrift objects {@code t1} and {@code t2} and returns true if they are equal false
* otherwise. The comparison is limited to the scope defined by {@code metadata}.
*
* If the objects are not equal then it optionally records their differences if {@code sb} is
* supplied.
*
*
*
* @param t1 the first object.
* @param t2 the second object.
* @param sb if non-null, results of the comparison are returned in it.
* @return true if objects are equivalent, false otherwise.
*/
public boolean areEqual(T t1, T t2, StringBuilder sb) {
return this.areEqual(this.metadata, t1, t2, sb);
}
private boolean areEqual(
ThriftMetadata.ThriftObject data, Object o1, Object o2, StringBuilder sb) {
byte fieldType = data.data.valueMetaData.type;
switch (fieldType) {
case TType.STRUCT:
return this.areEqual((ThriftMetadata.ThriftStruct) data, o1, o2, sb);
case TType.LIST:
return this.areEqual((ThriftMetadata.ThriftList) data, o1, o2, sb);
case TType.MAP:
return this.areEqual((ThriftMetadata.ThriftMap) data, o1, o2, sb);
case TType.SET:
return this.areEqual((ThriftMetadata.ThriftSet) data, o1, o2, sb);
case TType.ENUM:
return this.areEqual((ThriftMetadata.ThriftEnum) data, o1, o2, sb);
case TType.BOOL:
case TType.BYTE:
case TType.I16:
case TType.I32:
case TType.I64:
case TType.DOUBLE:
case TType.STRING:
return this.areEqual((ThriftMetadata.ThriftPrimitive) data, o1, o2, sb);
default:
throw unsupportedFieldTypeException(fieldType);
}
}
private boolean areEqual(
ThriftMetadata.ThriftStruct data, Object o1, Object o2, StringBuilder sb) {
ComparisonResult result = checkNullEquality(data, o1, o2, sb);
if (result != ComparisonResult.UNKNOWN) {
return result == ComparisonResult.EQUAL;
}
TBase t1 = (TBase) o1;
TBase t2 = (TBase) o2;
if (data.fields.size() == 0) {
if (t1.equals(t2)) {
return true;
} else {
appendNotEqual(data, sb, t1, t2, "struct1", "struct2");
return false;
}
} else {
boolean overallResult = true;
for (Object o : data.fields.values()) {
ThriftMetadata.ThriftObject field = (ThriftMetadata.ThriftObject) o;
Object f1 = t1.getFieldValue(field.fieldId);
Object f2 = t2.getFieldValue(field.fieldId);
overallResult = overallResult && this.areEqual(field, f1, f2, sb);
}
return overallResult;
}
}
private boolean areEqual(
ThriftMetadata.ThriftPrimitive data, Object o1, Object o2, StringBuilder sb) {
ComparisonResult result = checkNullEquality(data, o1, o2, sb);
if (result != ComparisonResult.UNKNOWN) {
return result == ComparisonResult.EQUAL;
}
if (data.isBinary()) {
if (areBinaryFieldsEqual(o1, o2)) {
return true;
}
} else if (o1.equals(o2)) {
return true;
}
appendNotEqual(data, sb, o1, o2, "o1", "o2");
return false;
}
private boolean areEqual(ThriftMetadata.ThriftEnum data, Object o1, Object o2, StringBuilder sb) {
ComparisonResult result = checkNullEquality(data, o1, o2, sb);
if (result != ComparisonResult.UNKNOWN) {
return result == ComparisonResult.EQUAL;
}
if (o1.equals(o2)) {
return true;
}
appendNotEqual(data, sb, o1, o2, "o1", "o2");
return false;
}
private boolean areEqual(ThriftMetadata.ThriftList data, Object o1, Object o2, StringBuilder sb) {
List