2ronwalf// The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalf/*
2ronwalf * Created on Dec 23, 2004
2ronwalf */
2ronwalfpackage impl.owl.list;
2ronwalf
2ronwalf
2ronwalfimport impl.owl.WrappedIndividual;
2ronwalf
2ronwalfimport java.util.ArrayList;
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.List;
2ronwalfimport java.util.NoSuchElementException;
2ronwalf
2ronwalfimport org.mindswap.exceptions.InvalidListException;
2ronwalfimport org.mindswap.owl.OWLDataValue;
2ronwalfimport org.mindswap.owl.OWLFactory;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLIndividualList;
2ronwalfimport org.mindswap.owl.OWLValue;
2ronwalfimport org.mindswap.owl.list.ListVocabulary;
2ronwalfimport org.mindswap.owl.list.RDFList;
2ronwalfimport org.mindswap.owl.vocabulary.RDF;
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
38daenzerorama * @author Michael D�nzer (University of Zurich)
2ronwalf */
2ronwalfpublic class RDFListImpl extends WrappedIndividual implements RDFList {
2ronwalf protected ListVocabulary vocabulary;
2ronwalf
2ronwalf private class RDFListIterator implements Iterator {
2ronwalf private RDFList list;
2ronwalf
2ronwalf private RDFListIterator(RDFList list) {
2ronwalf this.list = list;
2ronwalf }
2ronwalf
2ronwalf public void remove() {
2ronwalf throw new UnsupportedOperationException("Cannot remove from ObjList iterator");
2ronwalf }
2ronwalf
2ronwalf public boolean hasNext() {
2ronwalf return !list.isEmpty();
2ronwalf }
2ronwalf
2ronwalf public Object next() {
2ronwalf if(list.isEmpty())
2ronwalf throw new NoSuchElementException();
2ronwalf
16daenzerorama Object result = list.getFirst();
2ronwalf list = list.getRest();
2ronwalf
2ronwalf if(result == null || list == null)
2ronwalf throw new InvalidListException();
2ronwalf
2ronwalf return result;
16daenzerorama }
2ronwalf }
2ronwalf
2ronwalf public RDFListImpl(OWLIndividual ind) {
2ronwalf super(ind);
2ronwalf
2ronwalf setVocabulary(RDF.ListVocabulary);
2ronwalf }
2ronwalf
2ronwalf public RDFList getRest() {
2ronwalf return (RDFList) getProperty(vocabulary.rest()).castTo(RDFList.class);
2ronwalf }
2ronwalf
2ronwalf public void setRest(RDFList rest) {
2ronwalf setProperty(vocabulary.rest(), rest);
2ronwalf }
16daenzerorama
16daenzerorama public void setRestToNil() {
16daenzerorama setProperty(vocabulary.rest(), vocabulary.nil());
16daenzerorama }
2ronwalf
2ronwalf public OWLIndividual getFirst() {
2ronwalf return (OWLIndividual) getFirstValue();
2ronwalf }
2ronwalf
2ronwalf public OWLValue getFirstValue() {
2ronwalf OWLValue value = getProperty( vocabulary.firstD() );
2ronwalf if( value == null )
2ronwalf value = getProperty( vocabulary.first() );
2ronwalf
2ronwalf return value;
2ronwalf }
2ronwalf
2ronwalf public void setFirst(OWLValue value) {
38daenzerorama // if (isEmpty())
38daenzerorama // throw new RuntimeException( "Cannot modify empty list (list:nil)" );
2ronwalf
16daenzerorama if (value == null)
16daenzerorama setProperty(vocabulary.firstD(), vocabulary.nil());
16daenzerorama else if (value instanceof OWLDataValue )
16daenzerorama setProperty(vocabulary.firstD(), (OWLDataValue) value );
2ronwalf else
16daenzerorama setProperty(vocabulary.first(), (OWLIndividual) value );
2ronwalf }
2ronwalf
2ronwalf public OWLIndividualList getAll() {
2ronwalf OWLIndividualList result = OWLFactory.createIndividualList();
2ronwalf for(RDFList list = this; !list.isEmpty(); list = list.getRest())
2ronwalf result.add(list.getFirst());
2ronwalf
2ronwalf return result;
2ronwalf }
2ronwalf
2ronwalf public List getAllValues() {
2ronwalf List result = new ArrayList();
2ronwalf for(RDFList list = this; !list.isEmpty(); list = list.getRest())
2ronwalf result.add(list.getFirstValue());
2ronwalf
2ronwalf return result;
2ronwalf }
2ronwalf
2ronwalf public OWLIndividual get(int index) {
2ronwalf return (OWLIndividual) getValue(index);
2ronwalf }
2ronwalf
2ronwalf public OWLValue getValue(int index) {
2ronwalf if(isEmpty())
2ronwalf throw new IndexOutOfBoundsException();
2ronwalf
2ronwalf return (index == 0) ? getFirstValue() : getRest().getValue(index - 1);
2ronwalf }
2ronwalf
2ronwalf public RDFList add(OWLValue item) {
15daenzerorama if (isEmpty())
2ronwalf return insert( item );
2ronwalf
2ronwalf RDFList rest = getRest();
2ronwalf
2ronwalf if( rest.isEmpty() )
15daenzerorama setRest(rest.insert(item));
2ronwalf else
2ronwalf rest.add(item);
2ronwalf
2ronwalf return this;
2ronwalf }
2ronwalf
2ronwalf public RDFList insert(OWLValue first) {
2ronwalf RDFListImpl list = new RDFListImpl( getOntology().createInstance( vocabulary.List() ) );
2ronwalf list.setVocabulary( vocabulary );
2ronwalf list.setFirst( first );
2ronwalf list.setRest( this );
2ronwalf
2ronwalf return list;
2ronwalf }
2ronwalf
2ronwalf public RDFList insertAt(int index, OWLValue value) {
2ronwalf if( index == 0 )
15daenzerorama return insert(value);
2ronwalf
2ronwalf if( index < 0 || isEmpty() )
2ronwalf throw new IndexOutOfBoundsException();
2ronwalf
2ronwalf RDFList rest = getRest();
2ronwalf return rest.insertAt( index - 1, value );
2ronwalf }
2ronwalf
15daenzerorama public RDFList remove(OWLValue value) {
16daenzerorama if ((value == null) || (size() == 0))
15daenzerorama return this;
17daenzerorama if ((size() == 1) && (getFirstValue().equals(value)))
16daenzerorama return remove();
16daenzerorama
15daenzerorama RDFList rest = this;
16daenzerorama int i = 0;
16daenzerorama while (!rest.isEmpty()) {
16daenzerorama if (rest.getFirstValue().equals(value))
16daenzerorama return removeAt(i);
16daenzerorama i++;
16daenzerorama rest = rest.getRest();
15daenzerorama }
15daenzerorama return this;
15daenzerorama }
15daenzerorama
15daenzerorama public RDFList removeAt(int index) {
15daenzerorama if (index == 0)
15daenzerorama return remove();
15daenzerorama
15daenzerorama if (index < 0 || isEmpty())
15daenzerorama throw new IndexOutOfBoundsException();
16daenzerorama
16daenzerorama setRest(getRest().removeAt(index - 1));
16daenzerorama return this;
15daenzerorama }
15daenzerorama
15daenzerorama public RDFList remove() {
18daenzerorama RDFList list = getRest();
18daenzerorama
16daenzerorama if (size() > 1) {
16daenzerorama list.setFirst(getRest().getFirstValue());
18daenzerorama list.setRest(getRest().getRest());
18daenzerorama } else {
18daenzerorama list = (RDFList) vocabulary.nil().castTo(RDFList.class);
16daenzerorama }
18daenzerorama if (hasProperty(vocabulary.first()))
18daenzerorama removeProperties(vocabulary.first());
18daenzerorama if (hasProperty(vocabulary.rest()))
18daenzerorama removeProperties(vocabulary.rest());
18daenzerorama individual.delete();
18daenzerorama
15daenzerorama return list;
15daenzerorama }
18daenzerorama
18daenzerorama public RDFList removeAll() {
18daenzerorama RDFList list = this;
18daenzerorama while (list.size() > 0)
18daenzerorama list = list.remove();
18daenzerorama return list;
18daenzerorama }
18daenzerorama
18daenzerorama @Override
18daenzerorama public void delete() {
18daenzerorama removeAll();
18daenzerorama individual.delete();
18daenzerorama }
15daenzerorama
2ronwalf public void set(int index, OWLValue value) {
2ronwalf if( isEmpty() )
2ronwalf throw new UnsupportedOperationException( "Cannot modify empty list (list:nil)" );
2ronwalf
2ronwalf int size = size();
2ronwalf if( index > size )
2ronwalf throw new IndexOutOfBoundsException();
2ronwalf else if( index == size )
2ronwalf add( value );
2ronwalf else {
2ronwalf RDFList list = this;
2ronwalf for(int i = 0; i < index; i++)
2ronwalf list = list.getRest();
2ronwalf
2ronwalf list.setFirst( value );
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf public Iterator iterator() {
2ronwalf return new RDFListIterator(this);
2ronwalf }
2ronwalf
16daenzerorama public int size() {
2ronwalf return isEmpty() ? 0 : 1 + getRest().size();
2ronwalf }
2ronwalf
2ronwalf public boolean isEmpty() {
16daenzerorama return equals(vocabulary.nil()) || (getFirstValue() != null && getFirstValue().equals(vocabulary.nil()));
2ronwalf }
2ronwalf
2ronwalf public ListVocabulary getVocabulary() {
2ronwalf return vocabulary;
2ronwalf }
2ronwalf
2ronwalf public void setVocabulary(ListVocabulary vocabulary) {
2ronwalf this.vocabulary = vocabulary;
2ronwalf }
2ronwalf}