All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmdx.state2.aop1.ListView Maven / Gradle / Ivy

There is a newer version: 2.18.10
Show newest version
/*
 * ====================================================================
 * Project:     openMDX, http://www.openmdx.org/
 * Description: List View
 * Owner:       OMEX AG, Switzerland, http://www.omex.ch
 * ====================================================================
 *
 * This software is published under the BSD license as listed below.
 * 
 * Copyright (c) 2008-2012, OMEX AG, Switzerland
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 * * Neither the name of the openMDX team nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * ------------------
 * 
 * This product includes software developed by other organizations as
 * listed in the NOTICE file.
 */
package org.openmdx.state2.aop1;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.openmdx.base.accessor.cci.DataObject_1_0;
import org.openmdx.base.exception.ServiceException;
import org.openmdx.kernel.exception.BasicException;
import org.openmdx.kernel.exception.Throwables;

/**
 * List View
 */
final class ListView
    extends CollectionView, Object>
    implements List {

    /**
     * Constructor
     *
     * @param involvedMembers
     */
    private ListView(
        InvolvedMembers> involvedMembers
    ) {
        super(involvedMembers);
    }

    /**
     * List View Factory Method
     * 
     * @param involvedStates
     * @param feature
     * 
     * @return a new list view
     */
    static List newObjectList(
        final Involved involvedStates,
        final String feature
    ) {
        return new ListView(
            new InvolvedMembers>(
                involvedStates,
                feature
            ) {

                @Override
                protected List getMember(
                    DataObject_1_0 state
                )
                    throws ServiceException {
                    return state.objGetList(feature);
                }

            }
        );

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#iterator()
     */
    public Iterator iterator() {
        return listIterator();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#add(int, java.lang.Object)
     */
    public void add(
        int index,
        Object element
    ) {
        for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
            if (index > delegate.size()) {
                throw new IndexOutOfBoundsException(
                    "The size of the list in one of the underlying states is to small"
                );
            }
        }
        for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
            delegate.add(index, element);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#addAll(int, java.util.Collection)
     */
    public boolean addAll(
        int index,
        Collection c
    ) {
        for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
            if (index > delegate.size()) {
                throw new IndexOutOfBoundsException(
                    "The size of the list in one of the underlyaing states is to small"
                );
            }
        }
        boolean modified = false;
        for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
            modified |= delegate.addAll(index, c);
        }
        return modified;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#get(int)
     */
    public Object get(int index) {
        try {
            UniqueValue reply = new UniqueValue();
            for (List delegate : members.getInvolved(AccessMode.FOR_QUERY)) {
                reply.set(delegate.get(index));
            }
            return reply.get();
        } catch (ServiceException exception) {
            throw newIllegalStateException(
                exception, "The underlying states are inappropriate for the unique determination of the given value"
            );
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#indexOf(java.lang.Object)
     */
    public int indexOf(Object o) {
        try {
            UniqueValue reply = new UniqueValue();
            for (List delegate : this.members.getInvolved(AccessMode.FOR_QUERY)) {
                reply.set(Integer.valueOf(delegate.indexOf(o)));
            }
            return reply.get().intValue();
        } catch (ServiceException exception) {
            final String message = "The underlying states are inappropriate for the unique determination of the given property";
            throw newIllegalStateException(exception, message);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#lastIndexOf(java.lang.Object)
     */
    public int lastIndexOf(Object o) {
        try {
            UniqueValue reply = new UniqueValue();
            for (List delegate : this.members.getInvolved(AccessMode.FOR_QUERY)) {
                reply.set(Integer.valueOf(delegate.lastIndexOf(o)));
            }
            return reply.get().intValue();
        } catch (ServiceException exception) {
            throw newIllegalStateException(
                exception,
                "The underlying states are inappropriate for the unique determination of the given property"
            );
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#listIterator()
     */
    public ListIterator listIterator() {
        return listIterator(0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#listIterator(int)
     */
    public ListIterator listIterator(int index) {
        return new ViewIterator(index);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#remove(int)
     */
    public Object remove(int index) {
        try {
            UniqueValue reply = new UniqueValue();
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                reply.set(delegate.get(index));
            }
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                delegate.remove(index);
            }
            return reply.get();
        } catch (ServiceException exception) {
            throw newIllegalStateException(exception, "The underlying states are inappropriate for the given operation");
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#set(int, java.lang.Object)
     */
    public Object set(
        int index,
        Object element
    ) {
        try {
            UniqueValue reply = new UniqueValue();
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                reply.set(delegate.get(index));
            }
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                delegate.set(index, element);
            }
            return reply.get();
        } catch (ServiceException exception) {
            throw newIllegalStateException(exception, "The underlying states are inappropriate for the given operation");
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.List#subList(int, int)
     */
    public List subList(
        final int fromIndex,
        final int toIndex
    ) {
        return new ListView(
            new InvolvedMembers>(
                members.involvedStates,
                members.feature
            ) {

                @Override
                protected List getMember(
                    DataObject_1_0 state
                )
                    throws ServiceException {
                    return state.objGetList(feature).subList(fromIndex, toIndex);
                }

            }

        );
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof List) {
            List that = (List) obj;
            int size = this.size();
            if (size == that.size()) {
                for (int i = 0; i < size; i++) {
                    Object thisMember = this.get(i);
                    Object thatMember = that.get(i);
                    if (thisMember == null ? thatMember != null : !thisMember.equals(thatMember)) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        int hash = 1;
        for (Object member : this) {
            hash *= 31;
            hash += member == null ? 0 : member.hashCode();
        }
        return hash;
    }

    //------------------------------------------------------------------------
    // Class ViewIterator
    //------------------------------------------------------------------------

    /**
     * View Iterator
     */
    class ViewIterator implements ListIterator {

        /**
         * Constructor
         *
         * @param index
         */
        ViewIterator(
            int index
        ) {
            this.nextIndex = index;
            this.previousIndex = index - 1;
            this.currentIndex = -1;
        }

        /**
         * 
         */
        private int nextIndex;

        /**
         * 
         */
        private int previousIndex;

        /**
         * 
         */
        private int currentIndex;

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#add(java.lang.Object)
         */
        public void add(Object o) {
            if (this.currentIndex < 0) {
                throw new IllegalStateException("No current element");
            }
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                delegate.add(this.nextIndex, o);
            }
            this.previousIndex++;
            this.nextIndex++;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#hasNext()
         */
        public boolean hasNext() {
            try {
                UniqueValue reply = new UniqueValue();
                for (List delegate : members.getInvolved(AccessMode.FOR_QUERY)) {
                    reply.set(Boolean.valueOf(nextIndex < delegate.size()));
                }
                return reply.get().booleanValue();
            } catch (ServiceException exception) {
                throw newIllegalStateException(
                    exception,
                    "The underlying states are inappropriate for the unique determination of the given property"
                );
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#hasPrevious()
         */
        public boolean hasPrevious() {
            return this.previousIndex >= 0;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#next()
         */
        public Object next() {
            try {
                UniqueValue reply = new UniqueValue();
                this.currentIndex = this.nextIndex;
                for (List delegate : members.getInvolved(AccessMode.FOR_QUERY)) {
                    reply.set(delegate.get(this.currentIndex));
                }
                this.previousIndex = this.nextIndex++;
                return reply.get();
            } catch (ServiceException exception) {
                throw newIllegalStateException(exception, "The underlying states are inappropriate for the given operation");
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#nextIndex()
         */
        public int nextIndex() {
            return this.nextIndex;

        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#previous()
         */
        public Object previous() {
            try {
                UniqueValue reply = new UniqueValue();
                this.currentIndex = this.previousIndex;
                for (List delegate : members.getInvolved(AccessMode.FOR_QUERY)) {
                    reply.set(delegate.get(this.currentIndex));
                }
                this.nextIndex = this.previousIndex--;
                return reply.get();
            } catch (ServiceException exception) {
                throw newIllegalStateException(exception, "The underlying states are inappropriate for the given operation");
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#previousIndex()
         */
        public int previousIndex() {
            return this.previousIndex;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#remove()
         */
        public void remove() {
            if (this.currentIndex < 0) {
                throw new IllegalStateException("No current element");
            }
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                delegate.remove(this.currentIndex);
            }
            this.currentIndex = -1;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.util.ListIterator#set(java.lang.Object)
         */
        public void set(Object o) {
            if (this.currentIndex < 0) {
                throw new IllegalStateException("No current element");
            }
            for (List delegate : members.getInvolved(AccessMode.FOR_UPDATE)) {
                delegate.set(this.currentIndex, o);
            }
        }

    }

    private IllegalStateException newIllegalStateException(
        ServiceException exception,
        final String message
    ) {
        return Throwables.initCause(
            new IllegalStateException(
                message + ". " + exception.getCause().getDescription()
            ),
            exception,
            BasicException.Code.DEFAULT_DOMAIN,
            BasicException.Code.ILLEGAL_STATE,
            getIdParameter()
        );
    }

}