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

net.schmizz.sshj.common.Factory Maven / Gradle / Ivy

There is a newer version: 0.39.0
Show newest version
/*
 * Copyright (C)2009 - SSHJ 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 net.schmizz.sshj.common;

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

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

    /**
     * Interface 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 */
        public static 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) {
                if (factories != null)
                    for (Named f : factories)
                        if (f.getName().equals(name))
                            return f.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) {
                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