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.collections;
025    
026    import java.util.Collection;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.ListIterator;
030    import java.util.Map;
031    
032    import org.ujmp.core.interfaces.Wrapper;
033    
034    public class MapToListWrapper<A> implements Wrapper<Map<Integer, A>>, List<A> {
035    
036            private Map<Integer, A> map = null;
037    
038            private int pos = 0;
039    
040            private int start = 0;
041    
042            public MapToListWrapper(Map<Integer, A> map) {
043                    this.map = map;
044                    start = 0;
045                    pos = map.size();
046            }
047    
048            public boolean add(A e) {
049                    map.put(pos++, e);
050                    return true;
051            }
052    
053            public void add(int index, A element) {
054                    throw new RuntimeException("not implemented");
055            }
056    
057            public boolean addAll(Collection<? extends A> c) {
058                    for (A a : c) {
059                            add(a);
060                    }
061                    return true;
062            }
063    
064            public boolean addAll(int index, Collection<? extends A> c) {
065                    throw new RuntimeException("not implemented");
066            }
067    
068            public void clear() {
069                    start = 0;
070                    pos = 0;
071                    map.clear();
072            }
073    
074            public boolean contains(Object o) {
075                    return map.containsValue(o);
076            }
077    
078            public boolean containsAll(Collection<?> c) {
079                    throw new RuntimeException("not implemented");
080            }
081    
082            public A get(int index) {
083                    return map.get(start + index);
084            }
085    
086            public int indexOf(Object o) {
087                    throw new RuntimeException("not implemented");
088            }
089    
090            public boolean isEmpty() {
091                    return map.isEmpty();
092            }
093    
094            public Iterator<A> iterator() {
095                    return map.values().iterator();
096            }
097    
098            public int lastIndexOf(Object o) {
099                    throw new RuntimeException("not implemented");
100            }
101    
102            public ListIterator<A> listIterator() {
103                    throw new RuntimeException("not implemented");
104            }
105    
106            public ListIterator<A> listIterator(int index) {
107                    throw new RuntimeException("not implemented");
108            }
109    
110            public boolean remove(Object o) {
111                    remove(indexOf(o));
112                    return true;
113            }
114    
115            public A remove(int index) {
116                    if (index == 0) {
117                            map.remove(start);
118                            start++;
119                    } else {
120                            throw new RuntimeException("not implemented");
121                    }
122                    return null;
123            }
124    
125            public boolean removeAll(Collection<?> c) {
126                    throw new RuntimeException("not implemented");
127            }
128    
129            public boolean retainAll(Collection<?> c) {
130                    throw new RuntimeException("not implemented");
131            }
132    
133            public A set(int index, A element) {
134                    map.put(start + index, element);
135                    return null;
136            }
137    
138            public int size() {
139                    return map.size();
140            }
141    
142            public List<A> subList(int fromIndex, int toIndex) {
143                    throw new RuntimeException("not implemented");
144            }
145    
146            public Object[] toArray() {
147                    throw new RuntimeException("not implemented");
148            }
149    
150            public <T> T[] toArray(T[] a) {
151                    throw new RuntimeException("not implemented");
152            }
153    
154            public Map<Integer, A> getWrappedObject() {
155                    return map;
156            }
157    
158            public void setWrappedObject(Map<Integer, A> object) {
159                    this.map = object;
160            }
161    
162    }