001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.record;
016
017import org.apache.hivemind.util.Defense;
018import org.apache.hivemind.util.ToStringBuilder;
019
020/**
021 * Represents a change to a component on a page.
022 * 
023 * @author Howard Lewis Ship
024 */
025
026public class PropertyChangeImpl implements PropertyChange
027{
028    private String _componentPath;
029
030    private String _propertyName;
031
032    private Object _newValue;
033
034    public PropertyChangeImpl(String componentPath, String propertyName, Object newValue)
035    {
036        Defense.notNull(propertyName, "propertyName");
037
038        // TODO: This breaks some tests, but those tests are wrong.
039        // Defense.notNull(newValue, "newValue");
040
041        _componentPath = componentPath;
042        _propertyName = propertyName;
043        _newValue = newValue;
044    }
045
046    /**
047     * The path to the component on the page, or null if the property is a property of the page.
048     */
049
050    public String getComponentPath()
051    {
052        return _componentPath;
053    }
054
055    /**
056     * The new value for the property, which may be null.
057     */
058
059    public Object getNewValue()
060    {
061        return _newValue;
062    }
063
064    /**
065     * The name of the property that changed.
066     */
067
068    public String getPropertyName()
069    {
070        return _propertyName;
071    }
072
073    public String toString()
074    {
075        ToStringBuilder builder = new ToStringBuilder(this);
076
077        builder.append("componentPath", _componentPath);
078        builder.append("propertyName", _propertyName);
079        builder.append("newValue", _newValue);
080
081        return builder.toString();
082    }
083
084    public boolean equals(Object object)
085    {
086        if (this == object)
087            return true;
088
089        if (object == null || object.getClass() != this.getClass())
090            return false;
091
092        PropertyChangeImpl other = (PropertyChangeImpl) object;
093
094        return same(_componentPath, other._componentPath)
095                && same(_propertyName, other._propertyName) && same(_newValue, other._newValue);
096    }
097
098    private boolean same(Object o1, Object o2)
099    {
100        return o1 == o2 || (o1 != null && o1.equals(o2));
101    }
102}