|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2014 Klemm Software Consulting, Mirko Klemm |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.kscs.util.jaxb; |
| 26 | + |
| 27 | +import java.beans.PropertyVetoException; |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author klemm0 2014-03-27 |
| 32 | + * |
| 33 | + */ |
| 34 | +public class BoundListProxy<E> implements BoundList<E> { |
| 35 | + private final List<E> list; |
| 36 | + private final List<VetoableCollectionChangeListener<E>> vetoableCollectionChangeListeners = new ArrayList<VetoableCollectionChangeListener<E>>(); |
| 37 | + private final List<CollectionChangeListener<E>> collectionChangeListeners = new ArrayList<CollectionChangeListener<E>>(); |
| 38 | + |
| 39 | + public BoundListProxy(final List<E> list) { |
| 40 | + this.list = list; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public int size() { |
| 45 | + return this.list.size(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isEmpty() { |
| 50 | + return this.list.isEmpty(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean contains(final Object o) { |
| 55 | + return this.list.contains(o); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Iterator<E> iterator() { |
| 60 | + return this.list.iterator(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Object[] toArray() { |
| 65 | + return this.list.toArray(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public <T> T[] toArray(final T[] a) { |
| 70 | + return this.list.toArray(a); |
| 71 | + } |
| 72 | + |
| 73 | + public boolean add(final E e) { |
| 74 | + final CollectionChangeEvent<E> event = checkCollectionChange("add", CollectionChangeEventType.ADD, new ArrayList<E>(this.list), Arrays.asList(e), this.list.size()); |
| 75 | + final boolean retVal = this.list.add(e); |
| 76 | + fireCollectionChange(event); |
| 77 | + return retVal; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean remove(final Object o) { |
| 82 | + final CollectionChangeEvent<E> event = checkCollectionChange("remove", CollectionChangeEventType.REMOVE, new ArrayList<E>(this.list), Arrays.asList((E)o), -1); |
| 83 | + final boolean retVal = this.list.remove(o); |
| 84 | + fireCollectionChange(event); |
| 85 | + return retVal; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean containsAll(final Collection<?> c) { |
| 90 | + return this.list.containsAll(c); |
| 91 | + } |
| 92 | + |
| 93 | + public boolean addAll(final Collection<? extends E> c) { |
| 94 | + final CollectionChangeEvent<E> event = checkCollectionChange("addAll", CollectionChangeEventType.ADD_ALL, new ArrayList<E>(this.list), c, this.list.size()); |
| 95 | + final boolean retVal = this.list.addAll(c); |
| 96 | + fireCollectionChange(event); |
| 97 | + return retVal; |
| 98 | + } |
| 99 | + |
| 100 | + public boolean addAll(final int index, final Collection<? extends E> c) { |
| 101 | + final CollectionChangeEvent<E> event = checkCollectionChange("addAll", CollectionChangeEventType.ADD_ALL_AT, new ArrayList<E>(this.list), c, this.list.size()); |
| 102 | + final boolean retVal = this.list.addAll(index, c); |
| 103 | + fireCollectionChange(event); |
| 104 | + return retVal; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean removeAll(final Collection<?> c) { |
| 109 | + final CollectionChangeEvent<E> event = checkCollectionChange("removeAll", CollectionChangeEventType.REMOVE_ALL, new ArrayList<E>(this.list), (Collection<E>)c, -1); |
| 110 | + final boolean retVal = this.list.removeAll(c); |
| 111 | + fireCollectionChange(event); |
| 112 | + return retVal; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean retainAll(final Collection<?> c) { |
| 117 | + final CollectionChangeEvent<E> event = checkCollectionChange("retainAll", CollectionChangeEventType.RETAIN_ALL, new ArrayList<E>(this.list), (Collection<E>)c, -1); |
| 118 | + final boolean retVal = this.list.retainAll(c); |
| 119 | + fireCollectionChange(event); |
| 120 | + return retVal; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void clear() { |
| 125 | + final CollectionChangeEvent<E> event = checkCollectionChange("clear", CollectionChangeEventType.RETAIN_ALL, new ArrayList<E>(this.list), Collections.<E>emptyList(), -1); |
| 126 | + this.list.clear(); |
| 127 | + fireCollectionChange(event); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean equals(final Object o) { |
| 132 | + return this.list.equals(o); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public int hashCode() { |
| 137 | + return this.list.hashCode(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public E get(final int index) { |
| 142 | + return this.list.get(index); |
| 143 | + } |
| 144 | + |
| 145 | + public E set(final int index, final E element) { |
| 146 | + final CollectionChangeEvent<E> event = checkCollectionChange("set", CollectionChangeEventType.SET_AT, new ArrayList<E>(this.list), Arrays.asList(element), index); |
| 147 | + final E retVal = this.list.set(index, element); |
| 148 | + fireCollectionChange(event); |
| 149 | + return retVal; |
| 150 | + } |
| 151 | + |
| 152 | + public void add(final int index, final E element) { |
| 153 | + final CollectionChangeEvent<E> event = checkCollectionChange("add", CollectionChangeEventType.ADD_AT, new ArrayList<E>(this.list), Arrays.asList(element), index); |
| 154 | + this.list.add(index, element); |
| 155 | + fireCollectionChange(event); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public E remove(final int index) { |
| 160 | + final CollectionChangeEvent<E> event = checkCollectionChange("remove", CollectionChangeEventType.REMOVE_AT, new ArrayList<E>(this.list), Collections.<E>emptyList(), index); |
| 161 | + final E retVal = this.list.remove(index); |
| 162 | + fireCollectionChange(event); |
| 163 | + return retVal; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public int indexOf(final Object o) { |
| 168 | + return this.list.indexOf(o); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public int lastIndexOf(final Object o) { |
| 173 | + return this.list.lastIndexOf(o); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public ListIterator<E> listIterator() { |
| 178 | + return this.list.listIterator(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public ListIterator<E> listIterator(final int index) { |
| 183 | + return this.list.listIterator(index); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public List<E> subList(final int fromIndex, final int toIndex) { |
| 188 | + return this.list.subList(fromIndex, toIndex); |
| 189 | + } |
| 190 | + |
| 191 | + protected CollectionChangeEvent<E> checkCollectionChange(final String methodName, final CollectionChangeEventType eventType, final Collection<E> oldItems, final Collection<? extends E> newItems, final int index) { |
| 192 | + try { |
| 193 | + final CollectionChangeEvent<E> event = new CollectionChangeEvent<E>( |
| 194 | + this, |
| 195 | + methodName, |
| 196 | + eventType, |
| 197 | + oldItems, |
| 198 | + newItems, |
| 199 | + index |
| 200 | + ); |
| 201 | + |
| 202 | + for(final VetoableCollectionChangeListener<E> listener : this.vetoableCollectionChangeListeners) { |
| 203 | + listener.vetoableCollectionChange(event); |
| 204 | + } |
| 205 | + return event; |
| 206 | + } catch(final PropertyVetoException pvx) { |
| 207 | + throw new RuntimeException(pvx); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + protected void fireCollectionChange(final CollectionChangeEvent<E> event) { |
| 212 | + for(final CollectionChangeListener<E> listener : this.collectionChangeListeners) { |
| 213 | + listener.collectionChange(event); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public void addCollectionChangeListener(final CollectionChangeListener<E> collectionChangeListener) { |
| 219 | + this.collectionChangeListeners.add(collectionChangeListener); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void addVetoableCollectionChangeListener(final VetoableCollectionChangeListener<E> vetoableCollectionChangeListener) { |
| 224 | + this.vetoableCollectionChangeListeners.add(vetoableCollectionChangeListener); |
| 225 | + } |
| 226 | +} |
0 commit comments