1// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build !go1.9
16
17package swag
18
19import (
20	"sort"
21	"sync"
22)
23
24// indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
25// Before go1.9, this may be implemented with a mutex on the map.
26type indexOfInitialisms struct {
27	getMutex *sync.Mutex
28	index    map[string]bool
29}
30
31func newIndexOfInitialisms() *indexOfInitialisms {
32	return &indexOfInitialisms{
33		getMutex: new(sync.Mutex),
34		index:    make(map[string]bool, 50),
35	}
36}
37
38func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {
39	m.getMutex.Lock()
40	defer m.getMutex.Unlock()
41	for k, v := range initial {
42		m.index[k] = v
43	}
44	return m
45}
46
47func (m *indexOfInitialisms) isInitialism(key string) bool {
48	m.getMutex.Lock()
49	defer m.getMutex.Unlock()
50	_, ok := m.index[key]
51	return ok
52}
53
54func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {
55	m.getMutex.Lock()
56	defer m.getMutex.Unlock()
57	m.index[key] = true
58	return m
59}
60
61func (m *indexOfInitialisms) sorted() (result []string) {
62	m.getMutex.Lock()
63	defer m.getMutex.Unlock()
64	for k := range m.index {
65		result = append(result, k)
66	}
67	sort.Sort(sort.Reverse(byInitialism(result)))
68	return
69}
70