001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.model;
016
017import java.io.Serializable;
018import java.util.HashSet;
019import java.util.Iterator;
020import java.util.Set;
021
022/**
023 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> TODO is this really needed?
024 */
025public class PropertyDiff implements Iterable<PropertyDiff> {
026
027    public static final int MODIFIED = 1;
028
029    public static final int ADDED = 2;
030
031    public static final int REMOVED = 3;
032
033    public int type;
034
035    public Serializable value;
036
037    public String name;
038
039    public final Set<PropertyDiff> children;
040
041    public PropertyDiff(int type, String name) {
042        this(type, name, null);
043    }
044
045    // FIXME: not fully implemented
046    public PropertyDiff(int type, String name, Serializable value) {
047        children = new HashSet<PropertyDiff>();
048    }
049
050    public boolean hasChildren() {
051        return !children.isEmpty();
052    }
053
054    public void modify(String name, Serializable value) {
055        children.add(new PropertyDiff(MODIFIED, name, value));
056    }
057
058    public void remove(String name) {
059        children.add(new PropertyDiff(REMOVED, name));
060    }
061
062    public void add(String name, Serializable value) {
063        children.add(new PropertyDiff(ADDED, name, value));
064    }
065
066    @Override
067    public Iterator<PropertyDiff> iterator() {
068        return children.iterator();
069    }
070
071    @Override
072    public boolean equals(Object obj) {
073        if (obj instanceof PropertyDiff) {
074            return name.equals(((PropertyDiff) obj).name);
075        }
076        return false;
077    }
078
079    @Override
080    public int hashCode() {
081        return (name == null) ? 0 : name.hashCode();
082    }
083
084}