001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
003 * 
004 * LearnLib 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 * LearnLib 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 LearnLib; if not, see
015 * <http://www.gnu.de/documents/lgpl.en.html>.
016 */
017package de.learnlib.api;
018
019import net.automatalib.words.Word;
020
021public abstract class Query<I, O> {
022        
023        private int hashCode = 0;
024        
025        public abstract Word<I> getPrefix();
026        public abstract Word<I> getSuffix();
027        
028        public abstract void answer(O output);
029        
030        public final Word<I> getInput() {
031                return getPrefix().concat(getSuffix());
032        }
033        
034        
035        /*
036         * (non-Javadoc)
037         * @see java.lang.Object#equals(java.lang.Object)
038         */
039        @Override
040        public final boolean equals(Object o) {
041                if(this == o)
042                        return true;
043                if(o == null)
044                        return false;
045                if(!(o instanceof Query))
046                        return false;
047                Query<?,?> other = (Query<?,?>)o;
048                
049                Word<I> thisPref = getPrefix();
050                Word<I> thisSuff = getSuffix();
051                
052                Word<?> otherPref = other.getPrefix();
053                Word<?> otherSuff = other.getSuffix();
054                
055                if(thisPref != otherPref && (thisPref == null || !thisPref.equals(otherPref)))
056                        return false;
057                if(thisSuff != otherSuff && (thisSuff == null || !thisSuff.equals(otherSuff)))
058                        return false;
059                
060                return true;
061        }
062        
063        /*
064         * (non-Javadoc)
065         * @see java.lang.Object#hashCode()
066         */
067        @Override
068        public final int hashCode() {
069                if(hashCode != 0)
070                        return hashCode;
071                
072                Word<I> prefix = getPrefix(), suffix = getSuffix();
073                hashCode = 5;
074        hashCode = 89 * hashCode + (prefix != null ? prefix.hashCode() : 0);
075        hashCode = 89 * hashCode + (suffix != null ? suffix.hashCode() : 0);
076        return hashCode;
077        }
078        
079}