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 *     mcedica@nuxeo.com
018 *     Anahide Tchertchian
019 */
020package org.nuxeo.targetplatforms.api.impl;
021
022import org.nuxeo.targetplatforms.api.Target;
023
024/**
025 * Common class to describe a target platform or package.
026 *
027 * @since 5.7.1
028 */
029public class TargetImpl extends TargetInfoImpl implements Target {
030
031    private static final long serialVersionUID = 1L;
032
033    // needed by GWT serialization
034    protected TargetImpl() {
035        super();
036    }
037
038    public TargetImpl(String id) {
039        super(id);
040    }
041
042    public TargetImpl(String id, String name, String version, String refVersion, String label) {
043        super(id, name, version, refVersion, label);
044    }
045
046    @Override
047    public boolean isAfterVersion(String version) {
048        if (version == null || version.trim().length() == 0 || getRefVersion().equals(version)) {
049            return true;
050        }
051
052        String[] components1 = getRefVersion().split("\\.");
053        String[] components2 = version.split("\\.");
054        int length = Math.min(components1.length, components2.length);
055        for(int i = 0; i < length; i++) {
056            int result = Integer.compare(Integer.valueOf(components1[i]), Integer.valueOf(components2[i]));
057            if (result != 0) {
058                return result > 0;
059            }
060        }
061        return components1.length > components2.length;
062    }
063
064    @Override
065    public boolean isStrictlyBeforeVersion(String version) {
066        if (version == null || version.trim().length() == 0) {
067            return true;
068        }
069
070        String[] components1 = getRefVersion().split("\\.");
071        String[] components2 = version.split("\\.");
072        int length = Math.min(components1.length, components2.length);
073        for(int i = 0; i < length; i++) {
074            int result = Integer.compare(Integer.valueOf(components1[i]), Integer.valueOf(components2[i]));
075            if (result != 0) {
076                return result < 0;
077            }
078        }
079        return components1.length < components2.length;
080    }
081
082    @Override
083    public boolean isVersion(String version) {
084        if (version == null || version.trim().length() == 0) {
085            return false;
086        }
087        return version.compareTo(getRefVersion()) == 0;
088    }
089
090    @Override
091    public boolean isStrictlyBeforeVersion(Target version) {
092        return isStrictlyBeforeVersion(version.getRefVersion());
093    }
094
095    @Override
096    public boolean isAfterVersion(Target version) {
097        return isAfterVersion(version.getRefVersion());
098    }
099
100    @Override
101    public boolean isVersion(Target version) {
102        return isVersion(getRefVersion());
103    }
104
105}