001/*
002 * (C) Copyright 2015-2018 Nuxeo (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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.core;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.lang3.builder.EqualsBuilder;
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeList;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.ecm.web.resources.api.ResourceBundle;
029
030/**
031 * @since 7.3
032 */
033@XObject("bundle")
034public class ResourceBundleDescriptor implements ResourceBundle {
035
036    private static final long serialVersionUID = 1L;
037
038    @XNode("@name")
039    public String name;
040
041    @XNode("resources@append")
042    boolean append;
043
044    @XNodeList(value = "resources/resource", type = ArrayList.class, componentType = String.class)
045    List<String> resources;
046
047    public String getName() {
048        return name;
049    }
050
051    public List<String> getResources() {
052        return resources;
053    }
054
055    public boolean isAppend() {
056        return append;
057    }
058
059    /**
060     * @since 7.4
061     */
062    public void setName(String name) {
063        this.name = name;
064    }
065
066    /**
067     * @since 7.4
068     */
069    public void setAppend(boolean append) {
070        this.append = append;
071    }
072
073    /**
074     * @since 7.4
075     */
076    public void setResources(List<String> resources) {
077        this.resources = resources;
078    }
079
080    public ResourceBundleDescriptor clone() {
081        ResourceBundleDescriptor c = new ResourceBundleDescriptor();
082        c.name = name;
083        c.append = append;
084        if (resources != null) {
085            c.resources = new ArrayList<>(resources);
086        }
087        return c;
088    }
089
090    @Override
091    public ResourceBundle merge(ResourceBundle other) {
092        if (other instanceof ResourceBundleDescriptor) {
093            boolean append = ((ResourceBundleDescriptor) other).isAppend();
094            List<String> res = other.getResources();
095            List<String> merged = new ArrayList<>();
096            if (append && resources != null) {
097                merged.addAll(resources);
098            }
099            if (res != null) {
100                merged.addAll(res);
101            }
102            resources = merged;
103        }
104        return this;
105    }
106
107    @Override
108    public boolean equals(Object obj) {
109        if (!(obj instanceof ResourceBundleDescriptor)) {
110            return false;
111        }
112        if (obj == this) {
113            return true;
114        }
115        ResourceBundleDescriptor b = (ResourceBundleDescriptor) obj;
116        return new EqualsBuilder().append(name, b.name).append(append, b.append).append(resources, b.resources).isEquals();
117    }
118
119}