blob: 72c48ae7520b57709d877d4d161f1f632dc5e353 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// +build !go1.9
2
3package swag
4
5import (
6 "sort"
7 "sync"
8)
9
10// indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
11// Before go1.9, this may be implemented with a mutex on the map.
12type indexOfInitialisms struct {
13 getMutex *sync.Mutex
14 index map[string]bool
15}
16
17func newIndexOfInitialisms() *indexOfInitialisms {
18 return &indexOfInitialisms{
19 getMutex: new(sync.Mutex),
20 index: make(map[string]bool, 50),
21 }
22}
23
24func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {
25 m.getMutex.Lock()
26 defer m.getMutex.Unlock()
27 for k, v := range initial {
28 m.index[k] = v
29 }
30 return m
31}
32
33func (m *indexOfInitialisms) isInitialism(key string) bool {
34 m.getMutex.Lock()
35 defer m.getMutex.Unlock()
36 _, ok := m.index[key]
37 return ok
38}
39
40func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {
41 m.getMutex.Lock()
42 defer m.getMutex.Unlock()
43 m.index[key] = true
44 return m
45}
46
47func (m *indexOfInitialisms) sorted() (result []string) {
48 m.getMutex.Lock()
49 defer m.getMutex.Unlock()
50 for k := range m.index {
51 result = append(result, k)
52 }
53 sort.Sort(sort.Reverse(byLength(result)))
54 return
55}