Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ListSet |
|
| 1.263157894736842;1.263 | ||||
ListSet$1 |
|
| 1.263157894736842;1.263 |
1 | /** | |
2 | * Distribution License: | |
3 | * JSword is free software; you can redistribute it and/or modify it under | |
4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
8 | * See the GNU Lesser General Public License for more details. | |
9 | * | |
10 | * The License is available on the internet at: | |
11 | * http://www.gnu.org/copyleft/lgpl.html | |
12 | * or by writing to: | |
13 | * Free Software Foundation, Inc. | |
14 | * 59 Temple Place - Suite 330 | |
15 | * Boston, MA 02111-1307, USA | |
16 | * | |
17 | * © CrossWire Bible Society, 2015 - 2016 | |
18 | */ | |
19 | package org.crosswire.common.util; | |
20 | ||
21 | import java.util.AbstractCollection; | |
22 | import java.util.ArrayList; | |
23 | import java.util.Collection; | |
24 | import java.util.Comparator; | |
25 | import java.util.Iterator; | |
26 | import java.util.List; | |
27 | import java.util.Set; | |
28 | import java.util.TreeSet; | |
29 | ||
30 | /** | |
31 | * Defines a List that maintains the uniqueness of elements | |
32 | * | |
33 | * @param <E> The type of the element in this ListSet. | |
34 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
35 | * @author DM Smith | |
36 | */ | |
37 | public class ListSet<E> extends AbstractCollection<E> implements Set<E> { | |
38 | ||
39 | /** | |
40 | * Constructs a new, empty list set. | |
41 | */ | |
42 | public ListSet() { | |
43 | 0 | this(null); |
44 | 0 | } |
45 | ||
46 | /** | |
47 | * Constructs a new, empty list set, sorted according to the specified | |
48 | * comparator. All elements inserted into the set must be <i>mutually | |
49 | * comparable</i> by the specified comparator: {@code comparator.compare(e1, | |
50 | * e2)} must not throw a {@code ClassCastException} for any elements | |
51 | * {@code e1} and {@code e2} in the set. If the user attempts to add | |
52 | * an element to the set that violates this constraint, the | |
53 | * {@code add} call will throw a {@code ClassCastException}. | |
54 | * | |
55 | * @param comparator the comparator that will be used to order this set. | |
56 | * If {@code null}, the {@linkplain Comparable natural | |
57 | * ordering} of the elements will be used. | |
58 | */ | |
59 | public ListSet(Comparator<? super E> comparator) { | |
60 | 0 | super(); |
61 | 0 | list = new ArrayList(); |
62 | 0 | set = new TreeSet(comparator); |
63 | 0 | } |
64 | ||
65 | @Override | |
66 | public boolean contains(Object o) { | |
67 | 0 | return set.contains(o); |
68 | } | |
69 | ||
70 | @Override | |
71 | public boolean add(E e) { | |
72 | 0 | boolean added = set.add(e); |
73 | 0 | if (added) { |
74 | 0 | list.add(e); |
75 | } | |
76 | 0 | return added; |
77 | } | |
78 | ||
79 | @Override | |
80 | public boolean remove(Object o) { | |
81 | 0 | boolean removed = set.remove(o); |
82 | 0 | if (removed) { |
83 | 0 | list.remove(o); |
84 | } | |
85 | 0 | return removed; |
86 | } | |
87 | ||
88 | public E remove(int index) { | |
89 | 0 | E t = list.get(index); |
90 | 0 | remove(t); |
91 | 0 | return t; |
92 | } | |
93 | ||
94 | @Override | |
95 | public boolean containsAll(Collection<?> c) { | |
96 | 0 | return set.containsAll(c); |
97 | } | |
98 | ||
99 | @Override | |
100 | public boolean retainAll(Collection<?> c) { | |
101 | 0 | boolean removed = false; |
102 | 0 | Iterator<E> it = this.iterator(); |
103 | 0 | while (it.hasNext()) { |
104 | 0 | E next = it.next(); |
105 | 0 | if (!c.contains(next)) { |
106 | 0 | it.remove(); |
107 | 0 | removed = true; |
108 | } | |
109 | 0 | } |
110 | 0 | return removed; |
111 | } | |
112 | ||
113 | @Override | |
114 | public int size() { | |
115 | 0 | return list.size(); |
116 | } | |
117 | ||
118 | @Override | |
119 | public boolean isEmpty() { | |
120 | 0 | return list.isEmpty(); |
121 | } | |
122 | ||
123 | @Override | |
124 | public void clear() { | |
125 | 0 | set.clear(); |
126 | 0 | list.clear(); |
127 | 0 | } |
128 | ||
129 | public E get(int index) { | |
130 | 0 | return list.get(index); |
131 | } | |
132 | ||
133 | public E get(int index, E defaultValue) { | |
134 | 0 | E value = list.get(index); |
135 | 0 | return value != null ? value : defaultValue; |
136 | } | |
137 | ||
138 | @Override | |
139 | public Iterator<E> iterator() { | |
140 | // Need to properly implement remove | |
141 | 0 | return new Iterator<E>() { |
142 | public boolean hasNext() { | |
143 | 0 | return itr.hasNext(); |
144 | } | |
145 | ||
146 | public E next() { | |
147 | 0 | E next = itr.next(); |
148 | 0 | current = next; |
149 | 0 | return next; |
150 | } | |
151 | ||
152 | public void remove() { | |
153 | 0 | itr.remove(); |
154 | 0 | set.remove(current); |
155 | 0 | current = null; |
156 | 0 | } |
157 | ||
158 | 0 | private Iterator<E> itr = list.iterator(); |
159 | private E current; | |
160 | }; | |
161 | } | |
162 | ||
163 | @Override | |
164 | public Object[] toArray() { | |
165 | 0 | return list.toArray(); |
166 | } | |
167 | ||
168 | @Override | |
169 | public <T> T[] toArray(T[] a) { | |
170 | 0 | return list.toArray(a); |
171 | } | |
172 | ||
173 | protected List<E> list; | |
174 | protected Set<E> set; | |
175 | } |