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