001/*
002 * (C) Copyright 2014 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.targetplatforms.api.impl;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.targetplatforms.api.TargetPackage;
028import org.nuxeo.targetplatforms.api.TargetPlatform;
029
030/**
031 * @since 5.7.1
032 */
033public class TargetPlatformImpl extends TargetImpl implements TargetPlatform {
034
035    private static final long serialVersionUID = 1L;
036
037    protected TargetPlatform parent;
038
039    protected Map<String, TargetPackage> availablePackages;
040
041    protected List<String> testVersions;
042
043    protected TargetPlatformImpl() {
044        super();
045    }
046
047    public TargetPlatformImpl(String id) {
048        super(id);
049    }
050
051    public TargetPlatformImpl(String id, String name, String version, String refVersion, String label) {
052        super(id, name, version, refVersion, label);
053    }
054
055    @Override
056    public List<String> getAvailablePackagesIds() {
057        if (availablePackages == null) {
058            return Collections.emptyList();
059        }
060        return new ArrayList<String>(availablePackages.keySet());
061    }
062
063    @Override
064    public List<TargetPackage> getAvailablePackages() {
065        if (availablePackages == null) {
066            return Collections.emptyList();
067        }
068        return new ArrayList<TargetPackage>(availablePackages.values());
069    }
070
071    public void addAvailablePackage(TargetPackage pack) {
072        if (pack == null) {
073            return;
074        }
075        if (availablePackages == null) {
076            availablePackages = new LinkedHashMap<String, TargetPackage>();
077        }
078        availablePackages.put(pack.getId(), pack);
079    }
080
081    public void setAvailablePackages(Map<String, TargetPackage> tps) {
082        if (availablePackages == null) {
083            availablePackages = new LinkedHashMap<String, TargetPackage>();
084        } else {
085            availablePackages.clear();
086        }
087        if (tps != null) {
088            availablePackages.putAll(tps);
089        }
090    }
091
092    @Override
093    public TargetPlatform getParent() {
094        return parent;
095    }
096
097    public void setParent(TargetPlatform parent) {
098        this.parent = parent;
099    }
100
101    @Override
102    public List<String> getTestVersions() {
103        return testVersions;
104    }
105
106    public void setTestVersions(List<String> testVersions) {
107        if (testVersions == null) {
108            this.testVersions = testVersions;
109        } else {
110            // dereference
111            this.testVersions = new ArrayList<String>(testVersions);
112        }
113    }
114
115    @Override
116    public int compareTo(TargetPlatform o) {
117        // compare first on name, then on version
118        int comp = getName().compareTo(o.getName());
119        if (comp == 0) {
120            comp = getVersion().compareTo(o.getVersion());
121        }
122        return comp;
123    }
124
125}