| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import Collection from './Collection'
- import NoSuchElementException from './NoSuchElementException'
- import UnsupportedOperationException from '../lang/UnsupportedOperationException'
- import Set from './Set'
- /**
- * @see http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html
- */
- export default class HashSet extends Set {
- constructor(o) {
- super()
- this.map = new Map()
- if (o instanceof Collection)
- this.addAll(o)
- }
- contains(o) {
- const hashCode = o.hashCode ? o.hashCode() : o
- if (this.map.has(hashCode))
- return true
- return false
- }
- add(o) {
- const hashCode = o.hashCode ? o.hashCode() : o
- if (this.map.has(hashCode))
- return false
- return !!this.map.set(hashCode, o)
- }
- addAll(c) {
- for (const e of c)
- this.add(e)
- return true
- }
- remove() {
- throw new UnsupportedOperationException()
- }
- size() {
- return this.map.size
- }
- isEmpty() {
- return this.map.size === 0
- }
- toArray() {
- return Array.from(this.map.values())
- }
- iterator() {
- return new Iterator(this.map)
- }
- [Symbol.iterator]() {
- return this.map
- }
- }
- class Iterator {
- constructor(map) {
- this.iterator = map.values()
- const { done, value } = this.iterator.next()
- this.done = done
- this.value = value
- }
- next() {
- if (this.done)
- throw new NoSuchElementException()
- const current = this.value
- const { done, value } = this.iterator.next()
- this.done = done
- this.value = value
- return current
- }
- hasNext() {
- return !this.done
- }
- remove() {
- throw new UnsupportedOperationException()
- }
- }
|