01: //$Id: LinkedHashCollectionHelper.java 10100 2006-07-10 16:31:09Z steve.ebersole@jboss.com $
02: package org.hibernate.util;
03:
04: import java.util.Map;
05: import java.util.Set;
06: import java.lang.reflect.Constructor;
07:
08: import org.hibernate.AssertionFailure;
09:
10: public final class LinkedHashCollectionHelper {
11:
12: private static final Class SET_CLASS;
13: private static final Class MAP_CLASS;
14: private static final Class[] CAPACITY_CTOR_SIG = new Class[] {
15: int.class, float.class };
16: private static final Constructor SET_CAPACITY_CTOR;
17: private static final Constructor MAP_CAPACITY_CTOR;
18: private static final float LOAD_FACTOR_V = .75f;
19: private static final Float LOAD_FACTOR = new Float(LOAD_FACTOR_V);
20:
21: static {
22: Class setClass;
23: Class mapClass;
24: Constructor setCtor;
25: Constructor mapCtor;
26: try {
27: setClass = Class.forName("java.util.LinkedHashSet");
28: mapClass = Class.forName("java.util.LinkedHashMap");
29: setCtor = setClass.getConstructor(CAPACITY_CTOR_SIG);
30: mapCtor = mapClass.getConstructor(CAPACITY_CTOR_SIG);
31: } catch (Throwable t) {
32: setClass = null;
33: mapClass = null;
34: setCtor = null;
35: mapCtor = null;
36: }
37: SET_CLASS = setClass;
38: MAP_CLASS = mapClass;
39: SET_CAPACITY_CTOR = setCtor;
40: MAP_CAPACITY_CTOR = mapCtor;
41: }
42:
43: public static Set createLinkedHashSet() {
44: try {
45: return (Set) SET_CLASS.newInstance();
46: } catch (Exception e) {
47: throw new AssertionFailure(
48: "Could not instantiate LinkedHashSet", e);
49: }
50: }
51:
52: public static Set createLinkedHashSet(int anticipatedSize) {
53: if (anticipatedSize <= 0) {
54: return createLinkedHashSet();
55: }
56: int initialCapacity = anticipatedSize
57: + (int) (anticipatedSize * LOAD_FACTOR_V);
58: try {
59: return (Set) SET_CAPACITY_CTOR.newInstance(new Object[] {
60: new Integer(initialCapacity), LOAD_FACTOR });
61: } catch (Exception e) {
62: throw new AssertionFailure(
63: "Could not instantiate LinkedHashSet", e);
64: }
65: }
66:
67: public static Map createLinkedHashMap() {
68: try {
69: return (Map) MAP_CLASS.newInstance();
70: } catch (Exception e) {
71: throw new AssertionFailure(
72: "Could not instantiate LinkedHashMap", e);
73: }
74: }
75:
76: public static Map createLinkedHashMap(int anticipatedSize) {
77: if (anticipatedSize <= 0) {
78: return createLinkedHashMap();
79: }
80: int initialCapacity = anticipatedSize
81: + (int) (anticipatedSize * LOAD_FACTOR_V);
82: try {
83: return (Map) MAP_CAPACITY_CTOR.newInstance(new Object[] {
84: new Integer(initialCapacity), LOAD_FACTOR });
85: } catch (Exception e) {
86: throw new AssertionFailure(
87: "Could not instantiate LinkedHashMap", e);
88: }
89: }
90:
91: private LinkedHashCollectionHelper() {
92: }
93:
94: }
|