package com.diozero.util;

import java.util.HashMap;

/*
 * #%L
 * Device I/O Zero - Core
 * %%
 * Copyright (C) 2016 - 2017 mattjlewis
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */


import java.util.List;
import java.util.Map;

import com.diozero.api.DeviceMode;
import com.diozero.api.GpioInfo;

public abstract class BoardGpioInfo {
	private Map<Integer, List<DeviceMode>> gpios;
	private Map<Integer, GpioInfo> gpiosByGpioNumber;
	private Map<String, GpioInfo> gpiosByName;
	private Map<Integer, GpioInfo> gpiosByPin;
	
	public BoardGpioInfo() {
		gpios = new HashMap<>();
		gpiosByGpioNumber = new HashMap<>();
		gpiosByName = new HashMap<>();
		gpiosByPin = new HashMap<>();
		
		init();
	}
	
	protected abstract void init();
	
	protected void addGpioInfo(GpioInfo gpioInfo) {
		gpios.put(Integer.valueOf(gpioInfo.getGpioNum()), gpioInfo.getModes());
		gpiosByGpioNumber.put(Integer.valueOf(gpioInfo.getGpioNum()), gpioInfo);
		gpiosByName.put(gpioInfo.getName(), gpioInfo);
		gpiosByPin.put(Integer.valueOf(gpioInfo.getPin()), gpioInfo);
	}

	public boolean isSupported(DeviceMode mode, int gpio) {
		// Default to true if the gpios aren't defined
		if (gpios == null) {
			return true;
		}
		
		List<DeviceMode> modes = gpios.get(Integer.valueOf(gpio));
		// Default to true if the modes for the requested gpios isn't found
		if (modes == null) {
			return true;
		}
		
		return modes.contains(mode);
	}
	
	@SuppressWarnings("static-method")
	public int mapGpio(int gpio) {
		return gpio;
	}
}

@@ -24,35 +24,8 @@
 l;%0A%0A
-import java.util.HashMap;%0A%0A
 /*%0A 
@@ -1193,19 +1193,16 @@
 #L%25%0A */%0A
-%0A%0D%0A
 import j
@@ -1214,39 +1214,11 @@
 til.
-List;%0D%0Aimport java.util.Map;%0D%0A%0D
+*;%0A
 %0Aimp
@@ -2036,16 +2036,467 @@
 nfo);%0A%09%7D
+%0A%09%0A%09public Set%3CInteger%3E getGioNumbers() %7B%0A%09%09return gpiosByGpioNumber.keySet();%0A%09%7D%0A%09%0A%09public Collection%3CGpioInfo%3E getGpios() %7B%0A%09%09return gpiosByGpioNumber.values();%0A%09%7D%0A%09%0A%09public GpioInfo getByGpioNumber(int gpio) %7B%0A%09%09return gpiosByGpioNumber.get(Integer.valueOf(gpio));%0A%09%7D%0A%09%0A%09public GpioInfo getByName(String name) %7B%0A%09%09return gpiosByGpioNumber.get(name);%0A%09%7D%0A%09%0A%09public GpioInfo getByPin(int pin) %7B%0A%09%09return gpiosByGpioNumber.get(Integer.valueOf(pin));%0A%09%7D
 %0A%0D%0A%09publ
@@ -2865,17 +2865,16 @@
 de);%0D%0A%09%7D
-%0D
 %0A%09%0D%0A%09@Su
@@ -2957,13 +2957,12 @@
 pio;%0D%0A%09%7D
-%0D
 %0A%7D%0D%0A

Access to Gpio Info