001/*
002 * Copyright 2017-2024 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.annotations.NotNull;
024import com.unboundid.scim2.common.utils.JsonDiff;
025import com.unboundid.scim2.common.utils.JsonUtils;
026
027/**
028 * This class can be used to calculate the diffs between two SCIM
029 * resources for the purpose of building a set of patch operations.
030 * The comparison takes into account the SCIM schema of the resources
031 * to be compared.
032 */
033public class ResourceDiff extends JsonDiff
034{
035  @NotNull
036  private ResourceTypeDefinition resourceTypeDefinition;
037
038  /**
039   * Construct a ResourceDiff instance.
040   *
041   * @param resourceTypeDefinition the ResourceTypeDefinition of the
042   *                               resources to be compared.
043   */
044  public ResourceDiff(
045      @NotNull final ResourceTypeDefinition resourceTypeDefinition)
046  {
047    super();
048    this.resourceTypeDefinition = resourceTypeDefinition;
049  }
050
051  /**
052   * {@inheritDoc}
053   */
054  @Override
055  protected int compareTo(@NotNull final Path path,
056                          @NotNull final JsonNode sourceNode,
057                          @NotNull final JsonNode targetNode)
058  {
059    return JsonUtils.compareTo(sourceNode, targetNode,
060        resourceTypeDefinition.getAttributeDefinition(path));
061  }
062}