001/*
002 * (C) Copyright 2014-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.targetplatforms.api.impl;
020
021import org.apache.commons.lang3.StringUtils;
022import org.nuxeo.targetplatforms.api.TargetInfo;
023import org.nuxeo.targetplatforms.api.TargetPlatformFilter;
024
025/**
026 * Filter of target platforms handling enabled, deprecated, restricted and type criteria.
027 *
028 * @since 5.7.1
029 */
030public class TargetPlatformFilterImpl implements TargetPlatformFilter {
031
032    private static final long serialVersionUID = 1L;
033
034    protected boolean filterDisabled = false;
035
036    protected boolean filterRestricted = false;
037
038    protected boolean filterDeprecated = false;
039
040    protected boolean filterDefault = false;
041
042    protected boolean filterNotTrial = false;
043
044    protected String filterType;
045
046    public TargetPlatformFilterImpl() {
047        super();
048    }
049
050    public TargetPlatformFilterImpl(boolean filterDisabled, boolean filterRestricted, boolean filterDeprecated,
051            boolean filterDefault, String filterType) {
052        super();
053        this.filterDisabled = filterDisabled;
054        this.filterRestricted = filterRestricted;
055        this.filterDeprecated = filterDeprecated;
056        this.filterDefault = filterDefault;
057        this.filterType = filterType;
058    }
059
060    public TargetPlatformFilterImpl(boolean filterNotTrial) {
061        super();
062        this.filterNotTrial = filterNotTrial;
063    }
064
065    public void setFilterDisabled(boolean filterDisabled) {
066        this.filterDisabled = filterDisabled;
067    }
068
069    public void setFilterRestricted(boolean filterRestricted) {
070        this.filterRestricted = filterRestricted;
071    }
072
073    public void setFilterDeprecated(boolean filterDeprecated) {
074        this.filterDeprecated = filterDeprecated;
075    }
076
077    public void setFilterType(String filterType) {
078        this.filterType = filterType;
079    }
080
081    public void setFilterDefault(boolean filterDefault) {
082        this.filterDefault = filterDefault;
083    }
084
085    public void setFilterNotTrial(boolean filterNotTrial) {
086        this.filterNotTrial = filterNotTrial;
087    }
088
089    @Override
090    public boolean accepts(TargetInfo t) {
091        if (t == null) {
092            return false;
093        }
094        if ((filterDisabled && !t.isEnabled()) || (filterDeprecated && t.isDeprecated())
095                || (filterRestricted && t.isRestricted()) || (filterNotTrial && !t.isTrial())
096                || (filterDefault && !t.isDefault())
097                || (!StringUtils.isBlank(filterType) && !t.matchesType(filterType))) {
098            return false;
099        }
100        return true;
101    }
102
103}