| 1 | package org.jtoolkit.essence.utils; |
| 2 | |
| 3 | import org.jtoolkit.essence.app.pojo.DatableUtils; |
| 4 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 5 | import org.jtoolkit.essence.concurrency.Immutable; |
| 6 | |
| 7 | import java.util.*; |
| 8 | |
| 9 | /* |
| 10 | Copyright 2006 Peter Lawrey |
| 11 | |
| 12 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | you may not use this file except in compliance with the License. |
| 14 | You may obtain a copy of the License at |
| 15 | |
| 16 | http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | |
| 18 | Unless required by applicable law or agreed to in writing, software |
| 19 | distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | See the License for the specific language governing permissions and |
| 22 | limitations under the License. |
| 23 | */ |
| 24 | /** |
| 25 | * @author Peter Lawrey |
| 26 | */ |
| 27 | |
| 28 | @SuppressWarnings({"unchecked"}) |
| 29 | public class ImmutableUtils { |
| 30 | private static final Set<Class> IS_IMMUTABLE = new HashSet<Class>(1024); |
| 31 | |
| 32 | static { |
| 33 | IS_IMMUTABLE.addAll(Arrays.asList(DatableUtils.BASE_CLASSES)); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | private ImmutableUtils() { |
| 38 | } |
| 39 | |
| 40 | public static <E> Set<E> immutableCopySet(Set<E> set) throws IllegalArgumentException { |
| 41 | return ImmutableSet.copy(set); |
| 42 | } |
| 43 | |
| 44 | public static <E> List<E> immutableCopyList(Collection<E> list) throws IllegalArgumentException { |
| 45 | return ImmutableList.copy(new ArrayList<E>(list)); |
| 46 | } |
| 47 | |
| 48 | public static <E> List<E> immutableCopyList(List<E> list) throws IllegalArgumentException { |
| 49 | return ImmutableList.copy(list); |
| 50 | } |
| 51 | |
| 52 | public static <K, V> Map<K, V> immutableCopyMap(Map<K, V> map) throws IllegalArgumentException { |
| 53 | return ImmutableMap.copy(map); |
| 54 | } |
| 55 | |
| 56 | public static <T> T immutableCopy(T object) throws IllegalArgumentException { |
| 57 | if (object == null) //noinspection ConstantConditions |
| 58 | return null; |
| 59 | T object2 = tryImmutableCopy(object); |
| 60 | if (isImmutable(object2.getClass())) return object2; |
| 61 | throw new IllegalArgumentException("Unable to make instance of " + object.getClass() + " immutable."); |
| 62 | } |
| 63 | |
| 64 | @SuppressWarnings({"MethodWithMultipleReturnPoints"}) |
| 65 | public static <T> T tryImmutableCopy(T object) { |
| 66 | if (object == null) return null; |
| 67 | if (isImmutable(object.getClass())) return object; |
| 68 | try { |
| 69 | if (object instanceof SortedMap) return (T) ImmutableSortedMap.copy((SortedMap) object); |
| 70 | if (object instanceof SortedSet) return (T) ImmutableSortedSet.copy((SortedSet) object); |
| 71 | if (object instanceof Map) return (T) ImmutableMap.copy((Map) object); |
| 72 | if (object instanceof Set) return (T) ImmutableSet.copy((Set) object); |
| 73 | if (object instanceof List) return (T) ImmutableList.copy((List) object); |
| 74 | if (object instanceof Collection) return (T) ImmutableList.copy(new ArrayList((Collection) object)); |
| 75 | } catch (IllegalArgumentException ignored) { |
| 76 | // ignored |
| 77 | } |
| 78 | return object; |
| 79 | } |
| 80 | |
| 81 | public static <K, V> SortedMap<K, V> immutableWrap(SortedMap<K, V> map) { |
| 82 | if (isImmutable(map.getClass())) return map; |
| 83 | return new ImmutableSortedMap<K, V>(map); |
| 84 | } |
| 85 | |
| 86 | public static <K, V> Map<K, V> immutableWrap(Map<K, V> map) { |
| 87 | if (isImmutable(map.getClass())) return map; |
| 88 | if (map instanceof SortedMap) |
| 89 | return new ImmutableSortedMap<K, V>((SortedMap<K, V>) map); |
| 90 | return new ImmutableMap<K, V>(map); |
| 91 | } |
| 92 | |
| 93 | public static <E> SortedSet<E> immutableWrap(SortedSet<E> set) { |
| 94 | if (isImmutable(set.getClass())) return set; |
| 95 | return new ImmutableSortedSet<E>(set); |
| 96 | } |
| 97 | |
| 98 | public static <E> Set<E> immutableWrap(Set<E> set) { |
| 99 | if (isImmutable(set.getClass())) return set; |
| 100 | if (set instanceof SortedSet) |
| 101 | return new ImmutableSortedSet<E>((SortedSet<E>) set); |
| 102 | return new ImmutableSet<E>(set); |
| 103 | } |
| 104 | |
| 105 | public static <E> List<E> immutableWrap(List<E> list) { |
| 106 | if (isImmutable(list.getClass())) return list; |
| 107 | return new ImmutableList<E>(list); |
| 108 | } |
| 109 | |
| 110 | public static <E> List<E> immutableWrap(Collection<E> collection) { |
| 111 | return new ImmutableList<E>(new ArrayList<E>(collection)); |
| 112 | } |
| 113 | |
| 114 | public static boolean isImmutable(Class clazz) { |
| 115 | if (IS_IMMUTABLE.contains(clazz)) return true; |
| 116 | if (clazz.getAnnotation(Immutable.class) == null && !DataValueClass.isAssignableFrom(Enum.class, clazz)) |
| 117 | return false; |
| 118 | synchronized (IS_IMMUTABLE) { |
| 119 | IS_IMMUTABLE.add(clazz); |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | // create unmodifyable collections with the @Immutable annotation and can be serialized/deserialized with DatableUtils. |
| 125 | @Immutable |
| 126 | static class ImmutableMap<K, V> extends AbstractMap<K, V> { |
| 127 | private final Map<K, V> map; |
| 128 | |
| 129 | ImmutableMap(Map<K, V> map) { |
| 130 | this.map = map; |
| 131 | } |
| 132 | |
| 133 | static <K, V> ImmutableMap<K, V> copy(Map<K, V> map) { |
| 134 | Map<K, V> map2 = new LinkedHashMap<K, V>(); |
| 135 | for (Map.Entry<K, V> entry : map.entrySet()) |
| 136 | map2.put(tryImmutableCopy(entry.getKey()), tryImmutableCopy(entry.getValue())); |
| 137 | return new ImmutableMap(map2); |
| 138 | } |
| 139 | |
| 140 | public boolean containsKey(Object key) { |
| 141 | return map.containsKey(key); |
| 142 | } |
| 143 | |
| 144 | public boolean containsValue(Object value) { |
| 145 | return map.containsValue(value); |
| 146 | } |
| 147 | |
| 148 | public V get(Object key) { |
| 149 | return map.get(key); |
| 150 | } |
| 151 | |
| 152 | public Set<K> keySet() { |
| 153 | return Collections.unmodifiableSet(map.keySet()); |
| 154 | } |
| 155 | |
| 156 | public int size() { |
| 157 | return map.size(); |
| 158 | } |
| 159 | |
| 160 | public Collection<V> values() { |
| 161 | return Collections.unmodifiableCollection(map.values()); |
| 162 | } |
| 163 | |
| 164 | public Set<Entry<K, V>> entrySet() { |
| 165 | return Collections.unmodifiableMap(map).entrySet(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Immutable |
| 170 | static class ImmutableSortedMap<K, V> extends AbstractMap<K, V> implements SortedMap<K, V> { |
| 171 | private final SortedMap<K, V> map; |
| 172 | |
| 173 | ImmutableSortedMap(SortedMap<K, V> map) { |
| 174 | this.map = map; |
| 175 | } |
| 176 | |
| 177 | static <K, V> ImmutableSortedMap<K, V> copy(SortedMap<K, V> map) { |
| 178 | SortedMap<K, V> map2 = new TreeMap<K, V>(map.comparator()); |
| 179 | for (Map.Entry<K, V> entry : map.entrySet()) |
| 180 | map2.put(tryImmutableCopy(entry.getKey()), tryImmutableCopy(entry.getValue())); |
| 181 | return new ImmutableSortedMap<K, V>(map2); |
| 182 | } |
| 183 | |
| 184 | public Comparator<? super K> comparator() { |
| 185 | return map.comparator(); |
| 186 | } |
| 187 | |
| 188 | public K firstKey() { |
| 189 | return map.firstKey(); |
| 190 | } |
| 191 | |
| 192 | public SortedMap<K, V> headMap(K toKey) { |
| 193 | return Collections.unmodifiableSortedMap(map.headMap(toKey)); |
| 194 | } |
| 195 | |
| 196 | public K lastKey() { |
| 197 | return map.lastKey(); |
| 198 | } |
| 199 | |
| 200 | public SortedMap<K, V> subMap(K fromKey, K toKey) { |
| 201 | return Collections.unmodifiableSortedMap(map.subMap(fromKey, toKey)); |
| 202 | } |
| 203 | |
| 204 | public SortedMap<K, V> tailMap(K fromKey) { |
| 205 | return Collections.unmodifiableSortedMap(map.tailMap(fromKey)); |
| 206 | } |
| 207 | |
| 208 | public boolean containsKey(Object key) { |
| 209 | return map.containsKey(key); |
| 210 | } |
| 211 | |
| 212 | public boolean containsValue(Object value) { |
| 213 | return map.containsValue(value); |
| 214 | } |
| 215 | |
| 216 | public V get(Object key) { |
| 217 | return map.get(key); |
| 218 | } |
| 219 | |
| 220 | public Set<K> keySet() { |
| 221 | return Collections.unmodifiableSet(map.keySet()); |
| 222 | } |
| 223 | |
| 224 | public int size() { |
| 225 | return map.size(); |
| 226 | } |
| 227 | |
| 228 | public String toString() { |
| 229 | return map.toString(); |
| 230 | } |
| 231 | |
| 232 | public Collection<V> values() { |
| 233 | return Collections.unmodifiableCollection(map.values()); |
| 234 | } |
| 235 | |
| 236 | public Set<Entry<K, V>> entrySet() { |
| 237 | return Collections.unmodifiableMap(map).entrySet(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | @Immutable |
| 242 | static class ImmutableSet<E> extends AbstractSet<E> { |
| 243 | private final Set<E> set; |
| 244 | |
| 245 | ImmutableSet(Set<E> set) { |
| 246 | this.set = set; |
| 247 | } |
| 248 | |
| 249 | static <E> ImmutableSet<E> copy(Set<E> set) { |
| 250 | Set<E> set2 = new LinkedHashSet<E>(); |
| 251 | for (E element : set) |
| 252 | set2.add(tryImmutableCopy(element)); |
| 253 | return new ImmutableSet<E>(set2); |
| 254 | } |
| 255 | |
| 256 | public boolean contains(Object o) { |
| 257 | return set.contains(o); |
| 258 | } |
| 259 | |
| 260 | public boolean containsAll(Collection<?> c) { |
| 261 | return set.containsAll(c); |
| 262 | } |
| 263 | |
| 264 | public String toString() { |
| 265 | return set.toString(); |
| 266 | } |
| 267 | |
| 268 | public Iterator<E> iterator() { |
| 269 | return Collections.unmodifiableSet(set).iterator(); |
| 270 | } |
| 271 | |
| 272 | public int size() { |
| 273 | return set.size(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | @Immutable |
| 278 | static class ImmutableSortedSet<E> extends AbstractSet<E> implements SortedSet<E> { |
| 279 | private final SortedSet<E> set; |
| 280 | |
| 281 | ImmutableSortedSet(SortedSet<E> set) { |
| 282 | this.set = set; |
| 283 | } |
| 284 | |
| 285 | static <E> ImmutableSortedSet<E> copy(SortedSet<E> set) { |
| 286 | SortedSet<E> set2 = new TreeSet<E>(set.comparator()); |
| 287 | for (E element : set) |
| 288 | set2.add(tryImmutableCopy(element)); |
| 289 | return new ImmutableSortedSet<E>(set2); |
| 290 | } |
| 291 | |
| 292 | public Comparator<? super E> comparator() { |
| 293 | return set.comparator(); |
| 294 | } |
| 295 | |
| 296 | public E first() { |
| 297 | return set.first(); |
| 298 | } |
| 299 | |
| 300 | public SortedSet<E> headSet(E toElement) { |
| 301 | return Collections.unmodifiableSortedSet(set.headSet(toElement)); |
| 302 | } |
| 303 | |
| 304 | public E last() { |
| 305 | return set.last(); |
| 306 | } |
| 307 | |
| 308 | public SortedSet<E> subSet(E fromElement, E toElement) { |
| 309 | return Collections.unmodifiableSortedSet(set.subSet(fromElement, toElement)); |
| 310 | } |
| 311 | |
| 312 | public SortedSet<E> tailSet(E fromElement) { |
| 313 | return Collections.unmodifiableSortedSet(set.tailSet(fromElement)); |
| 314 | } |
| 315 | |
| 316 | public boolean contains(Object o) { |
| 317 | return set.contains(o); |
| 318 | } |
| 319 | |
| 320 | public boolean containsAll(Collection<?> c) { |
| 321 | return set.containsAll(c); |
| 322 | } |
| 323 | |
| 324 | public Iterator<E> iterator() { |
| 325 | return Collections.unmodifiableSet(set).iterator(); |
| 326 | } |
| 327 | |
| 328 | public int size() { |
| 329 | return set.size(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | @Immutable |
| 334 | static class ImmutableList<E> extends AbstractList<E> { |
| 335 | private final List<E> list; |
| 336 | |
| 337 | ImmutableList(List<E> list) { |
| 338 | this.list = list; |
| 339 | } |
| 340 | |
| 341 | static <E> ImmutableList<E> copy(List<E> list) { |
| 342 | List<E> list2 = new ArrayList<E>(); |
| 343 | for (E element : list) |
| 344 | list2.add(tryImmutableCopy(element)); |
| 345 | return new ImmutableList<E>(list2); |
| 346 | } |
| 347 | |
| 348 | public E get(int index) { |
| 349 | return list.get(index); |
| 350 | } |
| 351 | |
| 352 | public Iterator<E> iterator() { |
| 353 | return Collections.unmodifiableList(list).iterator(); |
| 354 | } |
| 355 | |
| 356 | public int size() { |
| 357 | return list.size(); |
| 358 | } |
| 359 | } |
| 360 | } |