All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.zeno.genericobject.DiffHtmlCollectionLocker Maven / Gradle / Ivy

There is a newer version: 2.22.3
Show newest version
/*
 *
 *  Copyright 2013 Netflix, Inc.
 *
 *     Licensed 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 com.netflix.zeno.genericobject;

import com.netflix.zeno.diff.DiffRecord;
import com.netflix.zeno.diff.DiffSerializationFramework;
import com.netflix.zeno.genericobject.GenericObject.Field;
import com.netflix.zeno.serializer.NFTypeSerializer;
import com.netflix.zeno.serializer.SerializerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * This class will line up two collections so that the most similar items are paired together.

* * This helps to eyeball the differences between two objects with collections in their hierarchies, without * requiring knowledge of the semantics of these objects.

* * The similarity metric used here is the jaccard distance. * * @author dkoszewnik * */ public class DiffHtmlCollectionLocker { private final DiffSerializationFramework diffFramework; public DiffHtmlCollectionLocker(SerializerFactory factory) { diffFramework = new DiffSerializationFramework(factory); } void lockCollectionFields(GenericObject from, GenericObject to) { List lockedFromFields = new ArrayList(); List lockedToFields = new ArrayList(); List fromDiffRecords = createDiffRecordList(from); List toDiffRecords = createDiffRecordList(to); JaccardMatrixPairwiseMatcher matcher = new JaccardMatrixPairwiseMatcher(from.getFields(), fromDiffRecords, to.getFields(), toDiffRecords); while (matcher.nextPair()) { lockedFromFields.add(matcher.getX()); lockedToFields.add(matcher.getY()); } from.setFields(lockedFromFields); to.setFields(lockedToFields); } private List createDiffRecordList(GenericObject from) { List diffRecords = new ArrayList(); for (int i = 0; i < from.getFields().size(); i++) { Field field = from.getFields().get(i); if (field != null && field.getValue() != null) { DiffRecord rec = getDiffRecord(field); diffRecords.add(rec); } else { diffRecords.add(new DiffRecord()); } } return diffRecords; } private DiffRecord getDiffRecord(Field field) { DiffRecord rec = new DiffRecord(); if (field.getValue() instanceof GenericObject) { GenericObject fieldValue = (GenericObject) field.getValue(); rec.setTopLevelSerializerName(fieldValue.getObjectType()); ((NFTypeSerializer) diffFramework.getSerializer(fieldValue.getObjectType())).serialize(fieldValue.getActualObject(), rec); } else { rec.setTopLevelSerializerName("primitive"); rec.serializePrimitive("value", field.getValue()); } return rec; } }