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.io.Serializable; 027 import java.util.AbstractList; 028 import java.util.ArrayList; 029 import java.util.Collection; 030 import java.util.Comparator; 031 import java.util.Iterator; 032 import java.util.List; 033 import java.util.ListIterator; 034 import java.util.SortedSet; 035 import java.util.TreeSet; 036 037 import org.ujmp.core.exceptions.MatrixException; 038 039 public class SortedListSet<A> extends AbstractList<A> implements SortedSet<A>, List<A>, 040 Serializable { 041 private static final long serialVersionUID = -9069699328418816771L; 042 043 private SortedSet<A> set = new TreeSet<A>(); 044 045 private List<A> list = new ArrayList<A>(); 046 047 public SortedListSet() { 048 } 049 050 public Comparator<? super A> comparator() { 051 return set.comparator(); 052 } 053 054 public A first() { 055 return set.first(); 056 } 057 058 public SortedSet<A> headSet(A toElement) { 059 return set.headSet(toElement); 060 } 061 062 public A last() { 063 return set.last(); 064 } 065 066 public SortedSet<A> subSet(A fromElement, A toElement) { 067 return set.subSet(fromElement, toElement); 068 } 069 070 public SortedSet<A> tailSet(A fromElement) { 071 return set.tailSet(fromElement); 072 } 073 074 public synchronized boolean add(A e) { 075 list.clear(); 076 return set.add(e); 077 } 078 079 public synchronized boolean addAll(Collection<? extends A> c) { 080 list.clear(); 081 return set.addAll(c); 082 } 083 084 public synchronized void clear() { 085 list.clear(); 086 set.clear(); 087 } 088 089 public boolean contains(Object o) { 090 return set.contains(o); 091 } 092 093 public boolean containsAll(Collection<?> c) { 094 return set.containsAll(c); 095 } 096 097 public boolean isEmpty() { 098 return set.isEmpty(); 099 } 100 101 public Iterator<A> iterator() { 102 return set.iterator(); 103 } 104 105 public synchronized boolean remove(Object o) { 106 list.clear(); 107 return set.remove(o); 108 } 109 110 public synchronized boolean removeAll(Collection<?> c) { 111 list.clear(); 112 return set.removeAll(c); 113 } 114 115 public synchronized boolean retainAll(Collection<?> c) { 116 list.clear(); 117 return set.retainAll(c); 118 } 119 120 public int size() { 121 return set.size(); 122 } 123 124 public Object[] toArray() { 125 return set.toArray(); 126 } 127 128 public <T> T[] toArray(T[] a) { 129 return set.toArray(a); 130 } 131 132 public void add(int index, A element) { 133 throw new MatrixException("not implemented"); 134 } 135 136 public boolean addAll(int index, Collection<? extends A> c) { 137 throw new MatrixException("not implemented"); 138 } 139 140 public synchronized A get(int index) { 141 createList(); 142 return list.get(index); 143 } 144 145 public synchronized int indexOf(Object o) { 146 createList(); 147 return list.indexOf(o); 148 } 149 150 public synchronized int lastIndexOf(Object o) { 151 createList(); 152 return list.lastIndexOf(o); 153 } 154 155 public synchronized ListIterator<A> listIterator() { 156 createList(); 157 return list.listIterator(); 158 } 159 160 public synchronized ListIterator<A> listIterator(int index) { 161 createList(); 162 return list.listIterator(index); 163 } 164 165 public synchronized A remove(int index) { 166 createList(); 167 A o = list.remove(index); 168 list.clear(); 169 set.remove(o); 170 return o; 171 } 172 173 public A set(int index, A element) { 174 throw new MatrixException("not implemented"); 175 } 176 177 public synchronized List<A> subList(int fromIndex, int toIndex) { 178 createList(); 179 return list.subList(fromIndex, toIndex); 180 } 181 182 private void createList() { 183 if (list.size() != set.size()) { 184 list.clear(); 185 list.addAll(set); 186 } 187 } 188 189 }