|
| 1 | +# `IdentityHashMap<K,V>` |
| 2 | + |
| 3 | +线性探测实现,key 为引用,判断 key 相等使用 `==` 而不是 `equals`。 |
| 4 | + |
| 5 | +## 容量 |
| 6 | + |
| 7 | +```java |
| 8 | +/** |
| 9 | + * The initial capacity used by the no-args constructor. |
| 10 | + * MUST be a power of two. The value 32 corresponds to the |
| 11 | + * (specified) expected maximum size of 21, given a load factor |
| 12 | + * of 2/3. |
| 13 | + */ |
| 14 | +private static final int DEFAULT_CAPACITY = 32; |
| 15 | + |
| 16 | +/** |
| 17 | + * The minimum capacity, used if a lower value is implicitly specified |
| 18 | + * by either of the constructors with arguments. The value 4 corresponds |
| 19 | + * to an expected maximum size of 2, given a load factor of 2/3. |
| 20 | + * MUST be a power of two. |
| 21 | + */ |
| 22 | +private static final int MINIMUM_CAPACITY = 4; |
| 23 | + |
| 24 | +/** |
| 25 | + * The maximum capacity, used if a higher value is implicitly specified |
| 26 | + * by either of the constructors with arguments. |
| 27 | + * MUST be a power of two <= 1<<29. |
| 28 | + * |
| 29 | + * In fact, the map can hold no more than MAXIMUM_CAPACITY-1 items |
| 30 | + * because it has to have at least one slot with the key == null |
| 31 | + * in order to avoid infinite loops in get(), put(), remove() |
| 32 | + */ |
| 33 | +private static final int MAXIMUM_CAPACITY = 1 << 29; |
| 34 | + |
| 35 | +/** |
| 36 | + * Returns the appropriate capacity for the given expected maximum size. |
| 37 | + * Returns the smallest power of two between MINIMUM_CAPACITY and |
| 38 | + * MAXIMUM_CAPACITY, inclusive, that is greater than (3 * |
| 39 | + * expectedMaxSize)/2, if such a number exists. Otherwise returns |
| 40 | + * MAXIMUM_CAPACITY. |
| 41 | + */ |
| 42 | +private static int capacity(int expectedMaxSize) { |
| 43 | + // assert expectedMaxSize >= 0; |
| 44 | + return |
| 45 | + // 期待容量大于最大容量的 1/3 直接使用最大容量 |
| 46 | + (expectedMaxSize > MAXIMUM_CAPACITY / 3) ? MAXIMUM_CAPACITY : |
| 47 | + // 期待容量小于等于最小容量的 2/3 直接使用最小容量 |
| 48 | + (expectedMaxSize <= 2 * MINIMUM_CAPACITY / 3) ? MINIMUM_CAPACITY : |
| 49 | + // 使用大于期待容量3倍的最小 2^n 次数作为容量 |
| 50 | + Integer.highestOneBit(expectedMaxSize + (expectedMaxSize << 1)); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | +* Initializes object to be an empty map with the specified initial |
| 55 | +* capacity, which is assumed to be a power of two between |
| 56 | +* MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive. |
| 57 | +*/ |
| 58 | +private void init(int initCapacity) { |
| 59 | + // assert (initCapacity & -initCapacity) == initCapacity; // power of 2 |
| 60 | + // assert initCapacity >= MINIMUM_CAPACITY; |
| 61 | + // assert initCapacity <= MAXIMUM_CAPACITY; |
| 62 | + |
| 63 | + table = new Object[2 * initCapacity]; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## 开源组件拓展 |
| 68 | + |
| 69 | +最核心的是 `AnnotationAwareOrderComparator`。 |
| 70 | + |
| 71 | +### Spring bean 排序 |
| 72 | + |
| 73 | +`DefaultListableBeanFactory` 中 `FactoryAwareOrderSourceProvider` 用于保存 bean instace 到 beanName 的 mapping,spring 框架使用 bean 的引用查找对应的 beanName, 然后查找对应的 `RootBeanDefinition` 然后确定 bean 的优先级。因为 instance 是引用,不适合使用 equals 来判断相等性,因为同一个类的实例,equals 判断相等,但是实际对应两个对象,这种情况转换 beanName -> instance 转为 instance->beanName 的过程会丢 bean,所以只能使用引用来判断相等性,所以需要使用 `IdentityHashMap`。 |
| 74 | + |
| 75 | +```java |
| 76 | +private OrderComparator.OrderSourceProvider createFactoryAwareOrderSourceProvider(Map<String, ?> beans) { |
| 77 | + IdentityHashMap<Object, String> instancesToBeanNames = new IdentityHashMap<>(); |
| 78 | + beans.forEach((beanName, instance) -> instancesToBeanNames.put(instance, beanName)); |
| 79 | + return new FactoryAwareOrderSourceProvider(instancesToBeanNames); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +```java |
| 84 | +/** |
| 85 | +* An {@link org.springframework.core.OrderComparator.OrderSourceProvider} implementation |
| 86 | +* that is aware of the bean metadata of the instances to sort. |
| 87 | +* <p>Lookup for the method factory of an instance to sort, if any, and let the |
| 88 | +* comparator retrieve the {@link org.springframework.core.annotation.Order} |
| 89 | +* value defined on it. This essentially allows for the following construct: |
| 90 | +*/ |
| 91 | +private class FactoryAwareOrderSourceProvider implements OrderComparator.OrderSourceProvider { |
| 92 | + |
| 93 | + private final Map<Object, String> instancesToBeanNames; |
| 94 | + |
| 95 | + public FactoryAwareOrderSourceProvider(Map<Object, String> instancesToBeanNames) { |
| 96 | + this.instancesToBeanNames = instancesToBeanNames; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + @Nullable |
| 101 | + public Object getOrderSource(Object obj) { |
| 102 | + // 根据引用,拿到 beanName,用 beanName 查找对应的 RootBeanDefinition |
| 103 | + RootBeanDefinition beanDefinition = getRootBeanDefinition(this.instancesToBeanNames.get(obj)); |
| 104 | + if (beanDefinition == null) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + List<Object> sources = new ArrayList<>(2); |
| 108 | + Method factoryMethod = beanDefinition.getResolvedFactoryMethod(); |
| 109 | + if (factoryMethod != null) { |
| 110 | + sources.add(factoryMethod); |
| 111 | + } |
| 112 | + Class<?> targetType = beanDefinition.getTargetType(); |
| 113 | + if (targetType != null && targetType != obj.getClass()) { |
| 114 | + sources.add(targetType); |
| 115 | + } |
| 116 | + return sources.toArray(); |
| 117 | + } |
| 118 | + |
| 119 | + @Nullable |
| 120 | + private RootBeanDefinition getRootBeanDefinition(@Nullable String beanName) { |
| 121 | + if (beanName != null && containsBeanDefinition(beanName)) { |
| 122 | + BeanDefinition bd = getMergedBeanDefinition(beanName); |
| 123 | + if (bd instanceof RootBeanDefinition) { |
| 124 | + return (RootBeanDefinition) bd; |
| 125 | + } |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### `HttpMessageConverter` |
| 133 | + |
| 134 | +```java |
| 135 | +@Configuration |
| 136 | +public class HttpMessageConverterConfig { |
| 137 | + @Bean |
| 138 | + @Order(1) |
| 139 | + public HttpMessageConverter<String> messageConverter1() { |
| 140 | + return new StringHttpMessageConverter(StandardCharsets.UTF_8); |
| 141 | + } |
| 142 | + |
| 143 | + @Bean |
| 144 | + @Order(2) |
| 145 | + public HttpMessageConverter<String> messageConverter2() { |
| 146 | + return new StringHttpMessageConverter(StandardCharsets.UTF_8); |
| 147 | + } |
| 148 | + |
| 149 | + @Bean |
| 150 | + @Order(3) |
| 151 | + public HttpMessageConverter<String> messageConverter3() { |
| 152 | + return new StringHttpMessageConverter(StandardCharsets.UTF_8); |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### `ApplicationContextInitializer` |
| 158 | + |
| 159 | +```java |
| 160 | +@Order(1) |
| 161 | +public class ApplicationContextInitializer1 implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 162 | + @Override |
| 163 | + public void initialize(ConfigurableApplicationContext applicationContext) { |
| 164 | + System.out.println(this.getClass().getName()); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +@Order(2) |
| 169 | +public class ApplicationContextInitializer2 implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 170 | + @Override |
| 171 | + public void initialize(ConfigurableApplicationContext applicationContext) { |
| 172 | + System.out.println(this.getClass().getName()); |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
0 commit comments