001/*
002 * Copyright 2017-2019 Ping Identity Corporation
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018
019package com.unboundid.scim2.server.utils;
020
021import com.fasterxml.jackson.databind.JsonNode;
022import com.unboundid.scim2.common.Path;
023import com.unboundid.scim2.common.utils.JsonDiff;
024import com.unboundid.scim2.common.utils.JsonUtils;
025
026/**
027 * This class can be used to calculate the diffs between two SCIM
028 * resources for the purpose of building a set of patch operations.
029 * The comparison takes into account the SCIM schema of the resources
030 * to be compared.
031 */
032public class ResourceDiff extends JsonDiff {
033
034  private ResourceTypeDefinition resourceTypeDefinition;
035
036  /**
037   * Construct a ResourceDiff instance.
038   * @param resourceTypeDefinition the ResourceTypeDefinition of the
039   *                               resources to be compared.
040   */
041  public ResourceDiff(final ResourceTypeDefinition resourceTypeDefinition)
042  {
043    super();
044    this.resourceTypeDefinition = resourceTypeDefinition;
045  }
046
047  /**
048   * {@inheritDoc}
049   */
050  @Override
051  protected int compareTo(
052      final Path path,
053      final JsonNode sourceNode,
054      final JsonNode targetNode)
055  {
056    return JsonUtils.compareTo(sourceNode, targetNode,
057        resourceTypeDefinition.getAttributeDefinition(path));
058  }
059}