001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.prolog;
021    
022    import java.util.ArrayList;
023    
024    
025    /**
026     * 
027     * @author Sebastian Bader
028     *
029     */
030    public class List extends Term {
031        private Term head;
032        private List tail;
033        
034        public List() {
035            head = null;
036            tail = null;
037        }
038        
039        public List(Term head, List tail) {
040            this.head = head;
041            this.tail = tail;
042            if (tail == null)
043                this.tail = new List();
044        }
045        
046        public static List compose(ArrayList<Term> content) {
047            if (content.isEmpty()) {
048                return new List();
049            } else {
050                Term head = (Term) content.remove(0);
051                List body = compose(content);
052                return new List(head, body);
053            }            
054        }   
055        
056        @Override
057            public boolean isGround() {
058            if (!head.isGround())
059                return false;            
060            return tail.isGround();
061        }
062        
063        
064        @Override
065            public String toString() {
066            return "L["+((head != null)?head.toString()+"|"+tail:"")+"]";
067        }
068    
069        @Override
070            public String toPLString() {
071            return "["+((head != null)?head.toPLString()+"|"+tail.toPLString():"")+"]";
072        }
073        
074        @Override
075            public Term getInstance(Variable variable, Term term) {
076            if (head != null) {
077                Term newhead = head.getInstance(variable, term);
078                List newtail = (List) tail.getInstance(variable, term);
079                return new List(newhead, newtail);
080            }
081            return new List(null, null);
082        }
083    
084        @Override
085            public boolean equals(Object obj) {
086            if (obj == null)
087                return false;
088            
089            List list;
090            try {
091                    list = (List) obj;
092            } catch (ClassCastException cce) {
093                return false;
094            }
095    
096            if (head == null) {
097                if (list.head != null)
098                    return false;
099            } else {
100                if (!head.equals(list.head))
101                    return false;
102            }
103    
104            if (tail == null) {
105                return (list.tail == null);
106            } else {
107                return tail.equals(list.tail);
108            }                          
109        }
110    
111        @Override
112            public int hashCode() {
113            if (head == null)
114                return 0;
115            return head.hashCode();
116        }
117    
118        @Override
119            public Object clone() {
120            return new List((Term) head.clone(), (List) tail.clone());
121        }
122    }