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 - 2013 Pi4J 015 * %% 016 * Licensed under the Apache License, Version 2.0 (the "License"); 017 * you may not use this file except in compliance with the License. 018 * You may obtain a copy of the License at 019 * 020 * http://www.apache.org/licenses/LICENSE-2.0 021 * 022 * Unless required by applicable law or agreed to in writing, software 023 * distributed under the License is distributed on an "AS IS" BASIS, 024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 025 * See the License for the specific language governing permissions and 026 * limitations under the License. 027 * #L% 028 */ 029 030 031import com.pi4j.concurrent.DefaultExecutorServiceFactory; 032import com.pi4j.concurrent.ExecutorServiceFactory; 033import com.pi4j.io.gpio.impl.GpioControllerImpl; 034 035/** 036 * <p>This factory class provides a static method to create new 'GpioController' instances. </p> 037 * 038 * <p> 039 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to 040 * the following system libraries: 041 * <ul> 042 * <li>pi4j</li> 043 * <li>wiringPi</li> 044 * </ul> 045 * 046 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by 047 * Gordon Henderson @ <a href="https://projects.drogon.net/">https://projects.drogon.net/</a>) 048 * </blockquote> 049 * </p> 050 * 051 * @see #com.pi4j.io.gpio.Gpio 052 * 053 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a> 054 * @author Robert Savage (<a 055 * href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>) 056 */ 057public class GpioFactory { 058 059 // we only allow a single controller to exists 060 private static GpioController controller = null; 061 062 // we only allow a single default provider to exists 063 private static GpioProvider provider = null; 064 065 // we only allow a single default scheduled executor service factory to exists 066 private static ExecutorServiceFactory executorServiceFactory = null; 067 068 // private constructor 069 private GpioFactory() { 070 // forbid object construction 071 } 072 073 /** 074 * <p>Return default instance of {@link GpioController}.</p> 075 * <p>Note: this is not thread safe singleton pattern implementation. 076 * Implementation does not provide any synchronization or mechanisms to prevent 077 * instantiation of two instances.</p> 078 * 079 * @return Return a new GpioController impl instance. 080 */ 081 public static GpioController getInstance() { 082 // if a controller has not been created, then create a new instance 083 // Note: this is not thread safe singleton 084 if (controller == null) { 085 controller = new GpioControllerImpl(); 086 } 087 // else return a copy of the existing controller 088 return controller; 089 } 090 091 /** 092 * <p>Return default instance of {@link GpioProvider}.</p> 093 * <p>Note: this is not thread safe singleton pattern implementation. 094 * Implementation does not provide any synchronization or mechanisms to prevent 095 * instantiation of two instances.</p> 096 * 097 * @return Return a new GpioController impl instance. 098 */ 099 public static GpioProvider getDefaultProvider() { 100 // if a provider has not been created, then create a new instance 101 if (provider == null) { 102 provider = new RaspiGpioProvider(); 103 } 104 // return the provider instance 105 return provider; 106 } 107 108 /** 109 * Sets default {@link GpioProvider}. 110 * 111 * @param provider default gpio provider 112 */ 113 public static void setDefaultProvider(GpioProvider provider) { 114 // set the default provider instance 115 GpioFactory.provider = provider; 116 } 117 118 119 /** 120 * <p>Return instance of {@link ExecutorServiceFactory}.</p> 121 * <p>Note: .</p> 122 * 123 * @return Return a new GpioController impl instance. 124 */ 125 public static ExecutorServiceFactory getExecutorServiceFactory() { 126 // if an executor service provider factory has not been created, then create a new default instance 127 if (executorServiceFactory == null) { 128 executorServiceFactory = new DefaultExecutorServiceFactory(); 129 } 130 // return the provider instance 131 return executorServiceFactory; 132 } 133 134 /** 135 * Sets default {@link ExecutorServiceFactory}. 136 * 137 * @param executor service factory instance 138 */ 139 public static void setExecutorServiceFactory(ExecutorServiceFactory executorServiceFactory) { 140 // set the default factory instance 141 GpioFactory.executorServiceFactory = executorServiceFactory; 142 } 143}