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 - 2015 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;
036
037/**
038 * <p>This factory class provides a static method to create new 'GpioController' instances. </p>
039 * 
040 * <p>
041 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
042 * the following system libraries:
043 * <ul>
044 * <li>pi4j</li>
045 * <li>wiringPi</li>
046 * </ul>
047 * 
048 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
049 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
050 * </blockquote>
051 * </p>
052 * 
053 * @see com.pi4j.io.gpio.GpioController
054 * @see com.pi4j.io.gpio.GpioProvider
055 *
056 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
057 * @author Robert Savage (<a
058 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
059 */
060@SuppressWarnings("unused")
061public class GpioFactory {
062
063    // we only allow a single controller to exists
064    private static GpioController controller = null;
065
066    // we only allow a single default provider to exists
067    private static GpioProvider provider = null;
068
069    // we only allow a single default scheduled executor service factory to exists
070    private static ExecutorServiceFactory executorServiceFactory = null;
071    
072    // private constructor 
073    private GpioFactory() {
074        // forbid object construction 
075    }
076    
077    /**
078     * <p>Return default instance of {@link GpioController}.</p>
079     * <p>Note: this is not thread safe singleton pattern implementation. 
080     *    Implementation does not provide any synchronization or mechanisms to prevent
081     *    instantiation of two instances.</p>
082     * 
083     * @return Return a new GpioController impl instance.
084     */
085    public static GpioController getInstance() {
086        // if a controller has not been created, then create a new instance
087        // Note: this is not thread safe singleton 
088        if (controller == null) {
089            controller = new GpioControllerImpl();
090        }
091        // else return a copy of the existing controller
092        return controller;
093    }
094    
095    /**
096     * <p>Return default instance of {@link GpioProvider}.</p>
097     * <p>Note: this is not thread safe singleton pattern implementation. 
098     *    Implementation does not provide any synchronization or mechanisms to prevent
099     *    instantiation of two instances.</p>
100     * 
101     * @return Return a new GpioController impl instance.
102     */
103    public static GpioProvider getDefaultProvider() {
104        // if a provider has not been created, then create a new instance
105        if (provider == null) {
106            provider = new RaspiGpioProvider();
107        }
108        // return the provider instance
109        return provider;
110    }
111
112    /**
113     * Sets default {@link GpioProvider}.
114     * 
115     * @param provider default gpio provider
116     */
117    public static void setDefaultProvider(GpioProvider provider) {
118        // set the default provider instance
119        GpioFactory.provider = provider;
120    }
121    
122    
123    /**
124     * <p>Return instance of {@link ExecutorServiceFactory}.</p>
125     * <p>Note: .</p>
126     * 
127     * @return Return a new GpioController impl instance.
128     */
129    public static ExecutorServiceFactory getExecutorServiceFactory() {
130        // if an executor service provider factory has not been created, then create a new default instance
131        if (executorServiceFactory == null) {
132            executorServiceFactory = new DefaultExecutorServiceFactory();
133        }
134        // return the provider instance
135        return executorServiceFactory;
136    }
137
138    /**
139     * Sets default {@link ExecutorServiceFactory}.
140     * 
141     * @param executorServiceFactory service factory instance
142     */
143    public static void setExecutorServiceFactory(ExecutorServiceFactory executorServiceFactory) {
144        // set the default factory instance
145        GpioFactory.executorServiceFactory = executorServiceFactory;
146    }    
147}