📄 collections.java
字号:
/* * $Source: /home/data/cvsroot/src/jacomma/util/Collections.java,v $ * $Revision: 1.4 $ * $Date: 2000/10/28 20:09:08 $ * * This file is part of the jacomma framework * Copyright (c) 2000 Dimitrios Vyzovitis * mailto:dviz@egnatia.ee.auth.gr * * * * * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA */package jacomma.util;import java.util.Map;import java.util.Set;import java.util.Collection;import java.util.List;import java.util.SortedSet;import java.util.SortedMap;import java.util.Hashtable;/** * TBA **/public class Collections { public static final Object toImmutable( Object obj ) { Class[] a = new Class[1]; for ( TypeIterator it = new TypeIterator( obj ); it.hasMoreTypes(); ) { try { a[0] = it.nextType(); return Collections.class.getMethod( "toImmutable", a ) .invoke( null, new Object[]{ obj } ); } catch ( Exception exc ){} } throw new RuntimeException( "Bad Object Type: Not a Collection?" ); } public static final Map toImmutable( Map m ) { return java.util.Collections.unmodifiableMap( m ); } public static final Set toImmutable( Set s ) { return java.util.Collections.unmodifiableSet( s ); } public static final Collection toImmutable( Collection c ) { return java.util.Collections.unmodifiableCollection( c ); } public static final List toImmutable( List l ) { return java.util.Collections.unmodifiableList( l ); } public static final SortedSet toImmutable( SortedSet s ) { return java.util.Collections.unmodifiableSortedSet( s ); } public static final SortedMap toImmutable( SortedMap m ) { return java.util.Collections.unmodifiableSortedMap( m ); } public static final Object reifyImmutable( Object obj ) { try { return obj.getClass().getMethod( "clone", null ).invoke( obj, null ); } catch ( Exception exc ) { // ok, do the magic try { java.lang.reflect.Constructor c = (java.lang.reflect.Constructor)bindings_.get( obj.getClass() ); return c.newInstance( new Object[]{ obj } ); } catch ( Exception exc2 ) { exc2.printStackTrace(); throw new RuntimeException( "Bad Object type: Not an immutable collection?" ); } } } public static final Object toSynchronized( Object obj ) { Class[] a = new Class[1]; for ( TypeIterator it = new TypeIterator( obj ); it.hasMoreTypes(); ) { try { a[0] = it.nextType(); return Collections.class.getMethod( "toSynchronized", a ) .invoke( null, new Object[]{ obj } ); } catch ( Exception exc ){} } throw new RuntimeException( "Bad Object Type: Not a Collection?" ); } public static final Map toSynchronized( Map m ) { return java.util.Collections.synchronizedMap( m ); } public static final Set toSynchronized( Set s ) { return java.util.Collections.synchronizedSet( s ); } public static final Collection toSynchronized( Collection c ) { return java.util.Collections.synchronizedCollection( c ); } public static final List toSynchronized( List l ) { return java.util.Collections.synchronizedList( l ); } public static final SortedSet toSynchronized( SortedSet s ) { return java.util.Collections.synchronizedSortedSet( s ); } public static final SortedMap toSynchronized( SortedMap m ) { return java.util.Collections.synchronizedSortedMap( m ); } static final Hashtable bindings_; static { try { bindings_ = new Hashtable(); // a bit of a hack... // the alternative solution is to do it with priviledge access, which is // unecessary complicated ;-) String[] types = { "java.util.Collections$UnmodifiableCollection", "java.util.Collections$UnmodifiableSet", "java.util.Collections$UnmodifiableList", "java.util.Collections$UnmodifiableMap", "java.util.Collections$UnmodifiableSortedSet", "java.util.Collections$UnmodifiableSortedMap" }; Class[] bds = { java.util.Vector.class, java.util.HashSet.class , java.util.ArrayList.class, java.util.HashMap.class, java.util.TreeSet.class, java.util.TreeMap.class }; Class[] tgt = { Collection.class, Collection.class, Collection.class, Map.class, SortedSet.class, SortedMap.class }; Class[] t = new Class[1]; for ( int i = 0; i < types.length; i++ ) { t[0] = tgt[i]; bindings_.put( Class.forName( types[i] ), bds[i].getConstructor( t ) ); } } catch ( Exception exc ) { exc.printStackTrace(); throw new RuntimeException( "Oops... Error setting up immutable collection reification." ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -