001// ______________________________________________________ 002// Generated by sql2java - https://github.com/10km/sql2java-2-6-7 (custom branch) 003// modified by guyadong from 004// sql2java original version https://sourceforge.net/projects/sql2java/ 005// JDBC driver used at code generation time: com.mysql.jdbc.Driver 006// template: collection.utils.java.vm 007// ______________________________________________________ 008package net.gdface.utils; 009 010import java.util.AbstractMap; 011import java.util.AbstractSet; 012import java.util.Collection; 013import java.util.Iterator; 014import java.util.Map; 015import java.util.Set; 016import java.util.Objects; 017 018/** 019 * @author guyadong 020 * 021 */ 022public class CollectionUtils { 023 private CollectionUtils() {} 024 public static interface Function<F, T> { 025 /** 026 * function interface 027 * @param input 028 * @return 029 */ 030 T apply( F input); 031 } 032 /** 033 * dual transform type between L and R<br> 034 * {@link #toRight(Object)} L -> R<br> 035 * {@link #fromRight(Object)} R -> L 036 * @author guyadong 037 * 038 * @param <L> left type 039 * @param <R> right type 040 */ 041 public static interface DualTransformer<L,R>{ 042 /** 043 * cast L to R 044 * @param input 045 * @return 046 */ 047 R toRight(L input); 048 /** 049 * cast R to L 050 * @param input 051 * @return 052 */ 053 L fromRight(R input); 054 } 055 public static final <L,R> DualTransformer<L,R> asDualTransformer(final Function<L,R> t1,final Function<R,L>t2){ 056 checkNotNull(t1); 057 checkNotNull(t2); 058 return new DualTransformer<L,R>(){ 059 @Override 060 public R toRight(L input) { 061 return t1.apply(input); 062 } 063 @Override 064 public L fromRight(R input) { 065 return t2.apply(input); 066 } 067 }; 068 } 069 public static final <L,R> Function<L,R> asToRightTransformer(final DualTransformer<L,R> dual){ 070 checkNotNull(dual); 071 return new Function<L,R>(){ 072 @Override 073 public R apply(L input) { 074 return dual.toRight(input); 075 } 076 }; 077 } 078 public static final <L,R> Function<R,L> asFromRightTransformer(final DualTransformer<L,R> dual){ 079 checkNotNull(dual); 080 return new Function<R,L>(){ 081 @Override 082 public L apply(R input) { 083 return dual.fromRight(input); 084 } 085 }; 086 } 087 /** 088 * Ensures that an object reference passed as a parameter to the calling method is not null. 089 * @return the non-null reference that was validated 090 */ 091 public static <T> T checkNotNull(T reference) { 092 if (reference == null) { 093 throw new NullPointerException(); 094 } 095 return reference; 096 } 097 /** 098 * Ensures that an object reference passed as a parameter to the calling method is not null. 099 * @return the non-null reference that was validated 100 */ 101 public static <T> T checkNotNull(T reference, Object errorMessage) { 102 if (reference == null) { 103 throw new NullPointerException(String.valueOf(errorMessage)); 104 } 105 return reference; 106 } 107 /** 108 * Ensures that a collection reference passed as a parameter to the calling method not exists null element if it's not null. 109 * @return the non-null-element reference that was validated 110 */ 111 public static<C extends Collection<?>> C checkNotNullElement(C c){ 112 if(null != c){ 113 for(Object e:c){ 114 checkNotNull(e,"element of collection must not be null"); 115 } 116 } 117 return c; 118 } 119 static <V> V safeGet(Map<?, V> map, Object key) { 120 checkNotNull(map); 121 try { 122 return map.get(key); 123 } catch (ClassCastException e) { 124 return null; 125 } catch (NullPointerException e) { 126 return null; 127 } 128 } 129 /** @see #tranformKeys(Map, DualTransformer)*/ 130 public static final<K1,K2,V>Map<K2,V> tranformKeys(Map<K1,V>fromMap, 131 Function<K1, K2> transformer2, 132 Function<K2, K1> transformer1){ 133 return tranformKeys(fromMap,asDualTransformer(transformer2,transformer1)); 134 } 135 /** 136 * Returns a view of a map where each value is transformed by a function. All 137 * other properties of the map, such as iteration order, are left intact. <br> 138 * see also {@link com.google.common.collect.Maps#transformEntries(Map, com.google.common.collect.Maps.EntryTransformer)} 139 * @param fromMap 140 * @param transformer 141 * @return 142 */ 143 public static final<K1,K2,V>Map<K2,V> tranformKeys(Map<K1,V>fromMap, 144 DualTransformer<K1, K2> transformer){ 145 return new TransformedMap<K1, K2, V>(fromMap,transformer); 146 } 147 /** @see #transform(Set, DualTransformer)*/ 148 public static final <E1,E2>Set<E2>transform(Set<E1> fromSet, 149 Function<E1, E2> transformer2, 150 Function<E2, E1> transformer1){ 151 return transform(fromSet,asDualTransformer(transformer2,transformer1)); 152 } 153 /** 154 * Returns a view of a set where each key is transformed by a function. All 155 * other properties of the map, such as iteration order, are left intact. <br> 156 * see also {@link com.google.common.collect.Maps#transformEntries(Map, com.google.common.collect.Maps.EntryTransformer)} 157 * @param fromSet 158 * @param transformer 159 * @return 160 */ 161 public static final <E1,E2>Set<E2>transform(Set<E1> fromSet, 162 DualTransformer<E1,E2> transformer){ 163 return new TransformedSet<E1,E2>(fromSet,transformer); 164 } 165 166 /** 167 * Returns an iterator that applies {@code function} to each element of {@code fromIterator}.<br> 168 * see also {@link com.google.common.collect.Iterators#transform(Iterator, com.google.common.base.Function)} 169 * @param fromIterator 170 * @param transformer 171 * @return 172 */ 173 public static <F, T> Iterator<T> transform(final Iterator<F> fromIterator, 174 final Function<? super F, ? extends T> transformer) { 175 return new TransformedIterator<F, T>(fromIterator,transformer); 176 } 177 static class TransformedIterator<F, T> implements Iterator<T> { 178 final Iterator<? extends F> backingIterator; 179 private Function<? super F, ? extends T> transformer; 180 181 TransformedIterator(Iterator<? extends F> backingIterator,Function<? super F, ? extends T> transformer) { 182 this.backingIterator = checkNotNull(backingIterator); 183 this.transformer = checkNotNull(transformer); 184 } 185 186 @Override 187 public final boolean hasNext() { 188 return backingIterator.hasNext(); 189 } 190 191 @Override 192 public final T next() { 193 return transformer.apply(backingIterator.next()); 194 } 195 196 @Override 197 public final void remove() { 198 backingIterator.remove(); 199 } 200 } 201 static class TransformedSet<E1, E2> extends AbstractSet<E2> { 202 final Set<E1> fromSet; 203 private final DualTransformer<E1, E2> transformer; 204 205 TransformedSet(Set<E1> fromSet, DualTransformer<E1, E2> transformer) { 206 checkNotNull(fromSet); 207 checkNotNull(transformer); 208 this.fromSet = fromSet; 209 this.transformer = transformer; 210 } 211 @Override 212 public int size() { 213 return fromSet.size(); 214 } 215 216 @Override 217 public Iterator<E2> iterator() { 218 return transform(fromSet.iterator(), asToRightTransformer(transformer)); 219 } 220 221 @Override 222 public boolean add(E2 e) { 223 return fromSet.add(transformer.fromRight(e)); 224 } 225 226 @SuppressWarnings("unchecked") 227 @Override 228 public boolean contains(Object o) { 229 try { 230 return fromSet.contains(transformer.fromRight((E2) o)); 231 } catch (ClassCastException e) { 232 return false; 233 } 234 } 235 236 @SuppressWarnings("unchecked") 237 @Override 238 public boolean remove(Object o) { 239 try { 240 return fromSet.remove(transformer.fromRight((E2) o)); 241 } catch (ClassCastException e) { 242 return false; 243 } 244 } 245 } 246 247 static class TransformedMap<K1, K2, V> extends AbstractMap<K2, V> { 248 final Map<K1, V> fromMap; 249 private DualTransformer<K1,K2> transformer; 250 TransformedMap(Map<K1, V> fromMap, DualTransformer<K1,K2> transformer) { 251 checkNotNull(fromMap); 252 checkNotNull(transformer); 253 this.fromMap = fromMap; 254 this.transformer = transformer; 255 } 256 @SuppressWarnings("unchecked") 257 @Override 258 public V get(Object key) { 259 try { 260 return fromMap.get(transformer.fromRight((K2) key)); 261 } catch (ClassCastException e) { 262 return null; 263 } 264 } 265 266 @SuppressWarnings("unchecked") 267 @Override 268 public boolean containsKey(Object key) { 269 try { 270 return fromMap.containsKey(transformer.fromRight((K2) key)); 271 } catch (ClassCastException e) { 272 return false; 273 } 274 } 275 276 @Override 277 public V put(K2 key, V value) { 278 return fromMap.put(transformer.fromRight(key), value); 279 } 280 281 @SuppressWarnings("unchecked") 282 @Override 283 public V remove(Object key) { 284 try { 285 return fromMap.remove(transformer.fromRight((K2) key)); 286 } catch (ClassCastException e) { 287 return null; 288 } 289 } 290 291 @Override 292 public Set<Entry<K2, V>> entrySet() { 293 return new AbstractEntrySet<K2,V>(){ 294 295 @Override 296 Map<K2, V> map() { 297 return TransformedMap.this; 298 } 299 300 @Override 301 public Iterator<java.util.Map.Entry<K2, V>> iterator() { 302 return transform(fromMap.entrySet().iterator(), 303 new Function<Entry<K1,V>,Entry<K2,V>>(){ 304 @Override 305 public java.util.Map.Entry<K2, V> apply(java.util.Map.Entry<K1, V> input) { 306 return new TransformedEntry<K1,K2,V>(input,asToRightTransformer(TransformedMap.this.transformer)); 307 }}); 308 }}; 309 } 310 abstract static class AbstractEntrySet<K, V> extends AbstractSet<Entry<K, V>> { 311 /** 312 * return map instance 313 * @return 314 */ 315 abstract Map<K, V> map(); 316 317 @Override 318 public int size() { 319 return map().size(); 320 } 321 322 @Override 323 public boolean contains(Object o) { 324 if (o instanceof Entry) { 325 Entry<?, ?> entry = (Entry<?, ?>) o; 326 Object key = entry.getKey(); 327 V value = safeGet(map(), key); 328 return Objects.equals(value, entry.getValue()) 329 && (value != null || map().containsKey(entry.getKey())); 330 } 331 return false; 332 } 333 334 @Override 335 public boolean remove(Object o) { 336 if (contains(o)) { 337 Entry<?, ?> entry = (Entry<?, ?>) o; 338 return map().keySet().remove(entry.getKey()); 339 } 340 return false; 341 } 342 } 343 static class TransformedEntry<K1,K2,V> implements Entry<K2, V> { 344 final Entry<K1,V>fromEntry; 345 final Function<K1, K2> transformer2; 346 TransformedEntry(Entry<K1,V>fromEntry,Function<K1, K2> transformer2){ 347 checkNotNull(fromEntry); 348 this.fromEntry = fromEntry; 349 checkNotNull(transformer2); 350 this.transformer2 = transformer2; 351 } 352 @Override 353 public K2 getKey() { 354 return transformer2.apply(fromEntry.getKey()); 355 } 356 357 @Override 358 public V getValue() { 359 return fromEntry.getValue(); 360 } 361 362 @Override 363 public V setValue(V value) { 364 return fromEntry.setValue(value); 365 } 366 367 @Override 368 public boolean equals(Object object) { 369 if(object instanceof TransformedEntry){ 370 return fromEntry.equals((TransformedEntry<?, ?, ?>)object); 371 } 372 return super.equals(object); 373 } 374 375 @Override 376 public int hashCode() { 377 return fromEntry.hashCode(); 378 } 379 380 @Override 381 public String toString() { 382 return fromEntry.toString(); 383 } 384 } 385 } 386}