001package com.pi4j.io.gpio;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  GpioFactory.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2016 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.concurrent.DefaultExecutorServiceFactory;
034import com.pi4j.concurrent.ExecutorServiceFactory;
035import com.pi4j.io.gpio.impl.GpioControllerImpl;
036import com.pi4j.platform.Platform;
037import com.pi4j.platform.PlatformManager;
038
039/**
040 * <p>This factory class provides a static method to create new 'GpioController' instances. </p>
041 *
042 * <p>
043 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
044 * the following system libraries:
045 * <ul>
046 * <li>pi4j</li>
047 * <li>wiringPi</li>
048 * </ul>
049 *
050 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
051 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
052 * </blockquote>
053 * </p>
054 *
055 * @see com.pi4j.io.gpio.GpioController
056 * @see com.pi4j.io.gpio.GpioProvider
057 *
058 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
059 * @author Robert Savage (<a
060 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
061 */
062@SuppressWarnings("unused")
063public class GpioFactory {
064
065    // we only allow a single controller to exists
066    private static GpioController controller = null;
067
068    // we only allow a single default provider to exists
069    private static GpioProvider provider = null;
070
071    // we only allow a single default scheduled executor service factory to exists
072    private static ExecutorServiceFactory executorServiceFactory = null;
073
074    // private constructor
075    private GpioFactory() {
076        // forbid object construction
077    }
078
079    /**
080     * <p>Return default instance of {@link GpioController}.</p>
081     * <p>Note: this is not thread safe singleton pattern implementation.
082     *    Implementation does not provide any synchronization or mechanisms to prevent
083     *    instantiation of two instances.</p>
084     *
085     * @return Return a new GpioController impl instance.
086     */
087    public static GpioController getInstance() {
088        // if a controller has not been created, then create a new instance
089        // Note: this is not thread safe singleton
090        if (controller == null) {
091            controller = new GpioControllerImpl();
092        }
093        // else return a copy of the existing controller
094        return controller;
095    }
096
097    /**
098     * <p>Return default instance of {@link GpioProvider}.</p>
099     * <p>Note: this is not thread safe singleton pattern implementation.
100     *    Implementation does not provide any synchronization or mechanisms to prevent
101     *    instantiation of two instances.</p>
102     *
103     * @return Return a new GpioController impl instance.
104     */
105    public static GpioProvider getDefaultProvider() {
106        // if a provider has not been created, then create a new instance
107        if (provider == null) {
108            // create the provider based on the PlatformManager's selected platform
109            provider = PlatformManager.getPlatform().getGpioProvider();
110        }
111
112        // return the provider instance
113        return provider;
114    }
115
116    /**
117     * Sets default {@link GpioProvider}.
118     *
119     * @param provider default gpio provider
120     */
121    public static void setDefaultProvider(GpioProvider provider) {
122        // set the default provider instance
123        GpioFactory.provider = provider;
124    }
125
126
127    /**
128     * <p>Return instance of {@link ExecutorServiceFactory}.</p>
129     * <p>Note: .</p>
130     *
131     * @return Return a new GpioController impl instance.
132     */
133    public static ExecutorServiceFactory getExecutorServiceFactory() {
134        // if an executor service provider factory has not been created, then create a new default instance
135        if (executorServiceFactory == null) {
136            executorServiceFactory = new DefaultExecutorServiceFactory();
137        }
138        // return the provider instance
139        return executorServiceFactory;
140    }
141
142    /**
143     * Sets default {@link ExecutorServiceFactory}.
144     *
145     * @param executorServiceFactory service factory instance
146     */
147    public static void setExecutorServiceFactory(ExecutorServiceFactory executorServiceFactory) {
148        // set the default factory instance
149        GpioFactory.executorServiceFactory = executorServiceFactory;
150    }
151}