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

com.hierynomus.protocol.commons.Factory Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C)2016 - SMBJ Contributors
 *
 * Licensed 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 com.hierynomus.protocol.commons;

import java.util.LinkedList;
import java.util.List;

/**
 * A basic factory interface.
 *
 * @param  the type of object created by this factory
 */
public interface Factory {

    /**
     * Inteface for a named factory. Named factories are simply factories that are identified by a name. Such names are
     * used mainly in SSH algorithm negotiation.
     *
     * @param  type of object created by this factory
     */
    interface Named
        extends Factory {

        /**
         * Utility functions
         */
        class Util {

            /**
             * Creates an object by picking a factory from {@code factories} that is identified by {@code name} from a
             * list of named {@code factories}. Uses the first match.
             *
             * @param factories list of available factories
             * @param name      name of the desired factory
             * @param        type of the {@code factories}
             * @return a newly created instance of {@code T} or {@code null} if there was no match
             */
            public static  T create(List> factories, String name) {
                Named factory = get(factories, name);
                if (factory != null) {
                    return factory.create();
                }
                return null;
            }

            /**
             * Retrieve a particular factory as identified by {@code name} from a list of named {@code factories}.
             * Returns the first match.
             *
             * @param factories list of factories
             * @param name      the name of the factory to retrieve
             * @param        type of the {@code factories}
             * @return a factory or {@code null} if there was no match
             */
            public static  Named get(List> factories, String name) {
                if (factories != null) {
                    for (Named f : factories)
                        if (f.getName().equals(name))
                            return f;
                }
                return null;
            }

            /**
             * Get a comma-delimited string containing the factory names from the given list of {@code factories}.
             *
             * @param factories list of available factories
             * @param        type of the {@code factories}
             * @return a comma separated list of factory names
             */
            public static  List getNames(List> factories) {
                List list = new LinkedList<>();
                for (Named f : factories)
                    list.add(f.getName());
                return list;
            }

        }

        /**
         * @return the name of this factory.
         */
        String getName();

    }

    /**
     * @return a new object created using this factory.
     */
    T create();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy