@@ -35,9 +35,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
3535 override val value: V
3636 get() = v
3737
38- override fun setValue (newValue : V ): V {
39- throw UnsupportedOperationException ()
40- }
38+ override fun setValue (newValue : V ): V = throw UnsupportedOperationException ()
4139 }
4240
4341 private var lock: Lock = Lock ()
@@ -56,22 +54,21 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
5654
5755 init {
5856 var capacity = 1
59- while (capacity < initialCapacity)
57+ while (capacity < initialCapacity) {
6058 capacity = capacity shl 1
59+ }
6160
6261 threshold = AtomicInt ((capacity.toFloat() * loadFactor).toInt())
6362 buckets = AtomicReference (makeBuckets(capacity))
6463
6564 freeze()
6665 }
6766
68- private fun makeBuckets (capacity : Int ): Array <AtomicReference <SharedLinkedList <Entry <K , V >>>> {
69- return (
70- Array (capacity) {
71- AtomicReference (SharedLinkedList <Entry <K , V >>(1 ).freeze())
72- }
73- ).freeze()
74- }
67+ private fun makeBuckets (capacity : Int ): Array <AtomicReference <SharedLinkedList <Entry <K , V >>>> = (
68+ Array (capacity) {
69+ AtomicReference (SharedLinkedList <Entry <K , V >>(1 ).freeze())
70+ }
71+ ).freeze()
7572
7673 private inline fun iterInternal (proc : (Entry <K , V >) -> Unit ) {
7774 buckets.value.forEach {
@@ -115,31 +112,19 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
115112 }
116113
117114 private class NotReallyMutableSet <T >(private val delegate : MutableCollection <T >) : MutableSet<T> {
118- override fun add (element : T ): Boolean {
119- throw UnsupportedOperationException ()
120- }
115+ override fun add (element : T ): Boolean = throw UnsupportedOperationException ()
121116
122- override fun addAll (elements : Collection <T >): Boolean {
123- throw UnsupportedOperationException ()
124- }
117+ override fun addAll (elements : Collection <T >): Boolean = throw UnsupportedOperationException ()
125118
126- override fun clear () {
127- throw UnsupportedOperationException ()
128- }
119+ override fun clear (): Unit = throw UnsupportedOperationException ()
129120
130121 override fun iterator (): MutableIterator <T > = delegate.iterator()
131122
132- override fun remove (element : T ): Boolean {
133- throw UnsupportedOperationException ()
134- }
123+ override fun remove (element : T ): Boolean = throw UnsupportedOperationException ()
135124
136- override fun removeAll (elements : Collection <T >): Boolean {
137- throw UnsupportedOperationException ()
138- }
125+ override fun removeAll (elements : Collection <T >): Boolean = throw UnsupportedOperationException ()
139126
140- override fun retainAll (elements : Collection <T >): Boolean {
141- throw UnsupportedOperationException ()
142- }
127+ override fun retainAll (elements : Collection <T >): Boolean = throw UnsupportedOperationException ()
143128
144129 override val size: Int
145130 get() = delegate.size
@@ -201,10 +186,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
201186 return result
202187 }
203188
204- private fun internalRemoveByKey (
205- entryList : SharedLinkedList <Entry <K , V >>,
206- key : K
207- ): V ? {
189+ private fun internalRemoveByKey (entryList : SharedLinkedList <Entry <K , V >>, key : K ): V ? {
208190 var result: AbstractSharedLinkedList .Node <Entry <K , V >>? = null
209191 val iter = entryList.nodeIterator()
210192 while (iter.hasNext()) {
@@ -250,7 +232,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
250232
251233 private fun transfer (
252234 newTable : Array <AtomicReference <SharedLinkedList <Entry <K , V >>>>,
253- oldTable : Array <AtomicReference <SharedLinkedList <Entry <K , V >>>>
235+ oldTable : Array <AtomicReference <SharedLinkedList <Entry <K , V >>>>,
254236 ) {
255237 oldTable.forEach {
256238 it.value.iterator().forEach {
@@ -262,9 +244,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
262244
263245 internal fun currentBucketSize (): Int = buckets.value.size
264246
265- private fun indexFor (h : Int , length : Int ): Int {
266- return h and length - 1
267- }
247+ private fun indexFor (h : Int , length : Int ): Int = h and length - 1
268248
269249 internal fun rehash (initHash : Int ): Int {
270250 var h = initHash
@@ -275,10 +255,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
275255 return h xor h.ushr(7 ) xor h.ushr(4 )
276256 }
277257
278- private fun findEntryList (
279- bucketArray : Array <AtomicReference <SharedLinkedList <Entry <K , V >>>>,
280- key : K
281- ): SharedLinkedList <Entry <K , V >> {
258+ private fun findEntryList (bucketArray : Array <AtomicReference <SharedLinkedList <Entry <K , V >>>>, key : K ): SharedLinkedList <Entry <K , V >> {
282259 val hash = rehash(key.hashCode())
283260 val entryList = bucketArray.get(indexFor(hash, bucketArray.size)).value
284261 return entryList
0 commit comments