Diff.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.builder;

  18. import java.lang.reflect.Type;
  19. import java.util.Objects;

  20. import org.apache.commons.lang3.ObjectUtils;
  21. import org.apache.commons.lang3.reflect.TypeUtils;
  22. import org.apache.commons.lang3.tuple.Pair;

  23. /**
  24.  * A {@link Diff} contains the differences between two {@link Diffable} class
  25.  * fields.
  26.  *
  27.  * <p>
  28.  * Typically, {@link Diff}s are retrieved by using a {@link DiffBuilder} to
  29.  * produce a {@link DiffResult}, containing the differences between two objects.
  30.  * </p>
  31.  *
  32.  * @param <T>
  33.  *            The type of object contained within this {@link Diff}. Differences
  34.  *            between primitive objects are stored as their Object wrapper
  35.  *            equivalent.
  36.  * @since 3.3
  37.  */
  38. public abstract class Diff<T> extends Pair<T, T> {

  39.     private static final long serialVersionUID = 1L;

  40.     /** The field type. */
  41.     private final Type type;

  42.     /** The field name. */
  43.     private final String fieldName;

  44.     /**
  45.      * Constructs a new {@link Diff} for the given field name.
  46.      *
  47.      * @param fieldName
  48.      *            the field name
  49.      */
  50.     protected Diff(final String fieldName) {
  51.         this.fieldName = Objects.requireNonNull(fieldName);
  52.         this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class);
  53.     }

  54.     Diff(final String fieldName, final Type type) {
  55.         this.fieldName = Objects.requireNonNull(fieldName);
  56.         this.type = Objects.requireNonNull(type);
  57.     }

  58.     /**
  59.      * Gets the name of the field.
  60.      *
  61.      * @return the field name
  62.      */
  63.     public final String getFieldName() {
  64.         return fieldName;
  65.     }

  66.     /**
  67.      * Gets the type of the field.
  68.      *
  69.      * @return the field type
  70.      * @deprecated Unused, will be removed in 4.0.0.
  71.      */
  72.     @Deprecated
  73.     public final Type getType() {
  74.         return type;
  75.     }

  76.     /**
  77.      * Throws {@link UnsupportedOperationException}.
  78.      *
  79.      * @param value
  80.      *            ignored
  81.      * @return nothing
  82.      */
  83.     @Override
  84.     public final T setValue(final T value) {
  85.         throw new UnsupportedOperationException("Cannot alter Diff object.");
  86.     }

  87.     /**
  88.      * Returns a {@link String} representation of the {@link Diff}, with the
  89.      * following format:
  90.      *
  91.      * <pre>
  92.      * [fieldname: left-value, right-value]
  93.      * </pre>
  94.      *
  95.      * @return the string representation
  96.      */
  97.     @Override
  98.     public final String toString() {
  99.         return String.format("[%s: %s, %s]", fieldName, getLeft(), getRight());
  100.     }
  101. }