001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of AutomataLib, http://www.automatalib.net/.
003 * 
004 * AutomataLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * AutomataLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with AutomataLib; if not, see
015 * http://www.gnu.de/documents/lgpl.en.html.
016 */
017package net.automatalib.words.impl;
018
019import java.util.Objects;
020
021import net.automatalib.words.abstractimpl.AbstractAlphabet;
022
023public class ArrayAlphabet<I> extends AbstractAlphabet<I> {
024
025        protected final I[] symbols;
026        
027        @SafeVarargs
028        public ArrayAlphabet(I ...symbols) {
029                this.symbols = symbols;
030        }
031
032        @Override
033        public I getSymbol(int index) throws IllegalArgumentException {
034                return symbols[index];
035        }
036
037        @Override
038        public int getSymbolIndex(I symbol) throws IllegalArgumentException {
039                for(int i = 0; i < symbols.length; i++) {
040                        if(Objects.equals(symbols[i], symbol))
041                                return i;
042                }
043                return -1;
044        }
045
046        @Override
047        public int size() {
048                return symbols.length;
049        }
050
051        /* (non-Javadoc)
052         * @see net.automatalib.words.abstractimpl.AbstractAlphabet#writeToArray(int, java.lang.Object[], int, int)
053         */
054        @Override
055        public void writeToArray(int offset, Object[] array, int tgtOfs, int num) {
056                System.arraycopy(symbols, offset, array, tgtOfs, num);
057        }
058        
059        
060
061}