001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3.builder; 018 019import java.lang.reflect.Type; 020 021import org.apache.commons.lang3.ObjectUtils; 022import org.apache.commons.lang3.reflect.TypeUtils; 023import org.apache.commons.lang3.tuple.Pair; 024 025/** 026 * <p> 027 * A {@code Diff} contains the differences between two {@link Diffable} class 028 * fields. 029 * </p> 030 * 031 * <p> 032 * Typically, {@code Diff}s are retrieved by using a {@link DiffBuilder} to 033 * produce a {@link DiffResult}, containing the differences between two objects. 034 * </p> 035 * 036 * 037 * @param <T> 038 * The type of object contained within this {@code Diff}. Differences 039 * between primitive objects are stored as their Object wrapper 040 * equivalent. 041 * @since 3.3 042 * @version $Id: Diff.java 1606051 2014-06-27 12:22:17Z ggregory $ 043 */ 044public abstract class Diff<T> extends Pair<T, T> { 045 046 private static final long serialVersionUID = 1L; 047 048 private final Type type; 049 private final String fieldName; 050 051 /** 052 * <p> 053 * Constructs a new {@code Diff} for the given field name. 054 * </p> 055 * 056 * @param fieldName 057 * the name of the field 058 */ 059 protected Diff(final String fieldName) { 060 this.type = ObjectUtils.defaultIfNull( 061 TypeUtils.getTypeArguments(getClass(), Diff.class).get( 062 Diff.class.getTypeParameters()[0]), Object.class); 063 this.fieldName = fieldName; 064 } 065 066 /** 067 * <p> 068 * Returns the type of the field. 069 * </p> 070 * 071 * @return the field type 072 */ 073 public final Type getType() { 074 return type; 075 } 076 077 /** 078 * <p> 079 * Returns the name of the field. 080 * </p> 081 * 082 * @return the field name 083 */ 084 public final String getFieldName() { 085 return fieldName; 086 } 087 088 /** 089 * <p> 090 * Returns a {@code String} representation of the {@code Diff}, with the 091 * following format:</p> 092 * 093 * <pre> 094 * [fieldname: left-value, right-value] 095 * </pre> 096 * 097 * 098 * @return the string representation 099 */ 100 @Override 101 public final String toString() { 102 return String.format("[%s: %s, %s]", fieldName, getLeft(), getRight()); 103 } 104 105 /** 106 * <p> 107 * Throws {@code UnsupportedOperationException}. 108 * </p> 109 * 110 * @param value 111 * ignored 112 * @return nothing 113 */ 114 @Override 115 public final T setValue(final T value) { 116 throw new UnsupportedOperationException("Cannot alter Diff object."); 117 } 118}