001/**
002 * Copyright (C) 2007 - 2016, 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 */
019package org.dllearner.prolog;
020
021import java.util.ArrayList;
022
023/**
024 * 
025 * @author Sebastian Bader
026 *
027 */
028public class List extends Term {
029    private Term head;
030    private List tail;
031    
032    public List() {
033        head = null;
034        tail = null;
035    }
036    
037    public List(Term head, List tail) {
038        this.head = head;
039        this.tail = tail;
040        if (tail == null)
041            this.tail = new List();
042    }
043    
044    public static List compose(ArrayList<Term> content) {
045        if (content.isEmpty()) {
046            return new List();
047        } else {
048            Term head = content.remove(0);
049            List body = compose(content);
050            return new List(head, body);
051        }            
052    }   
053    
054    @Override
055        public boolean isGround() {
056        if (!head.isGround())
057            return false;            
058        return tail.isGround();
059    }
060    
061    
062    @Override
063        public String toString() {
064        return "L["+((head != null)?head.toString()+"|"+tail:"")+"]";
065    }
066
067    @Override
068        public String toPLString() {
069        return "["+((head != null)?head.toPLString()+"|"+tail.toPLString():"")+"]";
070    }
071    
072    @Override
073        public Term getInstance(Variable variable, Term term) {
074        if (head != null) {
075            Term newhead = head.getInstance(variable, term);
076            List newtail = (List) tail.getInstance(variable, term);
077            return new List(newhead, newtail);
078        }
079        return new List(null, null);
080    }
081
082    @Override
083        public boolean equals(Object obj) {
084        if (obj == null)
085            return false;
086        
087        List list;
088        try {
089                list = (List) obj;
090        } catch (ClassCastException cce) {
091            return false;
092        }
093
094        if (head == null) {
095            if (list.head != null)
096                return false;
097        } else {
098            if (!head.equals(list.head))
099                return false;
100        }
101
102        if (tail == null) {
103            return (list.tail == null);
104        } else {
105            return tail.equals(list.tail);
106        }                          
107    }
108
109    @Override
110        public int hashCode() {
111        if (head == null)
112            return 0;
113        return head.hashCode();
114    }
115
116    @Override
117        public Object clone() {
118        return new List((Term) head.clone(), (List) tail.clone());
119    }
120}