All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.felix.feature.impl.FeatureBuilderImpl Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apache.felix.feature.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.osgi.service.feature.Feature;
import org.osgi.service.feature.FeatureBuilder;
import org.osgi.service.feature.FeatureBundle;
import org.osgi.service.feature.FeatureConfiguration;
import org.osgi.service.feature.FeatureExtension;
import org.osgi.service.feature.ID;

class FeatureBuilderImpl implements FeatureBuilder {
    private final ID id;

    private String name;
    private String description;
    private String docURL;
    private String license;
    private String scm;
    private String vendor;
    private boolean complete;

    private final List bundles = new ArrayList<>();
    private final List categories = new ArrayList<>();
    private final Map configurations = new LinkedHashMap<>();
    private final Map extensions = new LinkedHashMap<>();
    private final Map variables = new LinkedHashMap<>();

    FeatureBuilderImpl(ID id) {
        this.id = id;
    }

    @Override
    public FeatureBuilder setName(String name) {
        this.name = name;
        return this;
    }

    @Override
    public FeatureBuilder setDocURL(String url) {
        this.docURL = url;
        return this;
    }

    @Override
    public FeatureBuilder setVendor(String vendor) {
        this.vendor = vendor;
        return this;
    }

    @Override
    public FeatureBuilder setLicense(String license) {
        this.license = license;
        return this;
    }

    @Override
    public FeatureBuilder setComplete(boolean complete) {
        this.complete = complete;
        return this;
    }

    @Override
    public FeatureBuilder setDescription(String description) {
        this.description = description;
        return this;
    }

    @Override
    public FeatureBuilder setSCM(String scm) {
        this.scm = scm;
        return this;
    }

    @Override
    public FeatureBuilder addBundles(FeatureBundle ... bundles) {
        this.bundles.addAll(Arrays.asList(bundles));
        return this;
    }

    
    @Override
	public FeatureBuilder addCategories(String ...categories) {
    	this.categories.addAll(Arrays.asList(categories));
		return this;
	}

	@Override
    public FeatureBuilder addConfigurations(FeatureConfiguration ... configs) {
        for (FeatureConfiguration cfg : configs) {
            this.configurations.put(cfg.getPid(), cfg);
        }
        return this;
    }

    @Override
    public FeatureBuilder addExtensions(FeatureExtension ... extensions) {
        for (FeatureExtension ex : extensions) {
            this.extensions.put(ex.getName(), ex);
        }
        return this;
    }

    @Override
    public FeatureBuilder addVariable(String key, Object value) {
        this.variables.put(key, value);
        return this;
    }

    @Override
    public FeatureBuilder addVariables(Map variables) {
        this.variables.putAll(variables);
        return this;
    }

    @Override
    public Feature build() {
        return new FeatureImpl(id, name, description, docURL,
                license, scm, vendor, complete,
                bundles, categories, configurations, extensions, variables);
    }

    private static class FeatureImpl implements Feature {
        private final ID id;
        private final Optional name;
        private final Optional description;
        private final Optional docURL;
        private final Optional license;
        private final Optional scm;
        private final Optional vendor;
        private final boolean complete;

        private final List bundles;
        private final List categories;
        private final Map configurations;
        private final Map extensions;
        private final Map variables;

        private FeatureImpl(ID id, String aName, String desc, String docs, String lic, String sc, String vnd,
                boolean comp, List bs, List cats, Map cs,
                Map es, Map vars) {
            this.id = id;
            name = Optional.ofNullable(aName);
            description = Optional.ofNullable(desc);
            docURL = Optional.ofNullable(docs);
            license = Optional.ofNullable(lic);
            scm = Optional.ofNullable(sc);
            vendor = Optional.ofNullable(vnd);
            complete = comp;

            bundles = Collections.unmodifiableList(bs);
            categories = Collections.unmodifiableList(cats);
            configurations = Collections.unmodifiableMap(cs);
            extensions = Collections.unmodifiableMap(es);
            variables = Collections.unmodifiableMap(vars);
        }

        @Override
        public ID getID() {
            return id;
        }
        
        @Override
        public Optional getName() {
            return name;
        }

        @Override
        public Optional getDescription() {
            return description;
        }

        @Override
        public Optional getVendor() {
            return vendor;
        }

        @Override
        public Optional getLicense() {
            return license;
        }

        @Override
        public Optional getDocURL() {
            return docURL;
        }

        @Override
        public Optional getSCM() {
            return scm;
        }

        @Override
        public boolean isComplete() {
            return complete;
        }

        @Override
        public List getBundles() {
            return bundles;
        }

        @Override
        public List getCategories() {
            return categories;
        }

        @Override
        public Map getConfigurations() {
            return configurations;
        }

        @Override
        public Map getExtensions() {
            return extensions;
        }

        @Override
        public Map getVariables() {
            return variables;
        }

        @Override
		public int hashCode() {
			return Objects.hash(bundles, categories, complete, configurations, description, docURL,
					extensions, id, license, name, scm, variables, vendor);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			FeatureImpl other = (FeatureImpl) obj;
			return Objects.equals(bundles, other.bundles) && Objects.equals(categories, other.categories)
					&& complete == other.complete && Objects.equals(configurations, other.configurations)
					&& Objects.equals(description, other.description)
					&& Objects.equals(docURL, other.docURL) && Objects.equals(extensions, other.extensions)
					&& Objects.equals(id, other.id) && Objects.equals(license, other.license)
					&& Objects.equals(name, other.name) && Objects.equals(scm, other.scm)
					&& Objects.equals(variables, other.variables) && Objects.equals(vendor, other.vendor);
		}

		@Override
        public String toString() {
            return "FeatureImpl [getID()=" + getID() + "]";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy