001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.setmatrix;
025    
026    import java.util.Collection;
027    import java.util.Iterator;
028    import java.util.Set;
029    
030    import org.ujmp.core.enums.ValueType;
031    import org.ujmp.core.exceptions.MatrixException;
032    import org.ujmp.core.genericmatrix.stub.AbstractDenseGenericMatrix2D;
033    import org.ujmp.core.util.MathUtil;
034    
035    public abstract class AbstractSetMatrix<A> extends AbstractDenseGenericMatrix2D<A> implements
036                    SetMatrix<A> {
037            private static final long serialVersionUID = -3152489258987719660L;
038    
039            public abstract Set<A> getSet();
040    
041            public final long[] getSize() {
042                    return new long[] { size(), 1 };
043            }
044    
045            public boolean add(A e) {
046                    boolean ret = getSet().add(e);
047                    notifyGUIObject();
048                    return ret;
049            }
050    
051            public boolean addAll(Collection<? extends A> c) {
052                    boolean ret = getSet().addAll(c);
053                    notifyGUIObject();
054                    return ret;
055            }
056    
057            public boolean contains(Object o) {
058                    return getSet().contains(o);
059            }
060    
061            public boolean containsAll(Collection<?> c) {
062                    return getSet().containsAll(c);
063            }
064    
065            
066            public boolean isEmpty() {
067                    return getSet().isEmpty();
068            }
069    
070            public Iterator<A> iterator() {
071                    return getSet().iterator();
072            }
073    
074            public boolean remove(Object o) {
075                    boolean ret = getSet().remove(o);
076                    notifyGUIObject();
077                    return ret;
078            }
079    
080            public boolean removeAll(Collection<?> c) {
081                    boolean ret = getSet().removeAll(c);
082                    notifyGUIObject();
083                    return ret;
084            }
085    
086            public boolean retainAll(Collection<?> c) {
087                    boolean ret = getSet().retainAll(c);
088                    notifyGUIObject();
089                    return ret;
090            }
091    
092            public int size() {
093                    return getSet().size();
094            }
095    
096            public A getObject(long row, long column) {
097                    return getObject((int) row, (int) column);
098            }
099    
100            public A getObject(int row, int column) {
101                    Iterator<A> it = getSet().iterator();
102                    for (int i = 0; i < row && it.hasNext(); i++) {
103                            it.next();
104                    }
105                    if (it.hasNext()) {
106                            return it.next();
107                    } else {
108                            return null;
109                    }
110            }
111    
112            public void setObject(A value, long row, long column) {
113                    throw new MatrixException("modifications are only allowed over Set<?> interface");
114            }
115    
116            public void setObject(A value, int row, int column) {
117                    throw new MatrixException("modifications are only allowed over Set<?> interface");
118            }
119    
120            public Object[] toArray() {
121                    return getSet().toArray();
122            }
123    
124            public <T> T[] toArray(T[] a) {
125                    return getSet().toArray(a);
126            }
127    
128            
129            public double getAsDouble(long... coordinates) throws MatrixException {
130                    return MathUtil.getDouble(getObject(coordinates));
131            }
132    
133            
134            public void setAsDouble(double value, long... coordinates) throws MatrixException {
135                    setAsObject(value, coordinates);
136            }
137    
138            
139            public ValueType getValueType() {
140                    return ValueType.OBJECT;
141            }
142    
143            
144            public final StorageType getStorageType() {
145                    return StorageType.SET;
146            }
147    
148            public final void clear() {
149                    getSet().clear();
150            }
151    
152    }