001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.theme.styling.service.descriptors;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeMap;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.theme.styling.negotiation.Negotiator;
026
027/**
028 * Descriptor for contributed negotiators.
029 *
030 * @since 7.4
031 */
032@XObject("negotiator")
033public class NegotiatorDescriptor implements Comparable<NegotiatorDescriptor> {
034
035    @XNode("@class")
036    protected Class<Negotiator> klass;
037
038    @XNode("@order")
039    protected int order = 0;
040
041    @XNodeMap(value = "property", key = "@name", type = HashMap.class, componentType = String.class)
042    Map<String, String> properties = new HashMap<String, String>();
043
044    public Class<Negotiator> getNegotiatorClass() {
045        return klass;
046    }
047
048    public void setNegotiatorKlass(Class<Negotiator> klass) {
049        this.klass = klass;
050    }
051
052    public int getOrder() {
053        return order;
054    }
055
056    public void setOrder(int order) {
057        this.order = order;
058    }
059
060    public Map<String, String> getProperties() {
061        return properties;
062    }
063
064    public void setProperties(Map<String, String> properties) {
065        this.properties = properties;
066    }
067
068    @Override
069    public int compareTo(NegotiatorDescriptor o) {
070        int cmp = order - o.order;
071        if (cmp == 0) {
072            // make sure we have a deterministic sort
073            cmp = klass.getName().compareTo(o.klass.getName());
074        }
075        return cmp;
076    }
077
078    @Override
079    public NegotiatorDescriptor clone() {
080        NegotiatorDescriptor clone = new NegotiatorDescriptor();
081        clone.setNegotiatorKlass(getNegotiatorClass());
082        clone.setOrder(getOrder());
083        Map<String, String> props = getProperties();
084        if (props != null) {
085            clone.setProperties(new HashMap<String, String>(props));
086        }
087        return clone;
088    }
089
090}