| 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 | |
| 19 | import java.lang.reflect.Type; |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import org.apache.commons.lang3.ObjectUtils; |
| 23 | import org.apache.commons.lang3.reflect.TypeUtils; |
| 24 | import org.apache.commons.lang3.tuple.Pair; |
| 25 | |
| 26 | /** |
| 27 | * A {@link Diff} contains the differences between two {@link Diffable} class |
| 28 | * fields. |
| 29 | * |
| 30 | * <p> |
| 31 | * Typically, {@link Diff}s are retrieved by using a {@link DiffBuilder} to |
| 32 | * produce a {@link DiffResult}, containing the differences between two objects. |
| 33 | * </p> |
| 34 | * |
| 35 | * @param <T> |
| 36 | * The type of object contained within this {@link Diff}. Differences |
| 37 | * between primitive objects are stored as their Object wrapper |
| 38 | * equivalent. |
| 39 | * @since 3.3 |
| 40 | */ |
| 41 | public abstract class Diff<T> extends Pair<T, T> { |
| 42 | |
| 43 | private static final long serialVersionUID = 1L; |
| 44 | |
| 45 | /** The field type. */ |
| 46 | private final Type type; |
| 47 | |
| 48 | /** The field name. */ |
| 49 | private final String fieldName; |
| 50 | |
| 51 | /** |
| 52 | * Constructs a new {@link Diff} for the given field name. |
| 53 | * |
| 54 | * @param fieldName |
| 55 | * the field name |
| 56 | */ |
| 57 | protected Diff(final String fieldName) { |
| 58 | this.fieldName = Objects.requireNonNull(fieldName); |
| 59 | this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class); |
| 60 | } |
| 61 | |
| 62 | Diff(final String fieldName, final Type type) { |
| 63 | this.fieldName = Objects.requireNonNull(fieldName); |
| 64 | this.type = Objects.requireNonNull(type); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the name of the field. |
| 69 | * |
| 70 | * @return the field name |
| 71 | */ |
| 72 | public final String getFieldName() { |
| 73 | return fieldName; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the type of the field. |
| 78 | * |
| 79 | * @return the field type |
| 80 | * @deprecated Unused, will be removed in 4.0.0. |
| 81 | */ |
| 82 | @Deprecated |
| 83 | public final Type getType() { |
| 84 | return type; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Throws {@link UnsupportedOperationException}. |
| 89 | * |
| 90 | * @param value |
| 91 | * ignored |
| 92 | * @return nothing |
| 93 | */ |
| 94 | @Override |
| 95 | public final T setValue(final T value) { |
| 96 | throw new UnsupportedOperationException("Cannot alter Diff object."); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns a {@link String} representation of the {@link Diff}, with the |
| 101 | * following format: |
| 102 | * |
| 103 | * <pre> |
| 104 | * [fieldname: left-value, right-value] |
| 105 | * </pre> |
| 106 | * |
| 107 | * @return the string representation |
| 108 | */ |
| 109 | @Override |
| 110 | public final String toString() { |
| 111 | return String.format("[%s: %s, %s]", fieldName, getLeft(), getRight()); |
| 112 | } |
| 113 | } |