001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api.model;
023
024import java.io.Serializable;
025import java.util.HashSet;
026import java.util.Iterator;
027import java.util.Set;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> TODO is this really needed?
031 */
032public class PropertyDiff implements Iterable<PropertyDiff> {
033
034    public static final int MODIFIED = 1;
035
036    public static final int ADDED = 2;
037
038    public static final int REMOVED = 3;
039
040    public int type;
041
042    public Serializable value;
043
044    public String name;
045
046    public final Set<PropertyDiff> children;
047
048    public PropertyDiff(int type, String name) {
049        this(type, name, null);
050    }
051
052    // FIXME: not fully implemented
053    public PropertyDiff(int type, String name, Serializable value) {
054        children = new HashSet<PropertyDiff>();
055    }
056
057    public boolean hasChildren() {
058        return !children.isEmpty();
059    }
060
061    public void modify(String name, Serializable value) {
062        children.add(new PropertyDiff(MODIFIED, name, value));
063    }
064
065    public void remove(String name) {
066        children.add(new PropertyDiff(REMOVED, name));
067    }
068
069    public void add(String name, Serializable value) {
070        children.add(new PropertyDiff(ADDED, name, value));
071    }
072
073    @Override
074    public Iterator<PropertyDiff> iterator() {
075        return children.iterator();
076    }
077
078    @Override
079    public boolean equals(Object obj) {
080        if (obj instanceof PropertyDiff) {
081            return name.equals(((PropertyDiff) obj).name);
082        }
083        return false;
084    }
085
086    @Override
087    public int hashCode() {
088        return (name == null) ? 0 : name.hashCode();
089    }
090
091}